Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Kotlin 在 Web 方面的应用

58 views

Published on

受邀至 GDG Shanghai 分享 Kotlin 在 Web 方面的應用,内容介绍如何通过 Ktor 实作 Website 及 API。

Published in: Internet
  • Be the first to comment

  • Be the first to like this

Kotlin 在 Web 方面的应用

  1. 1. Kotlin Web — (Shengyou Fan) GDG Shanghai Kotlin Everywhere 2019/11/02
  2. 2. — • Developer Advocate • Backend Developer • Open Source Community Organizer
  3. 3. Kotlin https://youtu.be/hg8oYJ8Ez8s • General-purpose • Static typing • OOP + FP • Developed by JetBrains • Open Source (Apache 2.0) https://kotlinlang.org/ Kotlin —
  4. 4. Kotlin — Browser Kotlin/JS Server Kotlin/JVM iOS Kotlin/Native Android Kotlin/JVM
  5. 5. Kotlin — Server Database Client Kotlin/JVM
  6. 6. Ktor — https://ktor.io/ • Web Framework • Asynchronous • Servers + Clients • Developed by JetBrains • Open Source (Apache 2.0)
  7. 7. — OpenJDK 8+ IntelliJ IDEA Ktor Plugin
  8. 8. 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/
  9. 9. IntelliJ IDEA — ToolBox App
  10. 10. — https://plugins.jetbrains.com
  11. 11. Ktor Plugin — Ktor Plugin Ktor
  12. 12. Routing — routing { }
  13. 13. HTTP Method — routing { get("/") { } }
  14. 14. Application Call — Server Database Client Kotlin/JVM
  15. 15. Call — routing { get("/") { call. } }
  16. 16. — routing { get("/") { call.respondText("Hello, world") } }
  17. 17. ContentType HttpStatusCode — routing { get("/") { call.respondText( "Hello, world”, ContentType.Text.Plain, HttpStatusCode.OK ) } }
  18. 18. Auto build & reload — // resources/application.conf ktor { deployment { port = 8080 port = ${?PORT} } application { modules = [ ... ] } }
  19. 19. Auto build & reload — // resources/application.conf ktor { deployment { port = 8080 port = ${?PORT} watch = [ ... ] } application { modules = [ ... ] } }
  20. 20. HTML — routing { get("/") { call.respondText( "...HTML...", ContentType.Text.HTML, HttpStatusCode.OK ) } }
  21. 21. Gradle — // build.gradle dependencies { //... implementation "io.ktor:ktor-html-builder:$ver" }
  22. 22. HTML DSL — get("/") { call.respondHtml { head { title { +"ToDo List" } } body { h1 { +"ToDo List" } p { +"a simple ToDo application" } } } }
  23. 23. 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>
  24. 24. 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 } } } } } }
  25. 25. JSON — routing { get("/") { call.respondText( "...JSON...", ContentType.Application.Json, HttpStatusCode.OK ) } }
  26. 26. Gradle — // build.gradle dependencies { //... implementation "io.ktor:ktor-jackson:$ver" }
  27. 27. Install Feature — install(ContentNegotiation) { jackson { } }
  28. 28. Ktor Feature — Client Ktor App HTTP Server Feature Feature Feature
  29. 29. 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) ) }
  30. 30. 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) ) }
  31. 31. — version: "2" services: database: image: mysql:5.7 ports: - "...:3306" environment: MYSQL_ROOT_PASSWORD: ... MYSQL_DATABASE: ...
  32. 32. Exposed DB MySQL Connector — // build.gradle dependencies { //... implementation "org.jetbrains.exposed:exposed:$ver" implementation "mysql:mysql-connector-java:$ver" }
  33. 33. Table — object Tasks: IntIdTable() { val title = varchar("title", 255) val completed = bool("completed") val createdAt = datetime("created_at") val updatedAt = datetime("updated_at") }
  34. 34. 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 }
  35. 35. — Database.connect( url = "...", driver = "com.mysql.jdbc.Driver", user = "...", password = "..." )
  36. 36. — 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() } } }
  37. 37. 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) } }
  38. 38. Page — get("/") { call.respondHtml { head { title { +"ToDo List" } } body { h1 { +"ToDo List" } p { +"a simple ToDo application" } ul { tasks.map { li { +it.title } } } } } }
  39. 39. API — get("/api/v1/tasks") { call.respond( mapOf("tasks" to tasks) ) }
  40. 40. 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) }
  41. 41. — • Kotlin • Ktor • • Application Call Request & Response • Routing • Exposed DB • • API
  42. 42. — Talking Kotlin http://talkingkotlin.com/ktor-with-ryan-harter/Ktor https://ktor.io/servers/index.html KotlinConf https://youtu.be/V4PS3IjIzlw
  43. 43. — https://github.com/shengyou/20191102-kotlin-for-web
  44. 44. ktor.guide Ktor —
  45. 45. Ktor (Shengyou) shengyou.fan@jetbrains.com — Kotlin Web

×