All Projects → EasyKotlin → ktor_demo

EasyKotlin / ktor_demo

Licence: other
ktor demo

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to ktor demo

sinator
Sinatra application generator
Stars: ✭ 19 (-13.64%)
Mutual labels:  web-application
jak
Hybrid web/desktop applications on Linux
Stars: ✭ 79 (+259.09%)
Mutual labels:  web-application
Online-Food-Ordering-Web-App
Online Food Ordering System Website using basic PHP, SQL, HTML & CSS. You can use any one of XAMPP, WAMP or LAMP server to run the Web App
Stars: ✭ 96 (+336.36%)
Mutual labels:  web-application
mVoterWeb
Official mVoter Web Application
Stars: ✭ 38 (+72.73%)
Mutual labels:  web-application
hisite
Hisite Yii2 base project
Stars: ✭ 15 (-31.82%)
Mutual labels:  web-application
distro
START HERE! This is the Foswiki project "Distribution". It is a monolith repository with the core + default extensions.
Stars: ✭ 93 (+322.73%)
Mutual labels:  web-application
core
Core and Admin UI for Angular7+ web applications
Stars: ✭ 47 (+113.64%)
Mutual labels:  web-application
RocketXDelight-Playground
Native Android application built with Kotlin and Jetpack Compose. This project also illustrates the usage of advanced libraries such as Ktor, SqlDelight, Hilt, etc with the recommended practices and Unit Tests.
Stars: ✭ 37 (+68.18%)
Mutual labels:  ktor
antbs
Automated package build and repository management web application.
Stars: ✭ 23 (+4.55%)
Mutual labels:  web-application
FaceNet-based-Attendance-System
Deep Learning based Web Application for marking attendance of students by recognizing the student's faces from the surveillance video footage of classroom.
Stars: ✭ 25 (+13.64%)
Mutual labels:  web-application
Prometheus
🌈 A Clean And Modern Ghost Theme with Progressive Web Apps (PWA)
Stars: ✭ 94 (+327.27%)
Mutual labels:  web-application
Scrummage
The Ultimate OSINT and Threat Hunting Framework
Stars: ✭ 355 (+1513.64%)
Mutual labels:  web-application
manta-config-engine-app
💀 This was a web-application to generate autoexec configurations. Killed by Valve.
Stars: ✭ 17 (-22.73%)
Mutual labels:  web-application
cs-sakaryauniversity
Sakarya Üniversitesi'nde okuduğum süre boyunca karşıma çıkan tüm ödevler, ders notları ve çıkmış sınav soruları (All the assignments, lecture notes and exams)
Stars: ✭ 133 (+504.55%)
Mutual labels:  web-application
online-shopping
This is an online shopping project using Spring Boot,Spring web-flow, Spring Rest Services and Hibernate. In this project we also used Spring Security with java and annotation configuration
Stars: ✭ 34 (+54.55%)
Mutual labels:  web-application
mqtt-panel
Self hosted Web App panel for MQTT
Stars: ✭ 29 (+31.82%)
Mutual labels:  web-application
course-materials
Study material (slides, documents, etc) for the Web Applications I course (Politecnico di Torino, 2019/2020)
Stars: ✭ 23 (+4.55%)
Mutual labels:  web-application
kotlin-ktor-realworld-example-app
Real world backend API built in Kotlin with Ktor + Kodein + Exposed
Stars: ✭ 117 (+431.82%)
Mutual labels:  ktor
Snippet-Share
This is a snippet sharing app that can be used to share snippets of code and more.
Stars: ✭ 41 (+86.36%)
Mutual labels:  web-application
kmm
Rick & Morty Kotlin Multiplatform Mobile: Ktor, Sqldelight, Koin, Flow, MVI, SwiftUI, Compose
Stars: ✭ 52 (+136.36%)
Mutual labels:  ktor

Ktor: Kotlin Web后端框架 Web backend framework for Kotlin 快速开始入门

Ktor 简介

Ktor 是一个用于在 Kotlin 中快速创建 web 应用程序的框架。

