Skip to content

Instantly share code, notes, and snippets.

@itang
Created January 29, 2016 09:45
Show Gist options
  • Save itang/e91f5d4302f0c3d7706f to your computer and use it in GitHub Desktop.
Save itang/e91f5d4302f0c3d7706f to your computer and use it in GitHub Desktop.
Test Vertx Web
package demo
import io.vertx.core.Vertx
import io.vertx.core.http.HttpServer
import io.vertx.core.http.HttpServerResponse
import io.vertx.ext.web.Route
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext
fun Router.get(path: String, handler: (RoutingContext) -> Unit): Route {
return this.get(path).handler { ctx -> handler(ctx) }
}
fun Router.all(path: String, handler: (RoutingContext) -> Unit): Route {
return route(path).handler { ctx -> handler(ctx) }
}
fun Router.all(handler: (RoutingContext) -> Unit): Route {
return route().handler { ctx -> handler(ctx) }
}
fun Vertx.router(): Router {
return Router.router(this)
}
fun main(args: Array<String>) {
Vertx.vertx().run {
val router = router().apply {
//hello
get("/hello") { ctx ->
ctx.response().end("Hello")
}
//default
all { ctx ->
val resp = ctx.response()
resp.putHeader("Content-Type", "text/plain")
resp.end("Index?")
}
}
createHttpServer().requestHandler { req -> router.accept(req) }.listen(8080)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment