Created
January 29, 2016 09:45
-
-
Save itang/e91f5d4302f0c3d7706f to your computer and use it in GitHub Desktop.
Test Vertx Web
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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