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
A Friendly Introduction to Ktor Server
Search
Ilker Aslan
March 12, 2023
Programming
0
120
A Friendly Introduction to Ktor Server
Ilker Aslan
March 12, 2023
Tweet
Share
More Decks by Ilker Aslan
See All by Ilker Aslan
Yet Another KMP*
ilkeraslan
0
290
Other Decks in Programming
See All in Programming
エラーって何種類あるの?
kajitack
5
290
生成AIコーディングとの向き合い方、AIと共創するという考え方 / How to deal with generative AI coding and the concept of co-creating with AI
seike460
PRO
1
330
Team topologies and the microservice architecture: a synergistic relationship
cer
PRO
0
1k
生成AIで日々のエラー調査を進めたい
yuyaabo
0
640
Haskell でアルゴリズムを抽象化する / 関数型言語で競技プログラミング
naoya
17
4.9k
Gleamという選択肢
comamoca
6
760
VS Code Update for GitHub Copilot
74th
1
300
FormFlow - Build Stunning Multistep Forms
yceruto
1
190
PostgreSQLのRow Level SecurityをPHPのORMで扱う Eloquent vs Doctrine #phpcon #track2
77web
2
290
Blazing Fast UI Development with Compose Hot Reload (droidcon New York 2025)
zsmb
1
190
つよそうにふるまい、つよい成果を出すのなら、つよいのかもしれない
irof
1
300
20250628_非エンジニアがバイブコーディングしてみた
ponponmikankan
0
330
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
184
22k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
RailsConf 2023
tenderlove
30
1.1k
Code Reviewing Like a Champion
maltzj
524
40k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Docker and Python
trallard
44
3.4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
700
We Have a Design System, Now What?
morganepeng
53
7.7k
Transcript
@@export_scripts@@ A FRIENDLY INTRODUCTION TO KTOR SERVER
@@export_scripts@@ What is Ktor? Framework for micro services, web applications,
and HTTP services – Open Source, by JetBrains – Based on Kotlin Coroutines (asynchronous) –
@@export_scripts@@
@@export_scripts@@ Self Contained Package Application controls the engine settings, connection,
and SSL options – Servlet Servlet container controls the application lifecycle and connection settings –
@@export_scripts@@ Your First Server fun main() { embeddedServer( factory =
Netty, port = 8080, host = "0.0.0.0", module = Application::module ) .start(wait = true) }
@@export_scripts@@
@@export_scripts@@ Application Engine Environment Connectors that describe where and how
server should listen. – The running application – start/stop functions –
@@export_scripts@@ Application Engine Configuration Current parallelism level (e.g. the number
of available processors) – Threads used for new connections, processing connections, parsing messages –
@@export_scripts@@ Modules public actual val modules: MutableList<Application.() -> Unit> =
mutableListOf() Extension functions of an Application to structure the plugins
@@export_scripts@@ fun Application.module() { install(Resources) install(StatusPages) install(RequestValidation) configureRouting() configureSerialization() }
@@export_scripts@@ fun Application.configureRouting() { routing { indexRoute() beerRoutes() } }
@@export_scripts@@ fun Route.indexRoute() { route("/") { get { call.respondText( text
= "Welcome", status = HttpStatusCode.OK ) } } }
@@export_scripts@@
@@export_scripts@@ Routing fun Route.beerRoutes() { route("/beer") { get {...} get("{name?}")
{...} get("{id?}") {...} post {...} put("{id?}") {...} delete("{id?}") {...} } }
@@export_scripts@@ Type Safe Routing fun Route.beerRoutes() { get<resources.Beer> {...} get<resources.Beer.Id>
{...} post<resources.Beer> {...} put<resources.Beer.Id> {...} delete<resources.Beer.Id> {...} }
@@export_scripts@@ Resources @Resource("/beer") class Beer( val name: String? = null
) { @Resource("{id}") class Id(val parent: Beer = Beer(), val id: Long) }
@@export_scripts@@ Resources To define a route handler for a typed
resource, pass a resource class to a verb function (get, post, put...). – Serializable by default –
@@export_scripts@@ Build Links from Resources val link = href(Beer.Id(id =
1))
@@export_scripts@@ Get Beers curl --header "Content-Type: application/json" \ "http://0.0.0.0:8080/beer"
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@ Response Model @Serializable data class Response<T>( val data: T?
= null, val errors: List<Error>? = null )
@@export_scripts@@ Create a Beer curl --header "Content-Type: application/json" \ --data
"{"name": "Bock"}" \ "http://0.0.0.0:8080/beer"
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@ Delete a Beer curl --header \ "Content-Type: application/json" \
--request DELETE \ "http://0.0.0.0:8080/beer/532..."
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@ SSL and Certificates Ktor uses Java KeyStore (JKS) as
a storage facility for certificates. – Self-signed certificates for testing purposes by calling buildKeyStore() . –
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@ fun main() { ... embeddedServer( factory = Netty, environment
= environment ).start(wait = true) }
@@export_scripts@@ Send HTML in Response
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@ Respond with HTML Form
@@export_scripts@@
@@export_scripts@@ Templates To respond, call the respondHtmlTemplate() . – To
create a template, implement the Template interface. – Inside templates, use Placeholder or TemplatePlaceholder . –
@@export_scripts@@ Conclusions
@@export_scripts@@
@@export_scripts@@
@@export_scripts@@ The Repository
@@export_scripts@@ Where To Stalk Me https://androiddev.social/@ilker https://github.com/ilkeraslan https://www.polywork.com/ilkeraslan https://www.linkedin.com/in/aslanilker/ https://blog.ilker.it/