サーバサイドKotlinへの入門 Ktor編
by
虎の穴ラボ株式会社
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
Kotlin Ktor 1 Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 2
Slide 2 text
,' ,' ,' ,' , ' ,' +) %" #( • 2019 3!& • *Java • .-/$ • – 0 – & 2 Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 3
Slide 3 text
1. Kotlin 2. 3. Ktor 4. (Routing) 5. (Locations) 6. Basic! (Authenticaton) 7. 3 Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 4
Slide 4 text
%$ %$ %$ %$ % $ %$ 1.Kotlin #*" →SparkFramework!Java/Kotlin Java & Kotlin*"+ Java '() 4 Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 5
Slide 5 text
.+ .+ .+ .+ . + .+ 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.
Slide 6
Slide 6 text
3.Ktor https://start.ktor.io/ 6 Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 7
Slide 7 text
3.Ktor IntellijKtor 7 Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 8
Slide 8 text
3.Ktor 9 Features Routing Locations Authentication Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 9
Slide 9 text
+ ) + ) + ) + ) + ) + ) 3.Ktor 10 fun main(args: Array) { 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.
Slide 10
Slide 10 text
3.Ktor 11 Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 11
Slide 11 text
4. (Routing) 12 Copyright © 2019 Toranoana Inc. All Rights Reserved. • /user/{name} {name}
Slide 12
Slide 12 text
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.
Slide 13
Slide 13 text
14 Copyright © 2019 Toranoana Inc. All Rights Reserved. 4.(Routing)
Slide 14
Slide 14 text
5. (Locations) 15 Copyright © 2019 Toranoana Inc. All Rights Reserved. • /type/{name} • /type/{name}/edit • /type/{name}/list/{page} type
Slide 15
Slide 15 text
5. (Locations) 16 fun Application.module(testing: Boolean = false) { install(Locations) { } routing { // Register nested routes get { type -> call.respondText("Inside $type") } get { edit -> call.respondText("Inside $edit") } get { 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.
Slide 16
Slide 16 text
17 fun Application.module(testing: Boolean = false) { install(Locations) { } routing { // Register nested routes get { type -> call.respondText("Inside $type") } get { edit -> call.respondText("Inside $edit") } get { 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)
Slide 17
Slide 17 text
18 Type nametora Copyright © 2019 Toranoana Inc. All Rights Reserved. 5. (Locations)
Slide 18
Slide 18 text
19 fun Application.module(testing: Boolean = false) { install(Locations) { } routing { // Register nested routes get { type -> call.respondText("Inside $type") } get { edit -> call.respondText("Inside $edit") } get { 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)
Slide 19
Slide 19 text
20 Edit List Copyright © 2019 Toranoana Inc. All Rights Reserved. 5. (Locations)
Slide 20
Slide 20 text
'# '# '# '# ' # '# 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() call.respondText("Hi ${principal?.name ?: ""}", contentType = ContentType.Text.Plain) } } } } *)! +myauth1 valiate *)!"%+ namepassword&( & Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 21
Slide 21 text
! ! ! ! ! ! 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() call.respondText("Hi ${principal?.name ?: ""}", contentType = ContentType.Text.Plain) } } } } http://localhost:8080/basic Basic#" #"$ #" Copyright © 2019 Toranoana Inc. All Rights Reserved. 6.Basic#"(Authenticaton)
Slide 22
Slide 22 text
23 Copyright © 2019 Toranoana Inc. All Rights Reserved. 6.Basic(Authenticaton)
Slide 23
Slide 23 text
=: =: =: =: = : =: 7. • IntellijKtor#%( Ktor#' 6; • &)( 29A – ()! Routing – )%,8Locations "%$.40/5+7*> )%31Validation-< • @?Features82 24 Copyright © 2019 Toranoana Inc. All Rights Reserved.
Slide 24
Slide 24 text
25 Copyright © 2019 Toranoana Inc. All Rights Reserved.