Skip to content

Commit

Permalink
Add after start and before stop hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
fracassi-marco committed Dec 20, 2019
1 parent 570a99b commit 7b46a44
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/main/kotlin/daikon/HttpServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import java.time.temporal.ChronoUnit.MILLIS
class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.() -> Unit = {}) : AutoCloseable {

private val routes = Routing()
private val befores = Routing()
private val afters = Routing()
private val beforeActions = Routing()
private val afterActions = Routing()
private val afterStartActions = mutableListOf<() -> Unit>()
private val beforeStopActions = mutableListOf<() -> Unit>()
private val basePath = mutableListOf("")

init {
Expand All @@ -28,15 +30,17 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
fun start(): HttpServer {
val beginStarting = now()
server = Server(port)
handler.addServlet(ServletHolder(RoutingServlet(befores, routes, afters)), "/*")
handler.addServlet(ServletHolder(RoutingServlet(beforeActions, routes, afterActions)), "/*")
server.handler = handler
server.start()
val endStarting = now()
println("Server up and running on port $port in ${beginStarting.until(endStarting, MILLIS)}ms")
afterStartActions.forEach { it.invoke() }
return this
}

override fun close() {
beforeStopActions.forEach { it.invoke() }
server.stop()
}

Expand Down Expand Up @@ -81,12 +85,12 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
}

fun before(path: String = "/*", action: (Request, Response) -> Unit): HttpServer {
befores.add(Route(ANY, joinPaths(path), DummyRouteAction(action)))
beforeActions.add(Route(ANY, joinPaths(path), DummyRouteAction(action)))
return this
}
fun after(path: String = "/*", action: (Request, Response) -> Unit): HttpServer {
afters.add(Route(ANY, joinPaths(path), DummyRouteAction(action)))
afterActions.add(Route(ANY, joinPaths(path), DummyRouteAction(action)))
return this
}
Expand Down Expand Up @@ -114,4 +118,14 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
Log.getProperties().setProperty("org.eclipse.jetty.util.log.announce", "false")
Log.getProperties().setProperty("org.eclipse.jetty.LEVEL", "OFF")
}
fun afterStart(function: () -> Unit): HttpServer {
afterStartActions.add(function)
return this
}
fun beforeStop(function: () -> Unit): HttpServer {
beforeStopActions.add(function)
return this
}
}
30 changes: 30 additions & 0 deletions src/test/kotlin/daikon/HooksTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,34 @@ class HooksTest {
assertThat(response.text).isEqualTo("Hello Bob")
}
}

@Test
fun `do action after start`() {
var called = false
HttpServer()
.afterStart {
assertThat(get("/").text).isEqualTo("Hello")
called = true
}
.get("/") { _, res -> res.write("Hello")}
.start()
.close()

assertThat(called).isTrue()
}

@Test
fun `do action before stop`() {
var called = false
HttpServer()
.beforeStop {
assertThat(get("/").text).isEqualTo("Hello")
called = true
}
.get("/") { _, res -> res.write("Hello")}
.start()
.close()

assertThat(called).isTrue()
}
}

0 comments on commit 7b46a44

Please sign in to comment.