1.安装 IDEA Ktor 插件
This plugin supports the Ktor framework. Note: It doesn't work on Android Studio. The following features are available:
Ability to create preconfigured Ktor projects with optional Swagger/OpenAPI support.
Installing the plugin
You can get the plugin from:
- Plugin Website: https://plugins.jetbrains.com/plugin/10823-ktor
- Inside IntelliJ:
IntelliJ IDEA
→Preferences...
→Plugins
→Install JetBrains Plugin...
→Ktor
→Install
- IntelliJ Welcome Screen:
Configure
→Plugins
→Install JetBrains Plugin...
→Ktor
→Install
安装完毕,重启IDEA。
2.创建工程
Like this website, the plugin allows you to create a Ktor project, but with the additional convenience of being fully integrated in the IDE and automatically setting up the project.
Creating a new project a Ktor
section will appear:
- In a first step, you can configure the project to generate and select features to install:
- In a second step, you can configure the project artifacts: image
3. 样板代码
工程代码目录:
$ tree .
.
├── build.gradle
├── demo_01.iml
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── resources
│ ├── application.conf
│ └── logback.xml
├── settings.gradle
├── src
│ └── Application.kt
└── test
└── ApplicationTest.kt
5 directories, 12 files
Application.kt
package com.ktor.web
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.routing.*
import io.ktor.http.*
import io.ktor.html.*
import kotlinx.html.*
import kotlinx.css.*
import io.ktor.client.*
import io.ktor.client.engine.apache.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
val client = HttpClient(Apache) {
}
routing {
get("/") {
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
}
get("/html-dsl") {
call.respondHtml {
body {
h1 { +"HTML" }
ul {
for (n in 1..10) {
li { +"$n" }
}
}
}
}
}
get("/styles.css") {
call.respondCss {
body {
backgroundColor = Color.red
}
p {
fontSize = 2.em
}
rule("p.myclass") {
color = Color.blue
}
}
}
}
}
fun FlowOrMetaDataContent.styleCss(builder: CSSBuilder.() -> Unit) {
style(type = ContentType.Text.CSS.toString()) {
+CSSBuilder().apply(builder).toString()
}
}
fun CommonAttributeGroupFacade.style(builder: CSSBuilder.() -> Unit) {
this.style = CSSBuilder().apply(builder).toString().trim()
}
suspend inline fun ApplicationCall.respondCss(builder: CSSBuilder.() -> Unit) {
this.respondText(CSSBuilder().apply(builder).toString(), ContentType.Text.CSS)
}
项目依赖
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: 'application'
group 'example'
version '0.0.1'
mainClassName = "io.ktor.server.netty.EngineMain"
sourceSets {
main.kotlin.srcDirs = main.java.srcDirs = ['src']
test.kotlin.srcDirs = test.java.srcDirs = ['test']
main.resources.srcDirs = ['resources']
test.resources.srcDirs = ['testresources']
}
repositories {
mavenLocal()
jcenter()
maven { url 'https://kotlin.bintray.com/ktor' }
maven { url 'https://kotlin.bintray.com/kotlin-js-wrappers' }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "io.ktor:ktor-server-netty:$ktor_version"
compile "ch.qos.logback:logback-classic:$logback_version"
compile "io.ktor:ktor-server-core:$ktor_version"
compile "io.ktor:ktor-html-builder:$ktor_version"
compile "org.jetbrains:kotlin-css-jvm:1.0.0-pre.31-kotlin-1.2.41"
compile "io.ktor:ktor-client-core:$ktor_version"
compile "io.ktor:ktor-client-core-jvm:$ktor_version"
compile "io.ktor:ktor-client-apache:$ktor_version"
testCompile "io.ktor:ktor-server-tests:$ktor_version"
}
参考文档:
网友评论