├── settings.gradle ├── src └── main │ ├── resources │ └── logback.xml │ └── kotlin │ └── com │ └── easy │ └── kotlin │ └── BlogApp.kt └── README.md /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'ktor_demo' 2 | 3 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/kotlin/com/easy/kotlin/BlogApp.kt: -------------------------------------------------------------------------------- 1 | package com.easy.kotlin 2 | 3 | 4 | import org.jetbrains.ktor.application.Application 5 | import org.jetbrains.ktor.application.install 6 | import org.jetbrains.ktor.features.CallLogging 7 | import org.jetbrains.ktor.features.DefaultHeaders 8 | import org.jetbrains.ktor.host.embeddedServer 9 | import org.jetbrains.ktor.http.ContentType 10 | import org.jetbrains.ktor.netty.Netty 11 | import org.jetbrains.ktor.response.respondText 12 | import org.jetbrains.ktor.routing.Routing 13 | import org.jetbrains.ktor.routing.get 14 | import java.util.* 15 | 16 | fun Application.module() { 17 | install(DefaultHeaders) 18 | install(CallLogging) 19 | install(Routing) { 20 | get("/") { 21 | var html = "
  • hello
  • " 22 | html += "
  • now
  • " 23 | call.respondText(html, ContentType.Text.Html) 24 | } 25 | 26 | get("/hello") { 27 | call.respondText("Hello, Ktor !", ContentType.Text.Html) 28 | } 29 | 30 | get("/now") { 31 | call.respondText("Now time is : ${Date()}", ContentType.Text.Html) 32 | } 33 | } 34 | } 35 | 36 | fun main(args: Array) { 37 | embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start() 38 | } 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ktor: Kotlin Web后端框架 Web backend framework for Kotlin 快速开始入门 2 | === 3 | 4 | # Ktor 简介 5 | 6 | Ktor 是一个用于在 Kotlin 中快速创建 web 应用程序的框架。 7 | 8 | ```kotlin 9 | import org.jetbrains.ktor.netty.* 10 | import org.jetbrains.ktor.routing.* 11 | import org.jetbrains.ktor.application.* 12 | import org.jetbrains.ktor.host.* 13 | import org.jetbrains.ktor.http.* 14 | import org.jetbrains.ktor.response.* 15 | 16 | fun main(args: Array) { 17 | embeddedServer(Netty, 8080) { 18 | routing { 19 | get("/") { 20 | call.respondText("Hello, world!", ContentType.Text.Html) 21 | } 22 | } 23 | }.start(wait = true) 24 | } 25 | 26 | ``` 27 | 28 | 29 | # 创建工程 30 | 31 | 首先使用 IDEA 创建标准 Gradle+Kotlin 工程。 32 | 33 | 然后分别添加: 34 | 35 | BlogApp.kt 36 | logback.xml 37 | 38 | 目录结构如下: 39 | 40 | ``` 41 | $ tree 42 | . 43 | ├── build.gradle 44 | ├── settings.gradle 45 | └── src 46 | ├── main 47 | │   ├── java 48 | │   ├── kotlin 49 | │   │   └── com 50 | │   │   └── easy 51 | │   │   └── kotlin 52 | │   │   └── BlogApp.kt 53 | │   └── resources 54 | │   └── logback.xml 55 | └── test 56 | ├── java 57 | ├── kotlin 58 | └── resources 59 | 60 | 12 directories, 4 files 61 | 62 | ``` 63 | 64 | # 在build.gradle中配置 Ktor 依赖 65 | 66 | ```groovy 67 | group 'com.easy.kotlin' 68 | version '1.0-SNAPSHOT' 69 | 70 | buildscript { 71 | ext.kotlin_version = '1.1.3-2' 72 | ext.ktor_version = '0.4.0' // ktor 版本 73 | 74 | repositories { 75 | mavenCentral() 76 | } 77 | dependencies { 78 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 79 | } 80 | } 81 | 82 | apply plugin: 'java' 83 | apply plugin: 'kotlin' 84 | apply plugin: 'application' // application plugin 85 | 86 | mainClassName = 'com.easy.kotlin.BlogAppKt' // main class 87 | 88 | sourceCompatibility = 1.8 89 | 90 | repositories { 91 | mavenCentral() 92 | maven { url "http://dl.bintray.com/kotlin/ktor" } 93 | maven { url "https://dl.bintray.com/kotlin/kotlinx" } 94 | } 95 | 96 | dependencies { 97 | ... 98 | compile "org.jetbrains.ktor:ktor-core:$ktor_version" 99 | compile "org.jetbrains.ktor:ktor-netty:$ktor_version" 100 | 101 | compile "ch.qos.logback:logback-classic:1.2.1" 102 | } 103 | 104 | 105 | kotlin { 106 | experimental { 107 | coroutines "enable" 108 | } 109 | } 110 | 111 | ``` 112 | 113 | 114 | 其中, 115 | 116 | compile "org.jetbrains.ktor:ktor-core:$ktor_version" 117 | compile "org.jetbrains.ktor:ktor-netty:$ktor_version" 118 | 119 | 120 | 是 ktor 核心依赖。 121 | 122 | 123 | # BlogApp.kt 124 | 125 | ```kotlin 126 | package com.easy.kotlin 127 | 128 | 129 | import org.jetbrains.ktor.application.Application 130 | import org.jetbrains.ktor.application.install 131 | import org.jetbrains.ktor.features.CallLogging 132 | import org.jetbrains.ktor.features.DefaultHeaders 133 | import org.jetbrains.ktor.host.embeddedServer 134 | import org.jetbrains.ktor.http.ContentType 135 | import org.jetbrains.ktor.netty.Netty 136 | import org.jetbrains.ktor.response.respondText 137 | import org.jetbrains.ktor.routing.Routing 138 | import org.jetbrains.ktor.routing.get 139 | import java.util.* 140 | 141 | fun Application.module() { 142 | install(DefaultHeaders) 143 | install(CallLogging) 144 | install(Routing) { 145 | get("/") { 146 | var html = "
  • hello
  • " 147 | html += "
  • now
  • " 148 | call.respondText(html, ContentType.Text.Html) 149 | } 150 | 151 | get("/hello") { 152 | call.respondText("Hello, Ktor !", ContentType.Text.Html) 153 | } 154 | 155 | get("/now") { 156 | call.respondText("Now time is : ${Date()}", ContentType.Text.Html) 157 | } 158 | } 159 | } 160 | 161 | fun main(args: Array) { 162 | embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start() 163 | } 164 | 165 | ``` 166 | 167 | # logback.xml 168 | 169 | ```xml 170 | 171 | 172 | 173 | %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | ``` 187 | 188 | # 运行结果 189 | 190 | 启动应用: 191 | 192 | 193 | ![螢幕快照 2017-09-05 22.42.29.png](http://upload-images.jianshu.io/upload_images/1233356-348139fd802d2e17.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 194 | 195 | 196 | 197 | 198 | 输出日志: 199 | 200 | ``` 201 | 22:40:44: Executing external task 'run'... 202 | :compileKotlin 203 | Using kotlin incremental compilation 204 | :compileJava NO-SOURCE 205 | :copyMainKotlinClasses 206 | :processResources 207 | :classes 208 | :run 209 | 2017-09-05 22:40:53.701 [main] DEBUG ktor.application - Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home 210 | 2017-09-05 22:40:53.712 [main] DEBUG ktor.application - Class Loader: sun.misc.Launcher$AppClassLoader@73d16e93: [/Users/jack/easykotlin/ktor_demo/build/classes/java/main/, /Users/jack/easykotlin/ktor_demo/build/resources/main/, /Users/jack/.gradle/caches/modules-2/files-2.1/org.jetbrains.ktor/ktor-netty/0.4.0/f8809d15d9b447b669e8514f14addcb3586dcd26/ktor-netty-0.4.0.jar, /Users/jack/.gradle/caches/modules-2/files-2.1/org.jetbrains.ktor/ktor-hosts-common/0.4.0/bec3be6cc48a989347a7d3048266aff412d16668/ktor-hosts-common-0.4.0.jar, /Users/jack/.gradle/caches/modules-2/files-2.1/org.jetbrains.ktor/ktor-core/0.4.0/be0937d74f19862e8087b08a3b2306de65aa6f12/ktor-core-0.4.0.jar, ... 211 | 2017-09-05 22:40:53.717 [main] INFO ktor.application - No ktor.deployment.watch patterns match classpath entries, automatic reload is not active 212 | 2017-09-05 22:40:54.364 [main] TRACE ktor.application - Application started: org.jetbrains.ktor.application.Application@f78a47e 213 | 214 | ``` 215 | 216 | 217 | 浏览器访问: http://127.0.0.1:8080/ 218 | 219 | ![螢幕快照 2017-09-05 22.43.19.png](http://upload-images.jianshu.io/upload_images/1233356-5618de0401aa8572.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 220 | 221 | 222 | ![螢幕快照 2017-09-05 22.43.26.png](http://upload-images.jianshu.io/upload_images/1233356-c109b28a6fea01cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 223 | 224 | 225 | 226 | ![螢幕快照 2017-09-05 22.43.34.png](http://upload-images.jianshu.io/upload_images/1233356-ba4a3c93a8160614.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 227 | 228 | 229 | 230 | 231 | # 完整工程示例代码 232 | 233 | https://github.com/EasyKotlin/ktor_demo 234 | 235 | 236 | 237 | 238 | # 参考资料: 239 | 240 |  [http://ktor.io](http://ktor.io/) 241 | 242 | 243 | 244 | ---------------------------- 245 | # 《Kotlin极简教程》 正式上架: 246 | 247 | Official on shelves: Kotlin Programming minimalist tutorial 248 | 249 | > 京东JD:https://item.jd.com/12181725.html 250 | 251 | > 天猫Tmall:https://detail.tmall.com/item.htm?id=558540170670 252 | --------------------------------------------------------------------------------