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 虎の穴ラボ株式会社
関西Ruby登壇資料
toranoana
0
13
Tailwind CSSとAtomic Designで実現する効率的な Web 開発の事例
toranoana
1
680
Denoについて、同人誌記事を出しました+update
toranoana
0
230
【虎の穴ラボ Tech Talk #2】プロンプトエンジニアリング
toranoana
0
160
20241121_[TechTalk#2]虎の穴ラボでのLLMについて取り組み紹介
toranoana
0
170
社内チャットへRAG導入した話(Tech Talk #2)
toranoana
0
240
Deno Deploy で Web Cache API を 使えるようになったので試した知見
toranoana
1
740
【虎の穴ラボ Tech Talk】虎の穴ラボTech Talk説明資料
toranoana
0
510
虎の穴ラボ Tech Talk_CDKでFargate環境構築
toranoana
1
570
Other Decks in Technology
See All in Technology
あなたの『Site』はどこですか? — xREという考え方
miyamu
0
1.2k
企業でAWS Organizationsを動かすための組織設計の考え方
nrinetcom
PRO
1
100
CIで使うClaude
iwatatomoya
0
280
型は壁、Rustでもバグを直すな、表現できなくせよ
nwiizo
14
2.1k
SRE本の知られざる名シーン / The Hidden Gems of Google SRE Book
nari_ex
1
410
SRE依存からの脱却 運用を開 発チームへ移す、 フルサイ クル開 発体制の実践
joooee0000
0
2.9k
Amazon EVS で VCF 9.0 / 9.1 のサポート開始まとめ
mtoyoda
0
300
しぶいSRE: サーバから見えない障害にどう向き合うか。ラストワンマイルのデバッグ実践 / Shibui SRE
kanny
13
6.3k
発表と総括 / Presentations and Summary
ks91
PRO
0
100
しくみを学んで使いこなそう GitHub Copilot app
torumakabe
2
280
Gen3R: 3D Scene Generation Meets Feed-Forward Reconstruction
spatial_ai_network
0
130
穢れた技術選定について
watany
17
5.2k
Featured
See All Featured
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
Chasing Engaging Ingredients in Design
codingconduct
0
240
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
180
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
460
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
340
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
WENDY [Excerpt]
tessaabrams
11
38k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
How to build a perfect <img>
jonoalderson
1
5.8k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
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.