Skip to content

Commit

Permalink
handled assets using daikon-core
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Coser committed Apr 14, 2020
1 parent 2a13254 commit df257bf
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {

compile 'org.eclipse.jetty:jetty-server:9.4.27.v20200227'
compile 'org.eclipse.jetty:jetty-servlet:9.4.27.v20200227'
compile 'com.github.DaikonWeb:daikon-core:1.3.0'
compile 'com.github.DaikonWeb:daikon-core:1.3.1'

testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/daikon/HttpResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class HttpResponse(private val response: HttpServletResponse) : Response {

override fun write(text: String) {
writer.write(text)
response.characterEncoding = UTF_8.name()
response.writer.write(text)
write(text.toByteArray(UTF_8))
}

override fun write(byteArray: ByteArray) {
response.outputStream.write(byteArray)
}
}
9 changes: 0 additions & 9 deletions src/main/kotlin/daikon/HttpServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package daikon
import daikon.core.DaikonServer
import daikon.core.RoutingHandler
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.DefaultServlet
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder
import org.eclipse.jetty.util.log.Log
import org.eclipse.jetty.util.resource.Resource
import java.time.LocalDateTime.now
import java.time.temporal.ChronoUnit.MILLIS

Expand Down Expand Up @@ -36,13 +34,6 @@ class HttpServer(private val port: Int = 4545, initializeActions: DaikonServer.(
server.stop()
}

fun assets(path: String): DaikonServer {
val servletHolder = ServletHolder(DefaultServlet())
handler.addServlet(servletHolder, path)
handler.baseResource = Resource.newResource(HttpServer::class.java.getResource("/assets/"))
return this
}

private fun disableJettyLog() {
Log.getProperties().setProperty("org.eclipse.jetty.util.log.announce", "false")
Log.getProperties().setProperty("org.eclipse.jetty.LEVEL", "OFF")
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/daikon/HttpContextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HttpContextTest {
fun `context preserve attributes`() {
HttpServer()
.afterStart { ctx -> ctx.addAttribute("key", "value") }
.get("/") { _, res, ctx -> res.write(ctx.getAttribute("key")) }
.get("/") { _, res, ctx -> res.write(ctx.getAttribute<String>("key")) }
.start().use {
assertThat(local("/").http.get().body).isEqualTo("value")
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/kotlin/daikon/HttpRoutingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import daikon.core.RouteAction
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import topinambur.http
import java.nio.file.Path

class HttpRoutingTest {

Expand Down Expand Up @@ -41,8 +42,8 @@ class HttpRoutingTest {
@Test
fun `serve static files`() {
HttpServer()
.assets("/foo/*")
.get("/bar/2") { _, res -> res.write("Hello") }
.assets("/foo/*")
.start().use {
assertThat(local("/foo/style.css").http.get().header("Content-Type")).isEqualTo("text/css")
assertThat(local("/foo/style.css").http.get().body).isEqualTo("body {}")
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/daikon/RequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class RequestTest {
fun attribute() {
HttpServer()
.before("/") { req, _ -> req.attribute("foo_key", "foo_value") }
.get("/") { req, res -> res.write(req.attribute("foo_key")) }
.get("/") { req, res -> res.write(req.attribute<String>("foo_key")) }
.start().use {
assertThat(local("/").http.get().body).isEqualTo("foo_value")
}
Expand Down
Binary file added src/test/resources/assets/foo/daikon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit df257bf

Please sign in to comment.