-
Notifications
You must be signed in to change notification settings - Fork 16
/
Launcher.kt
77 lines (62 loc) · 2.28 KB
/
Launcher.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import klite.*
import klite.annotations.annotated
import klite.jdbc.*
import klite.json.JsonBody
import klite.oauth.AuthRoutes
import klite.oauth.OAuthRoutes
import klite.oauth.OAuthUserProvider
import klite.openapi.openApi
import kotlinx.coroutines.delay
import users.UserRepository
import java.net.InetSocketAddress
import java.net.http.HttpClient
import java.nio.file.Path
import java.time.Duration.ofSeconds
fun main() {
sampleServer().start()
}
fun sampleServer(port: Int = 8080) = Server(listen = InetSocketAddress(port)).apply {
Config.useEnvFile()
use<JsonBody>() // enables parsing/sending of application/json requests/responses, depending on the Accept header
if (Config.isDev) startDevDB() // start docker-compose db automatically
use(DBModule(PooledDataSource())) // configure a DataSource
use<DBMigrator>() // migrate the DB
use<RequestTransactionHandler>() // runs each request in a transaction
assets("/", AssetsHandler(Path.of("public"), useIndexForUnknownPaths = true))
register(HttpClient.newBuilder().connectTimeout(ofSeconds(5)).build())
before<AdminChecker>()
after { ex, err -> ex.header("X-Error", err?.message ?: "none") }
context("/hello") {
get { "Hello World" }
get("/delay") {
delay(1000)
"Waited for 1 sec"
}
get("/failure") { error("Failure") }
get("/admin") @AdminOnly {
"Only for admins"
}
get("/param/:param") {
"Path: ${path("param")}, Query: $queryParams"
}
post("/post") {
data class JsonRequest(val required: String, val hello: String = "World")
body<JsonRequest>()
}
decorator { ex, h -> "<${h(ex)}>" }
get("/decorated") { "!!!" }
}
context("/api") {
useOnly<JsonBody>() // in case only json should be supported in this context
before(CorsHandler()) // enable CORS for this context, so that Swagger-UI can access the API
useHashCodeAsETag() // automatically send 304 NotModified if request generates the same response as before
annotated<APIRoutes>() // read routes from an annotated class - such classes are easier to unit-test
annotated<SSERoutes>("/sse") // Server-Side Events sample
openApi()
}
context("/auth") {
register<OAuthUserProvider>(UserRepository::class)
annotated<AuthRoutes>()
annotated<OAuthRoutes>()
}
}