Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
サーバサイドKotlinへの入門 Ktor編
Search
虎の穴ラボ株式会社
June 06, 2019
Technology
1.6k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
サーバサイドKotlinへの入門 Ktor編
虎の穴ラボ株式会社
June 06, 2019
More Decks by 虎の穴ラボ株式会社
See All by 虎の穴ラボ株式会社
Tailwind CSSとAtomic Designで実現する効率的な Web 開発の事例
toranoana
1
670
Denoについて、同人誌記事を出しました+update
toranoana
0
230
【虎の穴ラボ Tech Talk #2】プロンプトエンジニアリング
toranoana
0
160
20241121_[TechTalk#2]虎の穴ラボでのLLMについて取り組み紹介
toranoana
0
160
社内チャットへRAG導入した話(Tech Talk #2)
toranoana
0
230
Deno Deploy で Web Cache API を 使えるようになったので試した知見
toranoana
1
730
【虎の穴ラボ Tech Talk】虎の穴ラボTech Talk説明資料
toranoana
0
500
虎の穴ラボ Tech Talk_CDKでFargate環境構築
toranoana
1
560
虎の穴ラボスキルアップ支援制度の利用例
toranoana
0
11k
Other Decks in Technology
See All in Technology
From Prompt Engineering to Loop Engineering
shibuiwilliam
1
240
秘密度ラベル初心者が第1歩でつまづかないための「設計・運用」ポイント
seafay
PRO
1
490
Comment regagner la souveraineté de vos données tout en étant payé grâce à Nostr !
rlifchitz
0
200
2026-06-24_人とAIの責務分離に基づく開発プロセスの提案.pdf
takahiromatsui
0
190
Lightning近況報告
kozy4324
0
220
LayerX コーポレートエンジニアリング室におけるサプライチェーンセキュリティへの取り組み / Supply Chain Security at LayerX Corporate Engineering
yuyatakeyama
3
840
週末にループ・エンジニアリングの理解を深めるためのスライド
nagatsu
0
370
千葉での単身赴任からAWSをやり続け、千葉に戻ってきた話
yama3133
1
120
AI 不只幫你寫 Code: 當專案從 300 暴增到 1500, 我們如何撐住 DevOps
appleboy
0
250
Kiro Ambassador を目指す話
k_adachi_01
0
130
Zenoh on Zephyr on LiteX
takasehideki
2
110
Microsoft のサポートとフィードバック総まとめ
murachiakira
PRO
0
110
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Un-Boring Meetings
codingconduct
0
320
Heart Work Chapter 1 - Part 1
lfama
PRO
7
36k
Side Projects
sachag
455
43k
Automating Front-end Workflow
addyosmani
1370
210k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Abbi's Birthday
coloredviolet
3
8.2k
Evolving SEO for Evolving Search Engines
ryanjones
0
220
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
540
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
400
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
Transcript
Kotlin
Ktor 1 Copyright © 2019 Toranoana Inc. All Rights Reserved.
,' ,' ,' ,' , ' ,' +) %" #(
• 2019 3!& • *Java • .- /$ • – 0 – & 2 Copyright © 2019 Toranoana Inc. All Rights Reserved.
1.
Kotlin 2. 3. Ktor 4. (Routing) 5. (Locations) 6. Basic! (Authenticaton) 7. 3 Copyright © 2019 Toranoana Inc. All Rights Reserved.
% $ % $ % $ % $ % $
% $ 1.Kotlin # *" →SparkFramework!Java/Kotlin Java & Kotlin*"+ Java '() 4 Copyright © 2019 Toranoana Inc. All Rights Reserved.
. + . + . + . + . +
. + 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.
3.Ktor https://start.ktor.io/ 6
Copyright © 2019 Toranoana Inc. All Rights Reserved.
3.Ktor IntellijKtor
7 Copyright © 2019 Toranoana Inc. All Rights Reserved.
3.Ktor 9
Features Routing Locations Authentication Copyright © 2019 Toranoana Inc. All Rights Reserved.
+ ) + ) + ) + ) + )
+ ) 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.
3.Ktor 11 Copyright
© 2019 Toranoana Inc. All Rights Reserved.
4. (Routing)
12 Copyright © 2019 Toranoana Inc. All Rights Reserved. • /user/{name} {name}
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.
14 Copyright © 2019
Toranoana Inc. All Rights Reserved. 4.(Routing)
5. (Locations)
15 Copyright © 2019 Toranoana Inc. All Rights Reserved. • /type/{name} • /type/{name}/edit • /type/{name}/list/{page} type
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.
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)
18 Type
nametora Copyright © 2019 Toranoana Inc. All Rights Reserved. 5. (Locations)
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)
20 Edit
List Copyright © 2019 Toranoana Inc. All Rights Reserved. 5. (Locations)
'# '# '# '# ' # '# 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.
! ! ! ! !
! 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)
23 Copyright ©
2019 Toranoana Inc. All Rights Reserved. 6.Basic(Authenticaton)
= : = : = : = : = :
= : 7. • Intellij Ktor#%( Ktor#' 6; • &)( 29A – ()! Routing – )%,8Locations "%$ .40 /5+7*> )%31Validation-< • @? Features82 24 Copyright © 2019 Toranoana Inc. All Rights Reserved.
25 Copyright © 2019 Toranoana Inc. All Rights Reserved.