SlideShare a Scribd company logo
Kotlin 

—
(Shengyou Fan)
JetBrains
2020/07/24
—
API
—
{
"data": [
{
"id": "...UUID...",
"title": "...",
"description": "...",
"completed": true
}
]
}
Ktor
—
https://ktor.io/
• Web Framework
• Asynchronous
• Servers + Clients
• Developed by
JetBrains
• Open Source
(Apache 2.0)
Ktor API
—
Server
Database
Client
Kotlin/JVM
—
https://plugins.jetbrains.com/plugin/10823-ktor
Exposed

Kotlin SQL Framework
—
https://github.com/JetBrains/Exposed
—
// gradle.properties
exposed_version=0.21.1
h2_version=1.4.200
// build.gradle
dependencies {
//...
// Exposed SQL Library
implementation "org.jetbrains.exposed:exposed-core:$ver"
implementation "org.jetbrains.exposed:exposed-dao:$ver"
implementation "org.jetbrains.exposed:exposed-jdbc:$ver"
implementation "org.jetbrains.exposed:exposed-jodatime:$ver"
// H2 database
implementation "com.h2database:h2:$ver"
}
DAO
—
object Tasks : UUIDTable() {
val title = varchar("title", 255)
val description = text("description")
var completed = bool("completed")
val createdAt = datetime("created_at")
val updatedAt = datetime("updated_at")
}
class Task(id: EntityID<UUID>) : UUIDEntity(id) {
companion object : UUIDEntityClass<Task>(Tasks)
var title by Tasks.title
var description by Tasks.description
var completed by Tasks.completed
var createdAt by Tasks.createdAt
var updatedAt by Tasks.updatedAt
}
—
data class TaskResponse (
val id:UUID,
val title: String,
val description: String,
val completed: Boolean
)
Ktor Feature
—
Client
Ktor App
HTTP Server
Feature
Feature
Feature
—
// build.gradle
dependencies {
//...
// GSON
implementation "io.ktor:ktor-gson:$ver"
}
Feature
—
install(ContentNegotiation) {
gson {
}
}
—
Database.connect(
url = "...",
driver = "..."
)
—
transaction {
SchemaUtils.create(Tasks)
}
transaction {
for (i in 1..10) {
val task = Task.new {
title = "Task $i"
description = "..."
completed = false
createdAt = DateTime.now()
updatedAt = DateTime.now()
}
}
}
Routing
—
routing {
}
HTTP Method
—
routing {
get("/api/tasks") {
}
}
val tasks = transaction {
Task.all().map {
TaskResponse(
it.id.value,
it.title,
it.description,
it.completed,
it.createdAt.toString("..."),
it.updatedAt.toString("...")
)
}
}
—
Data Class
—
get("/api/tasks") {
// ...
call.respond(mapOf("data" to tasks))
}
—
post("/api/tasks") {
val request = call.receive<TaskRequest>()
val data = transaction {
val task = Task.new {
title = request.title
description = request.description
completed = false
createdAt = DateTime.now()
updatedAt = DateTime.now()
}
// ...
}
call.respond(mapOf("data" to data))
}
—
patch("/api/tasks/{id}/complete") {
val id = UUID.fromString(call.parameters["id"])
val data = transaction {
val task = Task.findById(id)
if (task != null) {
task.completed = true
} else {
throw ModelNotFoundException()
}
}
call.respond(mapOf("data" to data))
}
—
delete("/api/tasks/{id}/delete") {
val id = UUID.fromString(call.parameters["id"])
transaction {
val task = Task.findById(id)
if (task != null) {
task.delete()
} else {
throw ModelNotFoundException()
}
}
call.respond(HttpStatusCode.NoContent)
}
—
// build.gradle
plugins {
id 'com.google.cloud.tools.jib' version '2.2.0'
}
$ gradle jib --image=<MY IMAGE>
https://github.com/GoogleContainerTools/jib
—
• Ktor
• Application Call Request & Response
• Routing
• API (JSON)
• Exposed DB
(Shengyou)
shengyou.fan@jetbrains.com
Q&A
—
Kotlin

More Related Content

PDF
以 Kotlin 快速打造 Mobile Backend
PDF
以 Ktor 快速打造 Web 應用
PDF
運用 Exposed 管理及操作資料庫
PDF
用 Kotlin 做自動化工具
PDF
[HKOSCon 2020] Build an api service using ktor rapidly
PDF
Kotlin 讀書會 #1
PDF
Ktor 部署攻略 - 老派 Fat Jar 大法
PDF
Composer 經典食譜
以 Kotlin 快速打造 Mobile Backend
以 Ktor 快速打造 Web 應用
運用 Exposed 管理及操作資料庫
用 Kotlin 做自動化工具
[HKOSCon 2020] Build an api service using ktor rapidly
Kotlin 讀書會 #1
Ktor 部署攻略 - 老派 Fat Jar 大法
Composer 經典食譜

What's hot (20)

