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
25
Tailwind CSSとAtomic Designで実現する効率的な Web 開発の事例
toranoana
1
700
Denoについて、同人誌記事を出しました+update
toranoana
0
240
【虎の穴ラボ Tech Talk #2】プロンプトエンジニアリング
toranoana
0
160
20241121_[TechTalk#2]虎の穴ラボでのLLMについて取り組み紹介
toranoana
0
180
社内チャットへRAG導入した話(Tech Talk #2)
toranoana
0
250
Deno Deploy で Web Cache API を 使えるようになったので試した知見
toranoana
1
760
【虎の穴ラボ Tech Talk】虎の穴ラボTech Talk説明資料
toranoana
0
520
虎の穴ラボ Tech Talk_CDKでFargate環境構築
toranoana
1
690
Other Decks in Technology
See All in Technology
承認ゲートは何を守っているのか? AIエージェントの自律購入を実測した結果
yama3133
1
100
EUDIWの枠組みを出発点に民間エコシステムの在り方を考える(OAuth/OIDC Numa (Immersion) Workshop 2026)
oidfj
PRO
0
390
[ホンマでっか SRE] あなたはなぜ SRE に?
_awache
0
550
紙 to デジタル
ichien178
0
480
tamachi.goを支える技術
rymiyamoto
0
120
Guerilla InnerSource in enterprises, during the AI hype
onenashev
PRO
0
130
「面白い!」を信じ抜け。激動の時代を貫く、オンリーワン・エンジニアの条件
kizawa2020
2
510
サイボウズ 開発本部採用ピッチ / Cybozu Engineer Recruit
cybozuinsideout
PRO
12
86k
アクセスキー流出時の対応で再認識した攻撃側と防御側の非対称性について
kazzpapa3
1
480
Claude Teamプランの コスト最適化を考える
rfdnxbro
2
850
PdMをやめて、 "プロダクトビルダー"という 働き方に変えました / PdM to Product Builder
shikichee
2
530
会計事務所と顧問先の契約関係をOIDC・OAuthで表現する
terara
0
550
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
640
How to build a perfect <img>
jonoalderson
1
5.9k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
680
Technical Leadership for Architectural Decision Making
baasie
3
550
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
850
Claude Code のすすめ
schroneko
67
230k
The SEO Collaboration Effect
kristinabergwall1
1
540
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
290
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
WCS-LA-2024
lcolladotor
0
810
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.