|
@@ -7,10 +7,10 @@ import com.hexagonkt.core.media.TextMedia.PLAIN
|
|
|
import com.hexagonkt.http.model.ContentType
|
|
|
import com.hexagonkt.http.model.Header
|
|
|
import com.hexagonkt.http.model.Headers
|
|
|
+import com.hexagonkt.http.server.callbacks.DateCallback
|
|
|
import com.hexagonkt.http.server.handlers.HttpServerContext
|
|
|
import com.hexagonkt.http.server.handlers.PathHandler
|
|
|
import com.hexagonkt.http.server.handlers.path
|
|
|
-import com.hexagonkt.http.toHttpFormat
|
|
|
import com.hexagonkt.model.*
|
|
|
import com.hexagonkt.serialization.dsl.json.Json
|
|
|
import com.hexagonkt.serialization.serialize
|
|
@@ -18,7 +18,6 @@ import com.hexagonkt.store.BenchmarkStore
|
|
|
import com.hexagonkt.templates.TemplatePort
|
|
|
|
|
|
import java.net.URL
|
|
|
-import java.time.LocalDateTime.now
|
|
|
import java.util.concurrent.ThreadLocalRandom
|
|
|
|
|
|
import kotlin.text.Charsets.UTF_8
|
|
@@ -27,29 +26,26 @@ class Controller(
|
|
|
settings: Settings,
|
|
|
store: BenchmarkStore,
|
|
|
templateEngine: TemplatePort,
|
|
|
+ templateUrl: URL,
|
|
|
) {
|
|
|
private val queriesParam: String = settings.queriesParam
|
|
|
private val cachedQueriesParam: String = settings.cachedQueriesParam
|
|
|
private val worldRows: Int = settings.worldRows
|
|
|
+ private val textMessage: String = settings.textMessage
|
|
|
|
|
|
private val plain: ContentType = ContentType(PLAIN)
|
|
|
private val json: ContentType = ContentType(JSON)
|
|
|
private val html: ContentType = ContentType(HTML, charset = UTF_8)
|
|
|
|
|
|
- private val templateUrl: URL = URL("classpath:fortunes.pebble.html")
|
|
|
+ private val headers = Headers(Header("server", "Hexagon"))
|
|
|
|
|
|
- private val headers = Headers(
|
|
|
- Header("server", "Hexagon"),
|
|
|
- )
|
|
|
-
|
|
|
- internal val path: PathHandler by lazy {
|
|
|
+ val path: PathHandler by lazy {
|
|
|
path {
|
|
|
- on("*") {
|
|
|
- send(headers = headers + Header("date", now().toHttpFormat()))
|
|
|
- }
|
|
|
+ on("*") { send(headers = headers) }
|
|
|
+ on("*", DateCallback())
|
|
|
|
|
|
- get("/plaintext") { ok(settings.textMessage, contentType = plain) }
|
|
|
- get("/json") { ok(Message(settings.textMessage).toMap().serialize(Json.raw), contentType = json) }
|
|
|
+ get("/plaintext") { ok(textMessage, contentType = plain) }
|
|
|
+ get("/json") { ok(Message(textMessage).toJson(), contentType = json) }
|
|
|
get("/fortunes") { listFortunes(store, templateUrl, templateEngine) }
|
|
|
get("/db") { dbQuery(store) }
|
|
|
get("/query") { getWorlds(store) }
|
|
@@ -58,6 +54,9 @@ class Controller(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun Message.toJson(): String =
|
|
|
+ toMap().serialize(Json.raw)
|
|
|
+
|
|
|
private fun HttpServerContext.listFortunes(
|
|
|
store: BenchmarkStore, templateUrl: URL, templateAdapter: TemplatePort
|
|
|
): HttpServerContext {
|