Skip to content

Instantly share code, notes, and snippets.

@miere
Created May 30, 2019 02:03
Show Gist options
  • Select an option

  • Save miere/02babe37c2d5f1325d0f860129b7fb2b to your computer and use it in GitHub Desktop.

Select an option

Save miere/02babe37c2d5f1325d0f860129b7fb2b to your computer and use it in GitHub Desktop.
A Verticle implementation that abstracts VertX Http Server creation, providing convenient methods to configure and set web routes.
import io.vertx.core.AbstractVerticle
import io.vertx.core.http.HttpMethod
import io.vertx.core.http.HttpServerOptions
import io.vertx.core.http.HttpServerRequest
import io.vertx.core.http.HttpServerResponse
import io.vertx.ext.web.Router
import io.vertx.ext.web.handler.BodyHandler
import java.util.concurrent.atomic.AtomicBoolean
/**
* A [io.vertx.core.Verticle] implementation that abstracts VertX Http Server
* creation, providing convenient methods to configure and set web routes.
*/
abstract class VertXWebServer: AbstractVerticle() {
val httpServerOptions = HttpServerOptions().apply {
port = 8080
host = "0.0.0.0"
}
private val server by lazy { vertx.createHttpServer()!! }
private val router by lazy { Router.router(vertx)!! }
private val methodsThatMayReadBody = mapOf(
HttpMethod.POST to AtomicBoolean(),
HttpMethod.PUT to AtomicBoolean(),
HttpMethod.PATCH to AtomicBoolean(),
HttpMethod.DELETE to AtomicBoolean()
)
fun get( path: String, handler: NoBodyHandler ) {
handle(HttpMethod.GET, path, handler )
}
fun post( path: String, handler: NoBodyHandler ) {
handle(HttpMethod.POST, path, handler )
}
inline fun <reified T> post(path: String, handler: JsonBodyHandler<T> ) {
handle(HttpMethod.POST, path, handler, T::class.java )
}
fun put( path: String, handler: NoBodyHandler ) {
handle(HttpMethod.PUT, path, handler )
}
inline fun <reified T> put(path: String, handler: JsonBodyHandler<T> ) {
handle(HttpMethod.PUT, path, handler, T::class.java )
}
fun delete( path: String, handler: NoBodyHandler ) {
handle(HttpMethod.PUT, path, handler )
}
inline fun <reified T> delete(path: String, handler: JsonBodyHandler<T> ) {
handle(HttpMethod.PUT, path, handler, T::class.java )
}
fun patch( path: String, handler: NoBodyHandler ) {
handle(HttpMethod.PUT, path, handler )
}
inline fun <reified T> patch(path: String, handler: JsonBodyHandler<T> ) {
handle(HttpMethod.PUT, path, handler, T::class.java )
}
fun handle( method: HttpMethod, path: String, handler: NoBodyHandler ) {
router.route( method, path ).handler{ event ->
handler.handle(
request = event.request(),
response = event.response()
)
}
}
fun <T> handle( method: HttpMethod, path: String, handler: JsonBodyHandler<T>, type: Class<T> ) {
val isFirstTimeThisMethodIsUsed = methodsThatMayReadBody[method]!!.compareAndSet( false, true )
if (isFirstTimeThisMethodIsUsed)
router.route().method(method).handler( BodyHandler.create() )
router.route( method, path ).handler { event ->
val body = event.bodyAsJson.mapTo(type)
handler.handle(
body = body,
request = event.request(),
response = event.response()
)
}
}
}
interface NoBodyHandler {
fun handle( request: HttpServerRequest, response: HttpServerResponse )
}
interface JsonBodyHandler<T> {
fun handle( body: T, request: HttpServerRequest, response: HttpServerResponse )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment