add webserver, etc...

- add ktor webserver
- package is subpackaged.
This commit is contained in:
dalbodeule
2024-07-30 14:28:43 +09:00
parent 8f8d2f895a
commit 72f98b024b
38 changed files with 409 additions and 148 deletions

View File

@@ -0,0 +1,44 @@
package space.mori.chzzk_bot.webserver
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.swagger.*
import io.ktor.server.routing.*
import io.ktor.server.websocket.*
import kotlinx.serialization.json.Json
import space.mori.chzzk_bot.webserver.routes.apiRoutes
val server = embeddedServer(Netty, port = 8080) {
install(WebSockets)
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = true
})
}
install(CORS) {
anyHost()
allowHeader(HttpHeaders.ContentType)
}
routing {
apiRoutes()
swaggerUI("swagger-ui/index.html", "openapi/documentation.yaml") {
options {
version = "1.1.0"
}
}
}
}
fun start() {
server.start(wait = true)
}
fun stop() {
server.stop()
}

View File

@@ -0,0 +1,19 @@
package space.mori.chzzk_bot.webserver.routes
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
fun Routing.apiRoutes() {
route("/") {
get {
call.respondText("Hello World!", status = HttpStatusCode.OK)
}
}
route("/health") {
get {
call.respondText("OK", status= HttpStatusCode.OK)
}
}
}

View File

@@ -0,0 +1,35 @@
openapi: "3.1.0"
info:
title: "chzzk_bot API"
description: "chzzk_bot API"
version: "1.0.0"
servers:
- url: "http://localhost:8080"
paths:
/:
get:
summary: "Webroot"
description: "Main page of this api"
responses:
"200":
description: "OK"
content:
text/plain:
schema:
type: "string"
examples:
Example#1:
value: "Hello World!"
/health:
get:
description: "Health Check endpoint"
responses:
"200":
description: "OK"
content:
text/plain:
schema:
type: "string"
examples:
Example#1:
value: "OK"