import org.jetbrains.ktor.netty.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.host.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080) {
        routing {
            get("/") {
                call.respondText("Hello, world!", ContentType.Text.Html)
            }
        }
    }.start(wait = true)
}

创建工程

首先使用 IDEA 创建标准 Gradle+Kotlin 工程。

然后分别添加:

BlogApp.kt logback.xml

目录结构如下:

$ tree
.
├── build.gradle
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   ├── kotlin
    │   │   └── com
    │   │       └── easy
    │   │           └── kotlin
    │   │               └── BlogApp.kt
    │   └── resources
    │       └── logback.xml
    └── test
        ├── java
        ├── kotlin
        └── resources

12 directories, 4 files

在build.gradle中配置 Ktor 依赖

group 'com.easy.kotlin'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.1.3-2'
    ext.ktor_version = '0.4.0' // ktor 版本

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application' // application plugin

mainClassName = 'com.easy.kotlin.BlogAppKt'  // main class

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "http://dl.bintray.com/kotlin/ktor" }
    maven { url "https://dl.bintray.com/kotlin/kotlinx" }
}

dependencies {
    ...
    compile "org.jetbrains.ktor:ktor-core:$ktor_version"
    compile "org.jetbrains.ktor:ktor-netty:$ktor_version"

    compile "ch.qos.logback:logback-classic:1.2.1"
}


kotlin {
    experimental {
        coroutines "enable"
    }
}

其中,

compile "org.jetbrains.ktor:ktor-core:$ktor_version"
compile "org.jetbrains.ktor:ktor-netty:$ktor_version"

是 ktor 核心依赖。

BlogApp.kt

package com.easy.kotlin


import org.jetbrains.ktor.application.Application
import org.jetbrains.ktor.application.install
import org.jetbrains.ktor.features.CallLogging
import org.jetbrains.ktor.features.DefaultHeaders
import org.jetbrains.ktor.host.embeddedServer
import org.jetbrains.ktor.http.ContentType
import org.jetbrains.ktor.netty.Netty
import org.jetbrains.ktor.response.respondText
import org.jetbrains.ktor.routing.Routing
import org.jetbrains.ktor.routing.get
import java.util.*

fun Application.module() {
    install(DefaultHeaders)
    install(CallLogging)
    install(Routing) {
        get("/") {
            var html = "<li><a href = 'hello'>hello</a></li>"
            html += "<li><a href = 'now'>now</a></li>"
            call.respondText(html, ContentType.Text.Html)
        }

        get("/hello") {
            call.respondText("Hello, Ktor !", ContentType.Text.Html)
        }

        get("/now") {
            call.respondText("Now time is : ${Date()}", ContentType.Text.Html)
        }
    }
}

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()
}

logback.xml

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="trace">
        <appender-ref ref="STDOUT"/>
    </root>

    <logger name="org.eclipse.jetty" level="INFO"/>
    <logger name="io.netty" level="INFO"/>

</configuration>

运行结果

启动应用:

螢幕快照 2017-09-05 22.42.29.png

输出日志:

22:40:44: Executing external task 'run'...
:compileKotlin
Using kotlin incremental compilation
:compileJava NO-SOURCE
:copyMainKotlinClasses
:processResources
:classes
:run
2017-09-05 22:40:53.701 [main] DEBUG ktor.application - Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home
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, ...
2017-09-05 22:40:53.717 [main] INFO  ktor.application - No ktor.deployment.watch patterns match classpath entries, automatic reload is not active
2017-09-05 22:40:54.364 [main] TRACE ktor.application - Application started: org.jetbrains.ktor.application.Application@f78a47e

浏览器访问: http://127.0.0.1:8080/

螢幕快照 2017-09-05 22.43.19.png

螢幕快照 2017-09-05 22.43.26.png

螢幕快照 2017-09-05 22.43.34.png

完整工程示例代码

https://github.com/EasyKotlin/ktor_demo

参考资料:

 http://ktor.io


《Kotlin极简教程》 正式上架:

Official on shelves: Kotlin Programming minimalist tutorial

京东JD:https://item.jd.com/12181725.html

天猫Tmall:https://detail.tmall.com/item.htm?id=558540170670

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].