Skip to content

Commit

Permalink
Add context
Browse files Browse the repository at this point in the history
  • Loading branch information
fracassi-marco committed Dec 23, 2019
1 parent 7b46a44 commit f8b35a9
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repositories {
```
- Add the dependency
```
implementation 'com.github.DaikonWeb:daikon:0.6.1'
implementation 'com.github.DaikonWeb:daikon:0.6.2'
```

### Maven
Expand All @@ -40,7 +40,7 @@ implementation 'com.github.DaikonWeb:daikon:0.6.1'
<dependency>
<groupId>com.github.DaikonWeb</groupId>
<artifactId>daikon</artifactId>
<version>0.6.1</version>
<version>0.6.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'com.github.DaikonWeb'
version '0.6.1'
version '0.6.2'

repositories {
mavenCentral()
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/daikon/Context.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package daikon

interface Context {
fun addAttribute(key: String, value: Any)
fun <T> getAttribute(key: String) : T
}
2 changes: 1 addition & 1 deletion src/main/kotlin/daikon/DefaultRouteAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.eclipse.jetty.http.HttpStatus.NOT_FOUND_404
import org.eclipse.jetty.http.HttpStatus.OK_200

class DefaultRouteAction : RouteAction {
override fun handle(request: Request, response: Response) {
override fun handle(request: Request, response: Response, context: Context) {
if (request.method() == GET && request.path() == "/") {
response.status(OK_200)
response.type("text/html")
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/daikon/DummyRouteAction.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package daikon

class DummyRouteAction(private val action: (Request, Response) -> Unit) : RouteAction {
override fun handle(request: Request, response: Response) {
override fun handle(request: Request, response: Response, context: Context) {
action.invoke(request, response)
}
}

class ContextRouteAction(private val action: (Request, Response, Context) -> Unit) : RouteAction {
override fun handle(request: Request, response: Response, context: Context) {
action.invoke(request, response, context)
}
}
62 changes: 47 additions & 15 deletions src/main/kotlin/daikon/HttpServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
private val routes = Routing()
private val beforeActions = Routing()
private val afterActions = Routing()
private val afterStartActions = mutableListOf<() -> Unit>()
private val beforeStopActions = mutableListOf<() -> Unit>()
private val afterStartActions = mutableListOf<(Context) -> Unit>()
private val beforeStopActions = mutableListOf<(Context) -> Unit>()
private val basePath = mutableListOf("")
private val context = ServerContext()

init {
initializeActions()
Expand All @@ -30,17 +31,17 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
fun start(): HttpServer {
val beginStarting = now()
server = Server(port)
handler.addServlet(ServletHolder(RoutingServlet(beforeActions, routes, afterActions)), "/*")
handler.addServlet(ServletHolder(RoutingServlet(beforeActions, routes, afterActions, context)), "/*")
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() }
afterStartActions.forEach { it.invoke(context) }
return this
}

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

Expand All @@ -49,6 +50,11 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
return this
}

fun get(path: String, action: (Request, Response, Context) -> Unit): HttpServer {
get(path, ContextRouteAction(action))
return this
}

fun get(path: String, action: RouteAction): HttpServer {
add(GET, path, action)
return this
Expand All @@ -59,6 +65,11 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
return this
}

fun post(path: String, action: (Request, Response, Context) -> Unit): HttpServer {
post(path, ContextRouteAction(action))
return this
}

fun post(path: String, action: RouteAction): HttpServer {
add(POST, path, action)
return this
Expand All @@ -69,6 +80,11 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
return this
}

fun head(path: String, action: (Request, Response, Context) -> Unit): HttpServer {
head(path, ContextRouteAction(action))
return this
}

fun head(path: String, action: RouteAction): HttpServer {
add(HEAD, path, action)
return this
Expand All @@ -79,6 +95,11 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
return this
}

fun any(path: String, action: (Request, Response, Context) -> Unit): HttpServer {
any(path, ContextRouteAction(action))
return this
}

fun any(path: String, action: RouteAction): HttpServer {
add(ANY, path, action)
return this
Expand All @@ -89,11 +110,21 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
return this
}
fun before(path: String = "/*", action: (Request, Response, Context) -> Unit): HttpServer {
beforeActions.add(Route(ANY, joinPaths(path), ContextRouteAction(action)))
return this
}
fun after(path: String = "/*", action: (Request, Response) -> Unit): HttpServer {
afterActions.add(Route(ANY, joinPaths(path), DummyRouteAction(action)))
return this
}
fun after(path: String = "/*", action: (Request, Response, Context) -> Unit): HttpServer {
afterActions.add(Route(ANY, joinPaths(path), ContextRouteAction(action)))
return this
}
fun assets(path: String): HttpServer {
val servletHolder = ServletHolder(DefaultServlet())
handler.addServlet(servletHolder, path)
Expand All @@ -108,6 +139,16 @@ class HttpServer(private val port: Int = 4545, initializeActions: HttpServer.()
return this
}
fun afterStart(function: (Context) -> Unit): HttpServer {
afterStartActions.add(function)
return this
}
fun beforeStop(function: (Context) -> Unit): HttpServer {
beforeStopActions.add(function)
return this
}
private fun add(method: Method, path: String, action: RouteAction) {
routes.add(Route(method, joinPaths(path), action))
}
Expand All @@ -118,14 +159,5 @@ 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
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/daikon/RouteAction.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package daikon

interface RouteAction {
fun handle(request: Request, response: Response)
fun handle(request: Request, response: Response, context: Context)
}
7 changes: 3 additions & 4 deletions src/main/kotlin/daikon/RoutingServlet.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package daikon

import daikon.Method.ANY
import org.eclipse.jetty.http.HttpStatus.NOT_FOUND_404
import org.eclipse.jetty.http.HttpStatus.OK_200
import javax.servlet.GenericServlet
import javax.servlet.ServletRequest
import javax.servlet.ServletResponse
Expand All @@ -12,7 +10,8 @@ import javax.servlet.http.HttpServletResponse
class RoutingServlet(
private val befores: Routing,
private val routes: Routing,
private val afters: Routing
private val afters: Routing,
private val context: Context
) : GenericServlet() {

override fun service(req: ServletRequest, res: ServletResponse) {
Expand All @@ -37,6 +36,6 @@ class RoutingServlet(
}

private fun invoke(route: Route, req: HttpServletRequest, res: HttpResponse) {
route.action.handle(HttpRequest(req, PathParams(route.path)), res)
route.action.handle(HttpRequest(req, PathParams(route.path)), res, context)
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/daikon/ServerContext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package daikon

class ServerContext : Context {

private val attributes = mutableMapOf<String, Any>()

override fun addAttribute(key: String, value: Any) {
attributes[key] = value
}

override fun <T> getAttribute(key: String): T {
@Suppress("UNCHECKED_CAST")
return attributes[key] as T
}
}
5 changes: 3 additions & 2 deletions src/test/kotlin/daikon/DefaultRouteActionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class DefaultRouteActionTest {

private val request: Request = mock()
private val response: Response = mock()
private val context: Context = mock()

@Test
fun `page not found`() {
DefaultRouteAction().handle(request, response)
DefaultRouteAction().handle(request, response, context)

verify(response).status(NOT_FOUND_404)
}
Expand All @@ -26,7 +27,7 @@ class DefaultRouteActionTest {
whenever(request.method()).thenReturn(GET)
whenever(request.path()).thenReturn("/")

DefaultRouteAction().handle(request, response)
DefaultRouteAction().handle(request, response, context)

verify(response).status(OK_200)
verify(response).type("text/html")
Expand Down
18 changes: 18 additions & 0 deletions src/test/kotlin/daikon/HttpContextTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package daikon

import daikon.Localhost.get
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class HttpContextTest {

@Test
fun `context preserve attributes`() {
HttpServer()
.afterStart { ctx -> ctx.addAttribute("key", "value") }
.get("/") { _, res, ctx -> res.write(ctx.getAttribute("key")) }
.start().use {
assertThat(get("/").text).isEqualTo("value")
}
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/daikon/HttpRoutingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class HttpRoutingTest {
fun `route action`() {
HttpServer()
.get("/foo", object : RouteAction {
override fun handle(request: Request, response: Response) {
override fun handle(request: Request, response: Response, context: Context) {
response.write("Hello foo")
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/daikon/NopAction.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package daikon

class NopAction : RouteAction {
override fun handle(request: Request, response: Response) {
override fun handle(request: Request, response: Response, context: Context) {
}
}

0 comments on commit f8b35a9

Please sign in to comment.