PDF
Kotlin 在 Web 方面的应用
PDF
[JCConf 2020] 用 Kotlin 跨入 Serverless 世代
PDF
Kotlin for API (with Ktor)
PDF
[Kotlin Serverless 工作坊] 單元 4 - 實作 RSS Aggregator
PDF
Kotlin 讀書會第三梯次第一章
PDF
以 Laravel 經驗開發 Hyperf 應用
PDF
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API
PDF
Kotlin for Web (with Ktor)
PDF
用 OPENRNDR 將 Chatbot 訊息視覺化
PPTX
Keynote #Tech - Google : aperçu de la gestion des services distribués chez Go...
PDF
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
PPTX
Grails plugin
PPTX
Koop: Using 3rd Party Services in ArcGIS
PDF
Geb presentation
PDF
Droidcon Nigeria 2021 - Still Sleeping on KMM?
PDF
Git deep dive – chopping Kubernetes
PDF
Angular workflow with gulp.js
PDF
Flask and Angular: An approach to build robust platforms
PPTX
High Performance API Mashups with Node.js and ql.io
PDF
Declarative UIs with Jetpack Compose
Kotlin 在 Web 方面的应用
[JCConf 2020] 用 Kotlin 跨入 Serverless 世代
Kotlin for API (with Ktor)
[Kotlin Serverless 工作坊] 單元 4 - 實作 RSS Aggregator
Kotlin 讀書會第三梯次第一章
以 Laravel 經驗開發 Hyperf 應用
[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API
Kotlin for Web (with Ktor)
用 OPENRNDR 將 Chatbot 訊息視覺化
Keynote #Tech - Google : aperçu de la gestion des services distribués chez Go...
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
Grails plugin
Koop: Using 3rd Party Services in ArcGIS
Geb presentation
Droidcon Nigeria 2021 - Still Sleeping on KMM?
Git deep dive – chopping Kubernetes
Angular workflow with gulp.js
Flask and Angular: An approach to build robust platforms
High Performance API Mashups with Node.js and ql.io
Declarative UIs with Jetpack Compose
Ad

Similar to Kotlin 一條龍 - 打造全平台應用 (20)

PDF
Kotlin: Why Do You Care?
PPTX
Kotlin. One language to dominate them all.
PDF
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguaje
PDF
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
PDF
通过 Ktor 迅速打造以 Kotlin 为核心的后端服务应用
PDF
A quick and fast intro to Kotlin
PDF
Rapid Web API development with Kotlin and Ktor
PDF
Kotlin for Android Developers - 1
PDF
Spring ❤️ Kotlin #jjug
PPTX
從零開始學 Android
PPTX
Kotlin : Happy Development
PDF
Why Kotlin is your next language?
PDF
Ktor 101 (以 Ktor 實作 Website 範例)
PDF
Kotlin for Android Developers
PDF
Be More Productive with Kotlin
PDF
Little Helpers for Android Development with Kotlin
PDF
Building microservices with Kotlin
PDF
Kotlin Advanced - language reference for Android developers
PDF
Kotlin advanced - language reference for android developers
PDF
Kotlin, smarter development for the jvm
Kotlin: Why Do You Care?
Kotlin. One language to dominate them all.
Kotlin+MicroProfile: Enseñando trucos de 20 años a un nuevo lenguaje
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
通过 Ktor 迅速打造以 Kotlin 为核心的后端服务应用
A quick and fast intro to Kotlin
Rapid Web API development with Kotlin and Ktor
Kotlin for Android Developers - 1
Spring ❤️ Kotlin #jjug
從零開始學 Android
Kotlin : Happy Development
Why Kotlin is your next language?
Ktor 101 (以 Ktor 實作 Website 範例)
Kotlin for Android Developers
Be More Productive with Kotlin
Little Helpers for Android Development with Kotlin
Building microservices with Kotlin
Kotlin Advanced - language reference for Android developers
Kotlin advanced - language reference for android developers
Kotlin, smarter development for the jvm
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
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation theory and applications.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Machine Learning_overview_presentation.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
A Presentation on Artificial Intelligence
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Empathic Computing: Creating Shared Understanding
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Cloud computing and distributed systems.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
DOCX
The AUB Centre for AI in Media Proposal.docx
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation theory and applications.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Spectral efficient network and resource selection model in 5G networks
Machine Learning_overview_presentation.pptx
NewMind AI Weekly Chronicles - August'25-Week II
A Presentation on Artificial Intelligence
Review of recent advances in non-invasive hemoglobin estimation
Empathic Computing: Creating Shared Understanding
A comparative analysis of optical character recognition models for extracting...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Mobile App Security Testing_ A Comprehensive Guide.pdf
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine learning based COVID-19 study performance prediction
Cloud computing and distributed systems.
Diabetes mellitus diagnosis method based random forest with bat algorithm
The AUB Centre for AI in Media Proposal.docx

Kotlin 一條龍 - 打造全平台應用