SlideShare a Scribd company logo
Kotlin Web
—
(Shengyou Fan)
GDG Shanghai Kotlin Everywhere
2019/11/02
—
• Developer Advocate
• Backend Developer
• Open Source Community Organizer
Kotlin https://youtu.be/hg8oYJ8Ez8s
• General-purpose
• Static typing
• OOP + FP
• Developed by
JetBrains
• Open Source
(Apache 2.0)
https://kotlinlang.org/
Kotlin
—
Kotlin
—
Browser
Kotlin/JS
Server
Kotlin/JVM
iOS
Kotlin/Native
Android
Kotlin/JVM
Kotlin
—
Server
Database
Client
Kotlin/JVM
Ktor
—
https://ktor.io/
• Web Framework
• Asynchronous
• Servers + Clients
• Developed by
JetBrains
• Open Source
(Apache 2.0)
—
OpenJDK 8+ IntelliJ IDEA Ktor Plugin
OpenJDK
—
$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
$ sdk install java
$ sdk list java
$ sdk current java
$ sdk use java <version>
$ sdk default java <version>
$ sdk upgrade java
https://sdkman.io/
IntelliJ IDEA
—
ToolBox App
—
https://plugins.jetbrains.com
Ktor Plugin
—
Ktor Plugin
Ktor
Routing
—
routing {
}
HTTP Method
—
routing {
get("/") {
}
}
Application Call
—
Server
Database
Client
Kotlin/JVM
Call
—
routing {
get("/") {
call.
}
}
—
routing {
get("/") {
call.respondText("Hello, world")
}
}
ContentType
HttpStatusCode
—
routing {
get("/") {
call.respondText(
"Hello, world”,
ContentType.Text.Plain,
HttpStatusCode.OK
)
}
}
Auto build & reload
—
// resources/application.conf
ktor {
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [ ... ]
}
}
Auto build & reload
—
// resources/application.conf
ktor {
deployment {
port = 8080
port = ${?PORT}
watch = [ ... ]
}
application {
modules = [ ... ]
}
}
HTML
—
routing {
get("/") {
call.respondText(
"...HTML...",
ContentType.Text.HTML,
HttpStatusCode.OK
)
}
}
Gradle
—
// build.gradle
dependencies {
//...
implementation "io.ktor:ktor-html-builder:$ver"
}
HTML DSL
—
get("/") {
call.respondHtml {
head {
title { +"ToDo List" }
}
body {
h1 { +"ToDo List" }
p { +"a simple ToDo application" }
}
}
}
HTML
—
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<title>ToDo List</title>
</head>
<body>
<h1>ToDo List</h1>
<p>a simple ToDo application</p>
</body>
</html>
HTML DSL
—
get("/") {
val tasks = mutableListOf<String>()
for (i in 0..9) {
tasks.add("Task $i")
}
call.respondHtml {
head {
title { +"ToDo List" }
}
body {
h1 { +"ToDo List" }
p { +"a simple ToDo application" }
ul {
tasks.map {
li { +it }
}
}
}
}
}
JSON
—
routing {
get("/") {
call.respondText(
"...JSON...",
ContentType.Application.Json,
HttpStatusCode.OK
)
}
}
Gradle
—
// build.gradle
dependencies {
//...
implementation "io.ktor:ktor-jackson:$ver"
}
Install Feature
—
install(ContentNegotiation) {
jackson {
}
}
Ktor Feature
—
Client
Ktor App
HTTP Server
Feature
Feature
Feature
JSON respond
—
data class TaskRespond (
val title: String,
val completed: Boolean = false
)
get("/api/v1/tasks") {
val task = TaskRespond("Task 1”)
call.respond(
mapOf("task" to task)
)
}
JSON respond
—
get("/api/v1/tasks") {
val tasks = mutableListOf<TaskRespond>()
for (i in 1..10) {
tasks.add(TaskRespond("Task $i"))
}
call.respond(
mapOf("tasks" to tasks)
)
}
—
version: "2"
services:
database:
image: mysql:5.7
ports:
- "...:3306"
environment:
MYSQL_ROOT_PASSWORD: ...
MYSQL_DATABASE: ...
Exposed DB
MySQL Connector
—
// build.gradle
dependencies {
//...
implementation "org.jetbrains.exposed:exposed:$ver"
implementation "mysql:mysql-connector-java:$ver"
}
Table
—
object Tasks: IntIdTable() {
val title = varchar("title", 255)
val completed = bool("completed")
val createdAt = datetime("created_at")
val updatedAt = datetime("updated_at")
}
Entity
—
class Task(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Task>(Tasks)
var title by Tasks.title
var completed by Tasks.completed
var createdAt by Tasks.createdAt
var updatedAt by Tasks.updatedAt
}
—
Database.connect(
url = "...",
driver = "com.mysql.jdbc.Driver",
user = "...",
password = "..."
)
—
transaction {
SchemaUtils.drop(Tasks)
SchemaUtils.create(Tasks)
}
transaction {
for (i in 1..10) {
Task.new {
title = "Task $i"
completed = listOf(true, false, false)
.shuffled()
.first()
createdAt = DateTime.now()
updatedAt = DateTime.now()
}
}
}
Page & API
—
Database.connect(
url = "...",
driver = "com.mysql.jdbc.Driver",
user = "...",
password = "..."
)
val tasks = transaction {
Task.all().sortedByDescending { it.id }.map {
TaskRespond(it.title, it.completed)
}
}
Page
—
get("/") {
call.respondHtml {
head {
title { +"ToDo List" }
}
body {
h1 { +"ToDo List" }
p { +"a simple ToDo application" }
ul {
tasks.map {
li { +it.title }
}
}
}
}
}
API
—
get("/api/v1/tasks") {
call.respond(
mapOf("tasks" to tasks)
)
}
CORS
—
install(CORS) {
host("...")
header(HttpHeaders.AccessControlAllowOrigin)
header(HttpHeaders.AccessControlAllowHeaders)
header(HttpHeaders.ContentType)
method(HttpMethod.Options)
method(HttpMethod.Post)
method(HttpMethod.Put)
method(HttpMethod.Patch)
method(HttpMethod.Delete)
}
—
• Kotlin
• Ktor
•
• Application Call Request & Response
• Routing
• Exposed DB
•
• API
—
Talking Kotlin http://talkingkotlin.com/ktor-with-ryan-harter/Ktor https://ktor.io/servers/index.html KotlinConf https://youtu.be/V4PS3IjIzlw
—
https://github.com/shengyou/20191102-kotlin-for-web
ktor.guide
Ktor
—
Ktor
(Shengyou)
shengyou.fan@jetbrains.com
—
Kotlin Web

More Related Content

PDF
以 Ktor 快速打造 Web 應用
PDF
Kotlin 讀書會 #1
PDF
Kotlin for API (with Ktor)
PDF
通过 Ktor 迅速打造以 Kotlin 为核心的后端服务应用
PDF
Kotlin for Web (with Ktor)
PDF
運用 Exposed 管理及操作資料庫
PDF
Ktor 101 (以 Ktor 實作 Website 範例)
PDF
[HKOSCon 2020] Build an api service using ktor rapidly
以 Ktor 快速打造 Web 應用
Kotlin 讀書會 #1
Kotlin for API (with Ktor)
通过 Ktor 迅速打造以 Kotlin 为核心的后端服务应用
Kotlin for Web (with Ktor)
運用 Exposed 管理及操作資料庫
Ktor 101 (以 Ktor 實作 Website 範例)
[HKOSCon 2020] Build an api service using ktor rapidly

What's hot (19)

PDF
Kotlin 一條龍 - 打造全平台應用
PDF
用 Kotlin 做自動化工具
PDF
以 Kotlin 快速打造 Mobile Backend
PDF
Composer 經典食譜
PDF
Kotlin 讀書會第三梯次第一章
PDF
以 Laravel 經驗開發 Hyperf 應用
PDF
Ktor 部署攻略 - 老派 Fat Jar 大法
PDF
[JCConf 2020] 用 Kotlin 跨入 Serverless 世代
PDF
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API
PDF
Kotlin 1.2: Sharing code between platforms
PDF
用 OPENRNDR 將 Chatbot 訊息視覺化
PDF
Scaling up development of a modular code base
PDF
[Kotlin Serverless 工作坊] 單元 4 - 實作 RSS Aggregator
PDF
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
PDF
Geb presentation
PDF
Scaling up development of a modular code base
PPTX
Cloud browser testing with Gradle and Geb
PDF
Automation and Ansible
PDF
How to build and distribute CLI tool in 15 minutes with Golang
Kotlin 一條龍 - 打造全平台應用
用 Kotlin 做自動化工具
以 Kotlin 快速打造 Mobile Backend
Composer 經典食譜
Kotlin 讀書會第三梯次第一章
以 Laravel 經驗開發 Hyperf 應用
Ktor 部署攻略 - 老派 Fat Jar 大法
[JCConf 2020] 用 Kotlin 跨入 Serverless 世代
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API
Kotlin 1.2: Sharing code between platforms
用 OPENRNDR 將 Chatbot 訊息視覺化
Scaling up development of a modular code base
[Kotlin Serverless 工作坊] 單元 4 - 實作 RSS Aggregator
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
Geb presentation
Scaling up development of a modular code base
Cloud browser testing with Gradle and Geb
Automation and Ansible
How to build and distribute CLI tool in 15 minutes with Golang
Ad

Similar to Kotlin 在 Web 方面的应用 (20)

PDF
Rapid Web API development with Kotlin and Ktor
PDF
Flask and Angular: An approach to build robust platforms
PPTX
Full stack development with node and NoSQL - All Things Open - October 2017
PPTX
Full Stack Development with Node.js and NoSQL
PPTX
Introduction to Vert.x
PDF
Automatically generating-json-from-java-objects-java-objects268
PDF
5.node js
PDF
Json generation
PPTX
Kotlin. One language to dominate them all.
KEY
CouchDB on Android
PPTX
Android kotlin google
PDF
Revolution or Evolution in Page Object
KEY
Building Dojo in the Cloud
PPTX
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
PDF
Scripting GeoServer
PDF
PPT
Play!ng with scala
PDF
Play vs Rails
PDF
Nordic APIs - Automatic Testing of (RESTful) API Documentation
PPTX
NodeJS
Rapid Web API development with Kotlin and Ktor
Flask and Angular: An approach to build robust platforms
Full stack development with node and NoSQL - All Things Open - October 2017
Full Stack Development with Node.js and NoSQL
Introduction to Vert.x
Automatically generating-json-from-java-objects-java-objects268
5.node js
Json generation
Kotlin. One language to dominate them all.
CouchDB on Android
Android kotlin google
Revolution or Evolution in Page Object
Building Dojo in the Cloud
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Scripting GeoServer
Play!ng with scala
Play vs Rails
Nordic APIs - Automatic Testing of (RESTful) API Documentation
NodeJS
Ad

More from Shengyou Fan (20)

PDF
[JCConf 2024] Kotlin/Wasm:為 Kotlin 多平台帶來更多可能性
PDF
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
PDF
[JCConf 2023] 從 Kotlin Multiplatform 到 Compose Multiplatform:在多平台間輕鬆共用業務邏輯與 U...
PDF
[Kotlin 讀書會第五梯次] 深入淺出 Kotlin 第一章導讀
PDF
[WebConf Taiwan 2023] 一份 Zend Engine 外帶!透過 Micro 讓一次打包、多處運行變得可能
PDF
How I make a podcast website using serverless technology in 2023
PDF
[Effective Kotlin 讀書會] 第八章 Efficient collection processing 導讀
PDF
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
PDF
[JCConf 2022] Compose for Desktop - 開發桌面軟體的新選擇
PDF
Using the Exposed SQL Framework to Manage Your Database
PDF
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
PDF
[COSCUP 2022] Kotlin Collection 遊樂園
PDF
初探 Kotlin Multiplatform
PDF
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率
PDF
[PHP 也有 Day #64] PHP 升級指南
PDF
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
PDF
老派浪漫:用 Kotlin 寫 Command Line 工具
PDF
[Kotlin Serverless 工作坊] 單元 2 - 簡介 Kotlin Serverless
PDF
[Kotlin Serverless 工作坊] 單元 1 - 開發環境建置
PDF
用 Kotlin 打造讀書會小幫手
[JCConf 2024] Kotlin/Wasm:為 Kotlin 多平台帶來更多可能性
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[JCConf 2023] 從 Kotlin Multiplatform 到 Compose Multiplatform:在多平台間輕鬆共用業務邏輯與 U...
[Kotlin 讀書會第五梯次] 深入淺出 Kotlin 第一章導讀
[WebConf Taiwan 2023] 一份 Zend Engine 外帶!透過 Micro 讓一次打包、多處運行變得可能
How I make a podcast website using serverless technology in 2023
[Effective Kotlin 讀書會] 第八章 Efficient collection processing 導讀
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
[JCConf 2022] Compose for Desktop - 開發桌面軟體的新選擇
Using the Exposed SQL Framework to Manage Your Database
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
[COSCUP 2022] Kotlin Collection 遊樂園
初探 Kotlin Multiplatform
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率
[PHP 也有 Day #64] PHP 升級指南
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
老派浪漫:用 Kotlin 寫 Command Line 工具
[Kotlin Serverless 工作坊] 單元 2 - 簡介 Kotlin Serverless
[Kotlin Serverless 工作坊] 單元 1 - 開發環境建置
用 Kotlin 打造讀書會小幫手

Recently uploaded (20)

PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PPTX
innovation process that make everything different.pptx
PPTX
international classification of diseases ICD-10 review PPT.pptx
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PPTX
newyork.pptxirantrafgshenepalchinachinane
PPTX
artificialintelligenceai1-copy-210604123353.pptx
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
PPTX
artificial intelligence overview of it and more
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
DOCX
Unit-3 cyber security network security of internet system
PPT
Ethics in Information System - Management Information System
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPTX
Funds Management Learning Material for Beg
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
introduction about ICD -10 & ICD-11 ppt.pptx
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
innovation process that make everything different.pptx
international classification of diseases ICD-10 review PPT.pptx
Slides PDF The World Game (s) Eco Economic Epochs.pdf
An introduction to the IFRS (ISSB) Stndards.pdf
newyork.pptxirantrafgshenepalchinachinane
artificialintelligenceai1-copy-210604123353.pptx
Introuction about WHO-FIC in ICD-10.pptx
Power Point - Lesson 3_2.pptx grad school presentation
artificial intelligence overview of it and more
SASE Traffic Flow - ZTNA Connector-1.pdf
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
Unit-3 cyber security network security of internet system
Ethics in Information System - Management Information System
Job_Card_System_Styled_lorem_ipsum_.pptx
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Paper PDF World Game (s) Great Redesign.pdf
Funds Management Learning Material for Beg

Kotlin 在 Web 方面的应用