Skip to content

Commit

Permalink
Add an error route, fix UTF8 encoding and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
orangy committed Apr 7, 2017
1 parent a16f866 commit 85e4daa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
38 changes: 25 additions & 13 deletions src/main/kotlin/org/jetbrains/ktor/heroku/Main.kt
Expand Up @@ -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.*
Expand All @@ -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> { 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<String, Any>()
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<String, Any>()
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<String>()
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))
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/resources/templates/error.ftl
@@ -1,15 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<#include "header.ftl">
<#include "header.ftl">
</head>

<body>

<#include "nav.ftl">
<#include "nav.ftl">

<div class="container">
${message}
<h2>An error occured</h2>
<code>${class}</code>
<p>${message}</p>
</div>

</body>
Expand Down

0 comments on commit 85e4daa

Please sign in to comment.