Upgrade to Pro — share decks privately, control downloads, hide ads and more …

サーバサイドKotlinへの入門 Ktor編

サーバサイドKotlinへの入門 Ktor編

Transcript

  1.         Kotlin 

    Ktor 1    Copyright © 2019 Toranoana Inc. All Rights Reserved.
  2. ,' ,' ,' ,' , ' ,' +) %" #(

    • 2019 3!& • *Java  • .- /$ •   –  0 – &  2 Copyright © 2019 Toranoana Inc. All Rights Reserved.
  3.          1.

    Kotlin 2.   3. Ktor  4.  (Routing) 5.  (Locations) 6. Basic! (Authenticaton) 7.  3 Copyright © 2019 Toranoana Inc. All Rights Reserved.
  4. % $ % $ % $ % $ % $

    % $ 1.Kotlin  # *" →SparkFramework!Java/Kotlin  Java  & Kotlin*"+ Java '()  4 Copyright © 2019 Toranoana Inc. All Rights Reserved.
  5. . + . + . + . + . +

    . + 2.) • Ktor JetBrains*# Web 1.0.02018"11$  (! 1.2.1(2019/5/27) Kotlin)1 %&, 0/ '-Features# 5 Copyright © 2019 Toranoana Inc. All Rights Reserved.
  6.        3.Ktor https://start.ktor.io/ 6

    Copyright © 2019 Toranoana Inc. All Rights Reserved.
  7.        3.Ktor  IntellijKtor

       7 Copyright © 2019 Toranoana Inc. All Rights Reserved.
  8.        3.Ktor 9 

    Features Routing Locations Authentication Copyright © 2019 Toranoana Inc. All Rights Reserved.
  9. + ) + ) + ) + ) + )

    + ) 3.Ktor  10 fun main(args: Array<String>) { val server = embeddedServer(Netty, 8080) { this.module(false) } server.start(wait = true) } fun Application.module(testing: Boolean = false) { routing { get("/") { call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain) } } } Application.kt #'! $*"-& ,. "!!!/Netty "/8080 " HELLOWORLD! "%( Copyright © 2019 Toranoana Inc. All Rights Reserved.
  10.        3.Ktor 11 Copyright

    © 2019 Toranoana Inc. All Rights Reserved.
  11.        4.  (Routing)

    12 Copyright © 2019 Toranoana Inc. All Rights Reserved.    • /user/{name} {name} 
  12.        4.  (Routing)

    13 fun Application.module(testing: Boolean = false) { routing { get("/") { call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain) } } get("/user/{name}") { val name = call.parameters["name"] ?: "" call.respondText("Hi! $name") } } http://localhost:8080/user/tora  Hi! tora Copyright © 2019 Toranoana Inc. All Rights Reserved.
  13.       14 Copyright © 2019

    Toranoana Inc. All Rights Reserved. 4.(Routing)
  14.        5.  (Locations)

    15 Copyright © 2019 Toranoana Inc. All Rights Reserved.    • /type/{name} • /type/{name}/edit • /type/{name}/list/{page} type   
  15.        5.  (Locations)

    16 fun Application.module(testing: Boolean = false) { install(Locations) { } routing { // Register nested routes get<Type> { type -> call.respondText("Inside $type") } get<Type.Edit> { edit -> call.respondText("Inside $edit") } get<Type.List> { list -> call.respondText("Inside $list") } } } @Location("/type/{name}") data class Type(val name: String) { @Location("/edit") data class Edit(val type: Type) @Location("/list/{page}") data class List(val type: Type, val page: Int) } http://localhost:8080/type/tora     Type Copyright © 2019 Toranoana Inc. All Rights Reserved.
  16.        17 fun Application.module(testing:

    Boolean = false) { install(Locations) { } routing { // Register nested routes get<Type> { type -> call.respondText("Inside $type") } get<Type.Edit> { edit -> call.respondText("Inside $edit") } get<Type.List> { list -> call.respondText("Inside $list") } } } @Location("/type/{name}") data class Type(val name: String) { @Location("/edit") data class Edit(val type: Type) @Location("/list/{page}") data class List(val type: Type, val page: Int) } http://localhost:8080/type/tora    Type Copyright © 2019 Toranoana Inc. All Rights Reserved. 5.  (Locations)
  17.        18 Type 

    nametora Copyright © 2019 Toranoana Inc. All Rights Reserved. 5.  (Locations)
  18.        19 fun Application.module(testing:

    Boolean = false) { install(Locations) { } routing { // Register nested routes get<Type> { type -> call.respondText("Inside $type") } get<Type.Edit> { edit -> call.respondText("Inside $edit") } get<Type.List> { list -> call.respondText("Inside $list") } } } @Location("/type/{name}") data class Type(val name: String) { @Location("/edit") data class Edit(val type: Type) @Location("/list/{page}") data class List(val type: Type, val page: Int) } NestLocation  /type/{name}/edit /type/{name}/list/{page} 5.   (Locations)
  19.        20 Edit 

    List  Copyright © 2019 Toranoana Inc. All Rights Reserved. 5.   (Locations)
  20. '# '# '# '# ' # '# 6.Basic*)(Authenticaton) 21 fun

    Application.module(testing: Boolean = false) { install(Authentication) { basic(name = "myauth1") { realm = "Ktor Server" validate { credentials -> if (credentials.name == credentials.password) { UserIdPrincipal(credentials.name) } else { null } } } } ( $) ($) routing { authenticate("myauth1") { get("/basic") { val principal = call.authentication.principal<UserIdPrincipal>() call.respondText("Hi ${principal?.name ?: ""}", contentType = ContentType.Text.Plain) } } } } *)! +myauth1 valiate *)!"%+ name password&( &   Copyright © 2019 Toranoana Inc. All Rights Reserved.
  21. !  !  !  !  ! 

    !  22 fun Application.module(testing: Boolean = false) { install(Authentication) { basic(name = "myauth1") { realm = "Ktor Server" validate { credentials -> if (credentials.name == credentials.password) { UserIdPrincipal(credentials.name) } else { null } } } } ( ) ( ) routing { authenticate("myauth1") { get("/basic") { val principal = call.authentication.principal<UserIdPrincipal>() call.respondText("Hi ${principal?.name ?: ""}", contentType = ContentType.Text.Plain) } } } } http://localhost:8080/basic  Basic#"  #"$ #"   Copyright © 2019 Toranoana Inc. All Rights Reserved. 6.Basic#"(Authenticaton)
  22.        23 Copyright ©

    2019 Toranoana Inc. All Rights Reserved. 6.Basic(Authenticaton)
  23. = : = : = : = : = :

    = : 7.  • Intellij Ktor#%( Ktor#' 6; • &)( 29A – ()! Routing – )%,8Locations "%$ .40 /5+7*> )%31Validation-< • @? Features82 24 Copyright © 2019 Toranoana Inc. All Rights Reserved.
  24.          

    25 Copyright © 2019 Toranoana Inc. All Rights Reserved.