Skip to content

Instantly share code, notes, and snippets.

@mytrile
Forked from itang/TestVertxWeb.kt
Created November 19, 2016 20:36
Show Gist options
  • Save mytrile/72964f1a08f81d8586352f1097e962ab to your computer and use it in GitHub Desktop.
Save mytrile/72964f1a08f81d8586352f1097e962ab 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