diff --git a/src/main/kotlin/org/jetbrains/ktor/heroku/Main.kt b/src/main/kotlin/org/jetbrains/ktor/heroku/Main.kt index 972d3ad..277734a 100644 --- a/src/main/kotlin/org/jetbrains/ktor/heroku/Main.kt +++ b/src/main/kotlin/org/jetbrains/ktor/heroku/Main.kt @@ -7,6 +7,7 @@ import org.jetbrains.ktor.content.* import org.jetbrains.ktor.features.* import org.jetbrains.ktor.freemarker.* import org.jetbrains.ktor.host.* +import org.jetbrains.ktor.http.* import org.jetbrains.ktor.netty.* import org.jetbrains.ktor.routing.* import java.util.* @@ -20,48 +21,59 @@ val dataSource = if (hikariConfig.jdbcUrl != null) else HikariDataSource() +val html_utf8 = ContentType.Text.Html.withCharset(Charsets.UTF_8) fun Application.module() { install(DefaultHeaders) + install(ConditionalHeaders) + install(PartialContentSupport) + install(FreeMarker) { templateLoader = ClassTemplateLoader(environment.classLoader, "templates") } + install(StatusPages) { exception { exception -> - call.respond(FreeMarkerContent("error.ftl", exception, "")) + call.respond(FreeMarkerContent("error.ftl", exception, "", html_utf8)) } } - install(ConditionalHeaders) - install(PartialContentSupport) - routing { + + install(Routing) { serveClasspathResources("public") + get("hello") { call.respond("Hello World") } + + get("error") { + throw IllegalStateException("An invalid place to be …") + } + get("/") { val model = HashMap() model.put("message", "Hello World!") val etag = model.toString().hashCode().toString() - call.respond(FreeMarkerContent("index.ftl", model, etag)) + call.respond(FreeMarkerContent("index.ftl", model, etag, html_utf8)) } + get("/db") { val model = HashMap() dataSource.connection.use { connection -> - val stmt = connection.createStatement() - stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)") - stmt.executeUpdate("INSERT INTO ticks VALUES (now())") - val rs = stmt.executeQuery("SELECT tick FROM ticks") + val rs = connection.createStatement().run { + executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)") + executeUpdate("INSERT INTO ticks VALUES (now())") + executeQuery("SELECT tick FROM ticks") + } val output = ArrayList() while (rs.next()) { output.add("Read from DB: " + rs.getTimestamp("tick")) } - model.put("results", output) - - val etag = model.toString().hashCode().toString() - call.respond(FreeMarkerContent("db.ftl", model, etag)) } + + val etag = model.toString().hashCode().toString() + call.respond(FreeMarkerContent("db.ftl", model, etag, html_utf8)) } } } diff --git a/src/main/resources/templates/error.ftl b/src/main/resources/templates/error.ftl index 51ba174..9a18675 100644 --- a/src/main/resources/templates/error.ftl +++ b/src/main/resources/templates/error.ftl @@ -1,15 +1,17 @@ - <#include "header.ftl"> +<#include "header.ftl"> - <#include "nav.ftl"> +<#include "nav.ftl">
- ${message} +

An error occured

+ ${class} +

${message}