Skip to content

Commit

Permalink
ensure that env is read before we run ServerIntegrationTest
Browse files Browse the repository at this point in the history
  • Loading branch information
angryziber committed Dec 15, 2024
1 parent 3c31a79 commit b3d959e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
88 changes: 45 additions & 43 deletions sample/src/Launcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,66 @@ import java.nio.file.Path
import java.time.Duration.ofSeconds

fun main() {
Config.useEnvFile()
sampleServer().start()
}

fun sampleServer(port: Int = 8080) = Server(listen = InetSocketAddress(port)).apply {
use<JsonBody>() // enables parsing/sending of application/json requests/responses, depending on the Accept header
fun sampleServer(port: Int = 8080): Server {
Config.useEnvFile()
return Server(listen = InetSocketAddress(port)).apply {
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
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))
assets("/", AssetsHandler(Path.of("public"), useIndexForUnknownPaths = true))

register(HttpClient.newBuilder().connectTimeout(ofSeconds(5)).build())
register(HttpClient.newBuilder().connectTimeout(ofSeconds(5)).build())

before<AdminChecker>()
after { ex, err -> ex.header("X-Error", err?.message ?: "none") }
before<AdminChecker>()
after { ex, err -> ex.header("X-Error", err?.message ?: "none") }

context("/hello") {
get { "Hello World" }
context("/hello") {
get { "Hello World" }

get("/delay") {
delay(1000)
"Waited for 1 sec"
}
get("/delay") {
delay(1000)
"Waited for 1 sec"
}

get("/failure") { error("Failure") }
get("/failure") { error("Failure") }

get("/admin") @AdminOnly {
"Only for admins"
}
get("/admin") @AdminOnly {
"Only for admins"
}

get("/param/:param") {
"Path: ${path("param")}, Query: $queryParams"
}
get("/param/:param") {
"Path: ${path("param")}, Query: $queryParams"
}

post("/post") {
data class JsonRequest(val required: String, val hello: String = "World")
body<JsonRequest>()
}
post("/post") {
data class JsonRequest(val required: String, val hello: String = "World")
body<JsonRequest>()
}

decorator { ex, h -> "<${h(ex)}>" }
get("/decorated") { "!!!" }
}
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("/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>()
context("/auth") {
register<OAuthUserProvider>(UserRepository::class)
annotated<AuthRoutes>()
annotated<OAuthRoutes>()
}
}
}
2 changes: 1 addition & 1 deletion server/src/klite/annotations/Annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Param(val p: KParameter) {
is PathParam -> e.path(name)?.toType()
is QueryParam ->
if (p.type.jvmErasure.isSuperclassOf(List::class)) e.queryList(name).map { Converter.from<Any>(it, p.type.arguments[0].type!!) }
else (e.query(name)).let { if (it !is String) it else if (e.isValueLessQueryParam(it)) true else it.toType()
else (e.query(name)).let { if (e.isValueLessQueryParam(it)) true else it?.toType()
}
is HeaderParam -> e.header(name)?.toType()
is CookieParam -> e.cookie(name)?.toType()
Expand Down

0 comments on commit b3d959e

Please sign in to comment.