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.

以 Ktor 快速打造 Web 應用

152 views

Published on

受邀至 Kotlin Night Taipei 分享如何以 Ktor 這套 100% 用 Kotlin 打造的框架快速開發 Web 應用,包括網頁及 API 兩種方式,以及如何整合 Exposed SQL Library 做出 CRUD 各種功能。

Published in: Internet
  • Be the first to comment

  • Be the first to like this

以 Ktor 快速打造 Web 應用

  1. 1. Ktor Web — (Shengyou Fan) Kotlin Night Taipei 2019/12/13
  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 Web — 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. Ktor Application — Client Ktor App HTTP Server Feature Feature Feature
  8. 8. Routing — routing { }
  9. 9. HTTP Method — routing { get("/") { } }
  10. 10. Call — routing { get("/") { call. } }
  11. 11. Hello, world — routing { get("/") { call.respondText("Hello, world") } }
  12. 12. — routing { get("/") { call.respondText( "Hello, world", ContentType.Text.Plain, HttpStatusCode.OK ) } }
  13. 13. HTML respond — install(FreeMarker) { templateLoader = ClassTemplateLoader( this::class.java.classLoader, "templates" ) } get("/") { // ... call.respond( FreeMarkerContent( "*.ftl", mapOf("..." to ...) ) ) }
  14. 14. JSON respond — install(ContentNegotiation) { jackson { } } get("/api/v1/books") { val book = BookRespond("...") call.respond( mapOf("book" to book) ) } data class BookRespond ( val title: String, val isbn: String, )
  15. 15. Exposed — // gradle.properties exposed_version=0.17.7 mysql_connector_version=5.1.46 // build.gradle dependencies { //... implementation "org.jetbrains.exposed:exposed:$ver" implementation "mysql:mysql-connector-java:$ver" }
  16. 16. MySQL — version: "2" services: database: image: mysql:5.7 ports: - "...:3306" environment: MYSQL_ROOT_PASSWORD: ... MYSQL_DATABASE: ... command: ...
  17. 17. — object Books : Table() { val id: Column<Int> = integer("id") .autoIncrement() .primaryKey() val title: Column<String> = varchar("title", 255) val isbn: Column<String> = varchar("isbn", 13) val publishedAt: Column<DateTime> = datetime("...") } object Books : IntIdTable() { val title = varchar("title", 255) val isbn = varchar("isbn", 13) val publishedAt = datetime("...") }
  18. 18. DAO — class Book(id: EntityID<Int>) : IntEntity(id) { companion object : IntEntityClass<Book>(Books) var title by Books.title var isbn by Books.isbn var publishedAt by Books.publishedAt }
  19. 19. — Database.connect( url = "...", driver = "com.mysql.jdbc.Driver", user = "...", password = "..." )
  20. 20. — transaction { // ... SchemaUtils.drop(…) // ... SchemaUtils.create(...) }
  21. 21. // create Book.new { title = ... } // read Book.all() Book.find { ... } .sortedBy { it.id } // update val book = Book.findById(...) if (book != null) { book.title = ... } // delete val book = Book.findById(1) book?.delete() CRUD (DAO) —
  22. 22. — • Ktor • Application Call Request & Response • Routing • Template (HTML) • API (JSON) • Exposed DB
  23. 23. — Talking Kotlin http://talkingkotlin.com/ktor-with-ryan-harter/ FreeMarker https://freemarker.apache.org/docs/dgui_quickstart.htmlKtor https://ktor.io/servers/index.html KotlinConf https://youtu.be/V4PS3IjIzlw
  24. 24. ktor.guide Ktor —
  25. 25. Web (Shengyou) shengyou.fan@jetbrains.com Q&A — Ktor

×