|
| 1 | +package com.baeldung.kovert |
| 2 | + |
| 3 | +import io.vertx.ext.web.Router |
| 4 | +import io.vertx.ext.web.RoutingContext |
| 5 | +import nl.komponents.kovenant.functional.bind |
| 6 | +import org.kodein.di.Kodein |
| 7 | +import org.kodein.di.conf.global |
| 8 | +import org.slf4j.Logger |
| 9 | +import org.slf4j.LoggerFactory |
| 10 | +import uy.klutter.config.typesafe.ClassResourceConfig |
| 11 | +import uy.klutter.config.typesafe.ReferenceConfig |
| 12 | +import uy.klutter.config.typesafe.kodein.importConfig |
| 13 | +import uy.klutter.config.typesafe.loadConfig |
| 14 | +import uy.klutter.vertx.kodein.KodeinVertx |
| 15 | +import uy.kohesive.kovert.core.HttpErrorCode |
| 16 | +import uy.kohesive.kovert.core.HttpErrorCodeWithBody |
| 17 | +import uy.kohesive.kovert.core.HttpErrorForbidden |
| 18 | +import uy.kohesive.kovert.vertx.bindController |
| 19 | +import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx |
| 20 | +import uy.kohesive.kovert.vertx.boot.KovertVerticle |
| 21 | +import uy.kohesive.kovert.vertx.boot.KovertVerticleModule |
| 22 | +import uy.kohesive.kovert.vertx.boot.KovertVertx |
| 23 | + |
| 24 | + |
| 25 | +class ErrorServer { |
| 26 | + companion object { |
| 27 | + private val LOG: Logger = LoggerFactory.getLogger(ErrorServer::class.java) |
| 28 | + |
| 29 | + @JvmStatic |
| 30 | + fun main(args: Array<String>) { |
| 31 | + ErrorServer().start() |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + class ErrorController { |
| 36 | + fun RoutingContext.getForbidden() { |
| 37 | + throw HttpErrorForbidden() |
| 38 | + } |
| 39 | + fun RoutingContext.getError() { |
| 40 | + throw HttpErrorCode("Something went wrong", 590) |
| 41 | + } |
| 42 | + fun RoutingContext.getErrorbody() { |
| 43 | + throw HttpErrorCodeWithBody("Something went wrong", 591, "Body here") |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + fun start() { |
| 48 | + Kodein.global.addImport(Kodein.Module { |
| 49 | + importConfig(loadConfig(ClassResourceConfig("/kovert.conf", ErrorServer::class.java), ReferenceConfig())) { |
| 50 | + import("kovert.vertx", KodeinKovertVertx.configModule) |
| 51 | + import("kovert.server", KovertVerticleModule.configModule) |
| 52 | + } |
| 53 | + |
| 54 | + // includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j |
| 55 | + import(KodeinVertx.moduleWithLoggingToSlf4j) |
| 56 | + // Kovert boot |
| 57 | + import(KodeinKovertVertx.module) |
| 58 | + import(KovertVerticleModule.module) |
| 59 | + }) |
| 60 | + |
| 61 | + val initControllers = fun Router.() { |
| 62 | + bindController(ErrorController(), "api") |
| 63 | + } |
| 64 | + |
| 65 | + // startup asynchronously... |
| 66 | + KovertVertx.start() bind { vertx -> |
| 67 | + KovertVerticle.deploy(vertx, routerInit = initControllers) |
| 68 | + } success { deploymentId -> |
| 69 | + LOG.warn("Deployment complete.") |
| 70 | + } fail { error -> |
| 71 | + LOG.error("Deployment failed!", error) |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | +} |
0 commit comments