Kovert examples (#5831)
This commit is contained in:
parent
94f4092314
commit
6ede1b7440
|
@ -72,6 +72,17 @@
|
|||
<artifactId>injekt-core</artifactId>
|
||||
<version>1.16.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>uy.kohesive.kovert</groupId>
|
||||
<artifactId>kovert-vertx</artifactId>
|
||||
<version>[1.5.0,1.6.0)</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>nl.komponents.kovenant</groupId>
|
||||
<artifactId>kovenant</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.kovert
|
||||
|
||||
import io.vertx.ext.web.Router
|
||||
import io.vertx.ext.web.RoutingContext
|
||||
import nl.komponents.kovenant.functional.bind
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.conf.global
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import uy.klutter.config.typesafe.ClassResourceConfig
|
||||
import uy.klutter.config.typesafe.ReferenceConfig
|
||||
import uy.klutter.config.typesafe.kodein.importConfig
|
||||
import uy.klutter.config.typesafe.loadConfig
|
||||
import uy.klutter.vertx.kodein.KodeinVertx
|
||||
import uy.kohesive.kovert.core.HttpVerb
|
||||
import uy.kohesive.kovert.core.Location
|
||||
import uy.kohesive.kovert.core.Verb
|
||||
import uy.kohesive.kovert.core.VerbAlias
|
||||
import uy.kohesive.kovert.vertx.bindController
|
||||
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticle
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVertx
|
||||
|
||||
|
||||
class AnnotatedServer {
|
||||
companion object {
|
||||
private val LOG: Logger = LoggerFactory.getLogger(AnnotatedServer::class.java)
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
AnnotatedServer().start()
|
||||
}
|
||||
}
|
||||
|
||||
@VerbAlias("show", HttpVerb.GET)
|
||||
class AnnotatedController {
|
||||
fun RoutingContext.showStringById(id: String) = id
|
||||
|
||||
@Verb(HttpVerb.GET)
|
||||
@Location("/ping/:id")
|
||||
fun RoutingContext.ping(id: String) = id
|
||||
}
|
||||
|
||||
fun start() {
|
||||
Kodein.global.addImport(Kodein.Module {
|
||||
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", AnnotatedServer::class.java), ReferenceConfig())) {
|
||||
import("kovert.vertx", KodeinKovertVertx.configModule)
|
||||
import("kovert.server", KovertVerticleModule.configModule)
|
||||
}
|
||||
|
||||
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
|
||||
import(KodeinVertx.moduleWithLoggingToSlf4j)
|
||||
// Kovert boot
|
||||
import(KodeinKovertVertx.module)
|
||||
import(KovertVerticleModule.module)
|
||||
})
|
||||
|
||||
val initControllers = fun Router.() {
|
||||
bindController(AnnotatedController(), "api")
|
||||
}
|
||||
|
||||
// startup asynchronously...
|
||||
KovertVertx.start() bind { vertx ->
|
||||
KovertVerticle.deploy(vertx, routerInit = initControllers)
|
||||
} success { deploymentId ->
|
||||
LOG.warn("Deployment complete.")
|
||||
} fail { error ->
|
||||
LOG.error("Deployment failed!", error)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.kovert
|
||||
|
||||
import io.vertx.ext.web.Router
|
||||
import io.vertx.ext.web.RoutingContext
|
||||
import nl.komponents.kovenant.functional.bind
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.conf.global
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import uy.klutter.config.typesafe.ClassResourceConfig
|
||||
import uy.klutter.config.typesafe.ReferenceConfig
|
||||
import uy.klutter.config.typesafe.kodein.importConfig
|
||||
import uy.klutter.config.typesafe.loadConfig
|
||||
import uy.klutter.vertx.kodein.KodeinVertx
|
||||
import uy.kohesive.kovert.core.HttpErrorCode
|
||||
import uy.kohesive.kovert.core.HttpErrorCodeWithBody
|
||||
import uy.kohesive.kovert.core.HttpErrorForbidden
|
||||
import uy.kohesive.kovert.vertx.bindController
|
||||
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticle
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVertx
|
||||
|
||||
|
||||
class ErrorServer {
|
||||
companion object {
|
||||
private val LOG: Logger = LoggerFactory.getLogger(ErrorServer::class.java)
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
ErrorServer().start()
|
||||
}
|
||||
}
|
||||
|
||||
class ErrorController {
|
||||
fun RoutingContext.getForbidden() {
|
||||
throw HttpErrorForbidden()
|
||||
}
|
||||
fun RoutingContext.getError() {
|
||||
throw HttpErrorCode("Something went wrong", 590)
|
||||
}
|
||||
fun RoutingContext.getErrorbody() {
|
||||
throw HttpErrorCodeWithBody("Something went wrong", 591, "Body here")
|
||||
}
|
||||
}
|
||||
|
||||
fun start() {
|
||||
Kodein.global.addImport(Kodein.Module {
|
||||
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", ErrorServer::class.java), ReferenceConfig())) {
|
||||
import("kovert.vertx", KodeinKovertVertx.configModule)
|
||||
import("kovert.server", KovertVerticleModule.configModule)
|
||||
}
|
||||
|
||||
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
|
||||
import(KodeinVertx.moduleWithLoggingToSlf4j)
|
||||
// Kovert boot
|
||||
import(KodeinKovertVertx.module)
|
||||
import(KovertVerticleModule.module)
|
||||
})
|
||||
|
||||
val initControllers = fun Router.() {
|
||||
bindController(ErrorController(), "api")
|
||||
}
|
||||
|
||||
// startup asynchronously...
|
||||
KovertVertx.start() bind { vertx ->
|
||||
KovertVerticle.deploy(vertx, routerInit = initControllers)
|
||||
} success { deploymentId ->
|
||||
LOG.warn("Deployment complete.")
|
||||
} fail { error ->
|
||||
LOG.error("Deployment failed!", error)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package com.baeldung.kovert
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
import io.vertx.ext.web.Router
|
||||
import io.vertx.ext.web.RoutingContext
|
||||
import nl.komponents.kovenant.functional.bind
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.conf.global
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import uy.klutter.config.typesafe.ClassResourceConfig
|
||||
import uy.klutter.config.typesafe.ReferenceConfig
|
||||
import uy.klutter.config.typesafe.kodein.importConfig
|
||||
import uy.klutter.config.typesafe.loadConfig
|
||||
import uy.klutter.vertx.kodein.KodeinVertx
|
||||
import uy.kohesive.kovert.vertx.bindController
|
||||
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticle
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVertx
|
||||
|
||||
class JsonServer {
|
||||
companion object {
|
||||
private val LOG: Logger = LoggerFactory.getLogger(JsonServer::class.java)
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
JsonServer().start()
|
||||
}
|
||||
}
|
||||
|
||||
data class Person(
|
||||
@JsonProperty("_id")
|
||||
val id: String,
|
||||
val name: String,
|
||||
val job: String
|
||||
)
|
||||
|
||||
class JsonController {
|
||||
fun RoutingContext.getPersonById(id: String) = Person(
|
||||
id = id,
|
||||
name = "Tony Stark",
|
||||
job = "Iron Man"
|
||||
)
|
||||
fun RoutingContext.putPersonById(id: String, person: Person) = person
|
||||
}
|
||||
|
||||
fun start() {
|
||||
Kodein.global.addImport(Kodein.Module {
|
||||
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", JsonServer::class.java), ReferenceConfig())) {
|
||||
import("kovert.vertx", KodeinKovertVertx.configModule)
|
||||
import("kovert.server", KovertVerticleModule.configModule)
|
||||
}
|
||||
|
||||
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
|
||||
import(KodeinVertx.moduleWithLoggingToSlf4j)
|
||||
// Kovert boot
|
||||
import(KodeinKovertVertx.module)
|
||||
import(KovertVerticleModule.module)
|
||||
})
|
||||
|
||||
val initControllers = fun Router.() {
|
||||
bindController(JsonController(), "api")
|
||||
}
|
||||
|
||||
// startup asynchronously...
|
||||
KovertVertx.start() bind { vertx ->
|
||||
KovertVerticle.deploy(vertx, routerInit = initControllers)
|
||||
} success { deploymentId ->
|
||||
LOG.warn("Deployment complete.")
|
||||
} fail { error ->
|
||||
LOG.error("Deployment failed!", error)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.kovert
|
||||
|
||||
import io.vertx.ext.web.Router
|
||||
import nl.komponents.kovenant.functional.bind
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.conf.global
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import uy.klutter.config.typesafe.ClassResourceConfig
|
||||
import uy.klutter.config.typesafe.ReferenceConfig
|
||||
import uy.klutter.config.typesafe.kodein.importConfig
|
||||
import uy.klutter.config.typesafe.loadConfig
|
||||
import uy.klutter.vertx.kodein.KodeinVertx
|
||||
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticle
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVertx
|
||||
|
||||
class NoopServer {
|
||||
companion object {
|
||||
private val LOG: Logger = LoggerFactory.getLogger(NoopServer::class.java)
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
NoopServer().start()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun start() {
|
||||
Kodein.global.addImport(Kodein.Module {
|
||||
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", NoopServer::class.java), ReferenceConfig())) {
|
||||
import("kovert.vertx", KodeinKovertVertx.configModule)
|
||||
import("kovert.server", KovertVerticleModule.configModule)
|
||||
}
|
||||
|
||||
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
|
||||
import(KodeinVertx.moduleWithLoggingToSlf4j)
|
||||
// Kovert boot
|
||||
import(KodeinKovertVertx.module)
|
||||
import(KovertVerticleModule.module)
|
||||
})
|
||||
|
||||
val initControllers = fun Router.() {
|
||||
}
|
||||
|
||||
// startup asynchronously...
|
||||
KovertVertx.start() bind { vertx ->
|
||||
KovertVerticle.deploy(vertx, routerInit = initControllers)
|
||||
} success { deploymentId ->
|
||||
LOG.warn("Deployment complete.")
|
||||
} fail { error ->
|
||||
LOG.error("Deployment failed!", error)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.kovert
|
||||
|
||||
import io.vertx.ext.web.Router
|
||||
import io.vertx.ext.web.RoutingContext
|
||||
import nl.komponents.kovenant.functional.bind
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.conf.global
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import uy.klutter.config.typesafe.ClassResourceConfig
|
||||
import uy.klutter.config.typesafe.ReferenceConfig
|
||||
import uy.klutter.config.typesafe.kodein.importConfig
|
||||
import uy.klutter.config.typesafe.loadConfig
|
||||
import uy.klutter.vertx.kodein.KodeinVertx
|
||||
import uy.kohesive.kovert.vertx.bindController
|
||||
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticle
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVertx
|
||||
|
||||
|
||||
class SecuredServer {
|
||||
companion object {
|
||||
private val LOG: Logger = LoggerFactory.getLogger(SecuredServer::class.java)
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
SecuredServer().start()
|
||||
}
|
||||
}
|
||||
|
||||
class SecuredContext(private val routingContext: RoutingContext) {
|
||||
val authenticated = routingContext.request().getHeader("Authorization") == "Secure"
|
||||
}
|
||||
|
||||
class SecuredController {
|
||||
fun SecuredContext.getSecured() = this.authenticated
|
||||
}
|
||||
|
||||
fun start() {
|
||||
Kodein.global.addImport(Kodein.Module {
|
||||
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", SecuredServer::class.java), ReferenceConfig())) {
|
||||
import("kovert.vertx", KodeinKovertVertx.configModule)
|
||||
import("kovert.server", KovertVerticleModule.configModule)
|
||||
}
|
||||
|
||||
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
|
||||
import(KodeinVertx.moduleWithLoggingToSlf4j)
|
||||
// Kovert boot
|
||||
import(KodeinKovertVertx.module)
|
||||
import(KovertVerticleModule.module)
|
||||
})
|
||||
|
||||
val initControllers = fun Router.() {
|
||||
bindController(SecuredController(), "api")
|
||||
}
|
||||
|
||||
// startup asynchronously...
|
||||
KovertVertx.start() bind { vertx ->
|
||||
KovertVerticle.deploy(vertx, routerInit = initControllers)
|
||||
} success { deploymentId ->
|
||||
LOG.warn("Deployment complete.")
|
||||
} fail { error ->
|
||||
LOG.error("Deployment failed!", error)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.kovert
|
||||
|
||||
import io.vertx.ext.web.Router
|
||||
import io.vertx.ext.web.RoutingContext
|
||||
import nl.komponents.kovenant.functional.bind
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.conf.global
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import uy.klutter.config.typesafe.ClassResourceConfig
|
||||
import uy.klutter.config.typesafe.ReferenceConfig
|
||||
import uy.klutter.config.typesafe.kodein.importConfig
|
||||
import uy.klutter.config.typesafe.loadConfig
|
||||
import uy.klutter.vertx.kodein.KodeinVertx
|
||||
import uy.kohesive.kovert.vertx.bindController
|
||||
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticle
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
|
||||
import uy.kohesive.kovert.vertx.boot.KovertVertx
|
||||
|
||||
|
||||
class SimpleServer {
|
||||
companion object {
|
||||
private val LOG: Logger = LoggerFactory.getLogger(SimpleServer::class.java)
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
SimpleServer().start()
|
||||
}
|
||||
}
|
||||
|
||||
class SimpleController {
|
||||
fun RoutingContext.getStringById(id: String) = id
|
||||
fun RoutingContext.get_truncatedString_by_id(id: String, length: Int = 1) = id.subSequence(0, length)
|
||||
}
|
||||
|
||||
fun start() {
|
||||
Kodein.global.addImport(Kodein.Module {
|
||||
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", SimpleServer::class.java), ReferenceConfig())) {
|
||||
import("kovert.vertx", KodeinKovertVertx.configModule)
|
||||
import("kovert.server", KovertVerticleModule.configModule)
|
||||
}
|
||||
|
||||
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
|
||||
import(KodeinVertx.moduleWithLoggingToSlf4j)
|
||||
// Kovert boot
|
||||
import(KodeinKovertVertx.module)
|
||||
import(KovertVerticleModule.module)
|
||||
})
|
||||
|
||||
val initControllers = fun Router.() {
|
||||
bindController(SimpleController(), "api")
|
||||
}
|
||||
|
||||
// startup asynchronously...
|
||||
KovertVertx.start() bind { vertx ->
|
||||
KovertVerticle.deploy(vertx, routerInit = initControllers)
|
||||
} success { deploymentId ->
|
||||
LOG.warn("Deployment complete.")
|
||||
} fail { error ->
|
||||
LOG.error("Deployment failed!", error)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
kovert: {
|
||||
vertx: {
|
||||
clustered: false
|
||||
}
|
||||
server: {
|
||||
listeners: [
|
||||
{
|
||||
host: "0.0.0.0"
|
||||
port: "8000"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue