Skip to content

Commit 6ede1b7

Browse files
sazzerpivovarit
authored andcommitted
Kovert examples (eugenp#5831)
1 parent 94f4092 commit 6ede1b7

File tree

8 files changed

+440
-0
lines changed

8 files changed

+440
-0
lines changed

core-kotlin/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@
7272
<artifactId>injekt-core</artifactId>
7373
<version>1.16.1</version>
7474
</dependency>
75+
<dependency>
76+
<groupId>uy.kohesive.kovert</groupId>
77+
<artifactId>kovert-vertx</artifactId>
78+
<version>[1.5.0,1.6.0)</version>
79+
<exclusions>
80+
<exclusion>
81+
<groupId>nl.komponents.kovenant</groupId>
82+
<artifactId>kovenant</artifactId>
83+
</exclusion>
84+
</exclusions>
85+
</dependency>
7586
</dependencies>
7687

7788
<properties>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.HttpVerb
16+
import uy.kohesive.kovert.core.Location
17+
import uy.kohesive.kovert.core.Verb
18+
import uy.kohesive.kovert.core.VerbAlias
19+
import uy.kohesive.kovert.vertx.bindController
20+
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
21+
import uy.kohesive.kovert.vertx.boot.KovertVerticle
22+
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
23+
import uy.kohesive.kovert.vertx.boot.KovertVertx
24+
25+
26+
class AnnotatedServer {
27+
companion object {
28+
private val LOG: Logger = LoggerFactory.getLogger(AnnotatedServer::class.java)
29+
30+
@JvmStatic
31+
fun main(args: Array<String>) {
32+
AnnotatedServer().start()
33+
}
34+
}
35+
36+
@VerbAlias("show", HttpVerb.GET)
37+
class AnnotatedController {
38+
fun RoutingContext.showStringById(id: String) = id
39+
40+
@Verb(HttpVerb.GET)
41+
@Location("/ping/:id")
42+
fun RoutingContext.ping(id: String) = id
43+
}
44+
45+
fun start() {
46+
Kodein.global.addImport(Kodein.Module {
47+
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", AnnotatedServer::class.java), ReferenceConfig())) {
48+
import("kovert.vertx", KodeinKovertVertx.configModule)
49+
import("kovert.server", KovertVerticleModule.configModule)
50+
}
51+
52+
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
53+
import(KodeinVertx.moduleWithLoggingToSlf4j)
54+
// Kovert boot
55+
import(KodeinKovertVertx.module)
56+
import(KovertVerticleModule.module)
57+
})
58+
59+
val initControllers = fun Router.() {
60+
bindController(AnnotatedController(), "api")
61+
}
62+
63+
// startup asynchronously...
64+
KovertVertx.start() bind { vertx ->
65+
KovertVerticle.deploy(vertx, routerInit = initControllers)
66+
} success { deploymentId ->
67+
LOG.warn("Deployment complete.")
68+
} fail { error ->
69+
LOG.error("Deployment failed!", error)
70+
}
71+
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.kovert
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty
4+
import io.vertx.ext.web.Router
5+
import io.vertx.ext.web.RoutingContext
6+
import nl.komponents.kovenant.functional.bind
7+
import org.kodein.di.Kodein
8+
import org.kodein.di.conf.global
9+
import org.slf4j.Logger
10+
import org.slf4j.LoggerFactory
11+
import uy.klutter.config.typesafe.ClassResourceConfig
12+
import uy.klutter.config.typesafe.ReferenceConfig
13+
import uy.klutter.config.typesafe.kodein.importConfig
14+
import uy.klutter.config.typesafe.loadConfig
15+
import uy.klutter.vertx.kodein.KodeinVertx
16+
import uy.kohesive.kovert.vertx.bindController
17+
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
18+
import uy.kohesive.kovert.vertx.boot.KovertVerticle
19+
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
20+
import uy.kohesive.kovert.vertx.boot.KovertVertx
21+
22+
class JsonServer {
23+
companion object {
24+
private val LOG: Logger = LoggerFactory.getLogger(JsonServer::class.java)
25+
26+
@JvmStatic
27+
fun main(args: Array<String>) {
28+
JsonServer().start()
29+
}
30+
}
31+
32+
data class Person(
33+
@JsonProperty("_id")
34+
val id: String,
35+
val name: String,
36+
val job: String
37+
)
38+
39+
class JsonController {
40+
fun RoutingContext.getPersonById(id: String) = Person(
41+
id = id,
42+
name = "Tony Stark",
43+
job = "Iron Man"
44+
)
45+
fun RoutingContext.putPersonById(id: String, person: Person) = person
46+
}
47+
48+
fun start() {
49+
Kodein.global.addImport(Kodein.Module {
50+
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", JsonServer::class.java), ReferenceConfig())) {
51+
import("kovert.vertx", KodeinKovertVertx.configModule)
52+
import("kovert.server", KovertVerticleModule.configModule)
53+
}
54+
55+
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
56+
import(KodeinVertx.moduleWithLoggingToSlf4j)
57+
// Kovert boot
58+
import(KodeinKovertVertx.module)
59+
import(KovertVerticleModule.module)
60+
})
61+
62+
val initControllers = fun Router.() {
63+
bindController(JsonController(), "api")
64+
}
65+
66+
// startup asynchronously...
67+
KovertVertx.start() bind { vertx ->
68+
KovertVerticle.deploy(vertx, routerInit = initControllers)
69+
} success { deploymentId ->
70+
LOG.warn("Deployment complete.")
71+
} fail { error ->
72+
LOG.error("Deployment failed!", error)
73+
}
74+
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.kovert
2+
3+
import io.vertx.ext.web.Router
4+
import nl.komponents.kovenant.functional.bind
5+
import org.kodein.di.Kodein
6+
import org.kodein.di.conf.global
7+
import org.slf4j.Logger
8+
import org.slf4j.LoggerFactory
9+
import uy.klutter.config.typesafe.ClassResourceConfig
10+
import uy.klutter.config.typesafe.ReferenceConfig
11+
import uy.klutter.config.typesafe.kodein.importConfig
12+
import uy.klutter.config.typesafe.loadConfig
13+
import uy.klutter.vertx.kodein.KodeinVertx
14+
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
15+
import uy.kohesive.kovert.vertx.boot.KovertVerticle
16+
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
17+
import uy.kohesive.kovert.vertx.boot.KovertVertx
18+
19+
class NoopServer {
20+
companion object {
21+
private val LOG: Logger = LoggerFactory.getLogger(NoopServer::class.java)
22+
23+
@JvmStatic
24+
fun main(args: Array<String>) {
25+
NoopServer().start()
26+
}
27+
}
28+
29+
30+
fun start() {
31+
Kodein.global.addImport(Kodein.Module {
32+
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", NoopServer::class.java), ReferenceConfig())) {
33+
import("kovert.vertx", KodeinKovertVertx.configModule)
34+
import("kovert.server", KovertVerticleModule.configModule)
35+
}
36+
37+
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
38+
import(KodeinVertx.moduleWithLoggingToSlf4j)
39+
// Kovert boot
40+
import(KodeinKovertVertx.module)
41+
import(KovertVerticleModule.module)
42+
})
43+
44+
val initControllers = fun Router.() {
45+
}
46+
47+
// startup asynchronously...
48+
KovertVertx.start() bind { vertx ->
49+
KovertVerticle.deploy(vertx, routerInit = initControllers)
50+
} success { deploymentId ->
51+
LOG.warn("Deployment complete.")
52+
} fail { error ->
53+
LOG.error("Deployment failed!", error)
54+
}
55+
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.vertx.bindController
16+
import uy.kohesive.kovert.vertx.boot.KodeinKovertVertx
17+
import uy.kohesive.kovert.vertx.boot.KovertVerticle
18+
import uy.kohesive.kovert.vertx.boot.KovertVerticleModule
19+
import uy.kohesive.kovert.vertx.boot.KovertVertx
20+
21+
22+
class SecuredServer {
23+
companion object {
24+
private val LOG: Logger = LoggerFactory.getLogger(SecuredServer::class.java)
25+
26+
@JvmStatic
27+
fun main(args: Array<String>) {
28+
SecuredServer().start()
29+
}
30+
}
31+
32+
class SecuredContext(private val routingContext: RoutingContext) {
33+
val authenticated = routingContext.request().getHeader("Authorization") == "Secure"
34+
}
35+
36+
class SecuredController {
37+
fun SecuredContext.getSecured() = this.authenticated
38+
}
39+
40+
fun start() {
41+
Kodein.global.addImport(Kodein.Module {
42+
importConfig(loadConfig(ClassResourceConfig("/kovert.conf", SecuredServer::class.java), ReferenceConfig())) {
43+
import("kovert.vertx", KodeinKovertVertx.configModule)
44+
import("kovert.server", KovertVerticleModule.configModule)
45+
}
46+
47+
// includes jackson ObjectMapper to match compatibility with Vertx, app logging via Vertx facade to Slf4j
48+
import(KodeinVertx.moduleWithLoggingToSlf4j)
49+
// Kovert boot
50+
import(KodeinKovertVertx.module)
51+
import(KovertVerticleModule.module)
52+
})
53+
54+
val initControllers = fun Router.() {
55+
bindController(SecuredController(), "api")
56+
}
57+
58+
// startup asynchronously...
59+
KovertVertx.start() bind { vertx ->
60+
KovertVerticle.deploy(vertx, routerInit = initControllers)
61+
} success { deploymentId ->
62+
LOG.warn("Deployment complete.")
63+
} fail { error ->
64+
LOG.error("Deployment failed!", error)
65+
}
66+
67+
}
68+
}

0 commit comments

Comments
 (0)