Skip to content

Instantly share code, notes, and snippets.

@johnbanq
Created February 20, 2020 11:44
Show Gist options
  • Save johnbanq/02fc3dd52b038273e60cc8d38e1db842 to your computer and use it in GitHub Desktop.
Save johnbanq/02fc3dd52b038273e60cc8d38e1db842 to your computer and use it in GitHub Desktop.
sample of vert.x verticle using a more explicit approach
package com.gzanyan.mcpks.logging
import com.gzanyan.mcpks.common.framework.suspendHandler
import com.gzanyan.mcpks.common.toolkit.auth.jwt.jwtAuthHandler
import com.gzanyan.mcpks.common.toolkit.di.*
import com.gzanyan.mcpks.common.toolkit.eventbus.eventBus
import com.gzanyan.mcpks.common.toolkit.web.httpServer
import com.gzanyan.mcpks.common.toolkit.web.logFailureHandler
import com.gzanyan.mcpks.logging.api.LogEventEBService
import com.gzanyan.mcpks.logging.inbound.LogEventQueryController
import io.vertx.core.http.HttpMethod
import io.vertx.core.http.HttpServer
import io.vertx.ext.auth.jwt.JWTAuth
import io.vertx.kotlin.core.http.closeAwait
import io.vertx.kotlin.core.http.listenAwait
import io.vertx.kotlin.coroutines.CoroutineVerticle
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.getBean
import org.springframework.context.ApplicationContext
import org.springframework.context.support.GenericApplicationContext
open class LoggingVerticle : CoroutineVerticle() {
private val log = LoggerFactory.getLogger(LoggingVerticle::class.java)
lateinit var container: GenericApplicationContext
lateinit var options: LoggingVerticleOptions
lateinit var jwt: JWTAuth
lateinit var http: HttpServer
override suspend fun start() {
options = LoggingVerticleOptions.fromJson(config)
container = configureDI()
container.refresh()
jwt = JWTAuth.create(vertx, options.jwtAuthOptions)
http = configureHttpServer()
http.listenAwait()
configureEventBus()
}
override suspend fun stop() {
http.closeAwait()
}
protected open fun configureDI(): GenericApplicationContext {
val verticle = this
return di {
register(SpringConfiguration::class.java)
register(SpringDBConfiguration::class.java)
beanFactory {
commonBeans(
vertx = vertx,
verticle = verticle,
options = options
)
}
}
}
protected open fun configureHttpServer(): HttpServer {
val scope = this
return httpServer(vertx) {
routes {
route(HttpMethod.GET, "/api/logEvents") {
jwtAuthHandler(jwt)
suspendHandler(scope, bean<LogEventQueryController>()::listLogEvents)
}
route(HttpMethod.GET, "/api/logEvents/count") {
jwtAuthHandler(jwt)
suspendHandler(scope, bean<LogEventQueryController>()::countLogEvent)
}
logFailureHandler(log)
}
options(options.httpServerOptions)
}
}
protected open fun configureEventBus() {
eventBus(vertx) {
service(options.logEventEBServiceAddress, bean<LogEventEBService>())
}
}
private inline fun <reified T : Any> bean(): T {
return container.getBean<T>()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment