YeetBot/src/main/kotlin/nl/voidcorp/yeetbot/HTTP.kt

41 lines
1.1 KiB
Kotlin

package nl.voidcorp.yeetbot
import freemarker.cache.FileTemplateLoader
import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.freemarker.FreeMarker
import io.ktor.freemarker.FreeMarkerContent
import io.ktor.http.ContentType
import io.ktor.http.content.files
import io.ktor.http.content.static
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.server.engine.embeddedServer
import io.ktor.server.jetty.Jetty
import java.io.File
suspend fun ApplicationCall.respondJson(any: Any) = this.respondText(any.toJson(), contentType = ContentType.parse("application/json"))
fun server() {
val server = embeddedServer(Jetty, port = config.httpPort) {
install(FreeMarker) {
templateLoader = FileTemplateLoader(File("templates"))
}
routing {
static {
files("static")
}
get("/") {
call.respond(FreeMarkerContent("index.ftl", mapOf("title" to "memes!")))
}
}
}
server.start()
}