-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from connorwyatt/server
Server package
- Loading branch information
Showing
5 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
dependencies { | ||
implementation(project(":configuration")) | ||
implementation(project(":eventstore")) | ||
implementation(project(":http")) | ||
implementation(project(":mongodb")) | ||
implementation(project(":rabbitmq")) | ||
implementation(project(":time")) | ||
|
||
implementation(libraries.kodein.di) | ||
implementation(libraries.kodein.di.framework.ktor.server) | ||
implementation(libraries.ktor.serialization.kotlinx.json) | ||
implementation(libraries.ktor.server.callId) | ||
implementation(libraries.ktor.server.callLogging) | ||
implementation(libraries.ktor.server.cio) | ||
implementation(libraries.ktor.server.contentNegotiation) | ||
implementation(libraries.ktor.server.core) | ||
implementation(libraries.ktor.server.requestValidation) | ||
implementation(libraries.ktor.server.statusPages) | ||
} |
103 changes: 103 additions & 0 deletions
103
server/src/main/kotlin/io/connorwyatt/common/server/ApplicationConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package io.connorwyatt.common.server | ||
|
||
import io.connorwyatt.common.eventstore.configuration.EventStoreConfiguration | ||
import io.connorwyatt.common.eventstore.ktor.configureEventStore | ||
import io.connorwyatt.common.mongodb.configuration.MongoDBConfiguration | ||
import io.connorwyatt.common.mongodb.ktor.configureMongoDB | ||
import io.connorwyatt.common.rabbitmq.configuration.RabbitMQConfiguration | ||
import io.connorwyatt.common.rabbitmq.ktor.configureRabbitMQ | ||
import io.ktor.server.application.* | ||
import io.ktor.server.plugins.requestvalidation.* | ||
import io.ktor.server.plugins.statuspages.* | ||
import io.ktor.server.routing.* | ||
import kotlinx.coroutines.runBlocking | ||
import org.kodein.di.DI | ||
import org.kodein.di.ktor.di | ||
|
||
class ApplicationConfiguration(block: Builder.() -> Unit) { | ||
val di = DI { importAll(builder.diModules) } | ||
|
||
private val builder: Builder = Builder().apply(block) | ||
|
||
fun applyTo(application: Application) { | ||
application.apply { | ||
di { extend(di) } | ||
builder.mongoDBConfiguration?.let { runBlocking { configureMongoDB() } } | ||
builder.eventStoreConfiguration?.let { configureEventStore(it) } | ||
builder.rabbitMQConfiguration?.let { configureRabbitMQ(it) } | ||
configureSerialization() | ||
builder.configureRequestValidation?.let { configureRequestValidation(it) } | ||
configureStatusPages(builder.configureStatusPages) | ||
configureCallId() | ||
configureCallLogging() | ||
builder.configureRouting?.let { configureRouting(it) } | ||
} | ||
} | ||
|
||
class Builder internal constructor() { | ||
internal var diModules = listOf<DI.Module>() | ||
private set | ||
|
||
internal var eventStoreConfiguration: EventStoreConfiguration? = null | ||
private set | ||
|
||
internal var mongoDBConfiguration: MongoDBConfiguration? = null | ||
private set | ||
|
||
internal var rabbitMQConfiguration: RabbitMQConfiguration? = null | ||
private set | ||
|
||
internal var http: Boolean = false | ||
private set | ||
|
||
internal var time: Boolean = false | ||
private set | ||
|
||
internal var configureRequestValidation: (RequestValidationConfig.() -> Unit)? = null | ||
private set | ||
|
||
internal var configureStatusPages: (StatusPagesConfig.() -> Unit)? = null | ||
private set | ||
|
||
internal var configureRouting: (Routing.() -> Unit)? = null | ||
private set | ||
|
||
fun addDIModule(diModule: DI.Module) { | ||
diModules = diModules.plus(diModule) | ||
} | ||
|
||
fun addEventStore(eventStoreConfiguration: EventStoreConfiguration) { | ||
this.eventStoreConfiguration = eventStoreConfiguration | ||
} | ||
|
||
fun addMongoDB(mongoDBConfiguration: MongoDBConfiguration) { | ||
this.mongoDBConfiguration = mongoDBConfiguration | ||
} | ||
|
||
fun addRabbitMQ(rabbitMQConfiguration: RabbitMQConfiguration) { | ||
this.rabbitMQConfiguration = rabbitMQConfiguration | ||
} | ||
|
||
fun addHttp() { | ||
this.http = true | ||
} | ||
|
||
fun addTime() { | ||
this.time = true | ||
} | ||
|
||
fun configureRequestValidation( | ||
configureRequestValidation: RequestValidationConfig.() -> Unit | ||
) { | ||
this.configureRequestValidation = configureRequestValidation | ||
} | ||
|
||
fun configureStatusPages(configureStatusPages: StatusPagesConfig.() -> Unit) { | ||
this.configureStatusPages = configureStatusPages | ||
} | ||
|
||
fun configureRouting(configureRouting: Routing.() -> Unit) { | ||
this.configureRouting = configureRouting | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
server/src/main/kotlin/io/connorwyatt/common/server/ApplicationExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.connorwyatt.common.server | ||
|
||
import io.connorwyatt.common.http.validation.ValidationProblemResponse | ||
import io.ktor.http.* | ||
import io.ktor.serialization.kotlinx.json.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.plugins.callid.* | ||
import io.ktor.server.plugins.callloging.* | ||
import io.ktor.server.plugins.contentnegotiation.* | ||
import io.ktor.server.plugins.requestvalidation.* | ||
import io.ktor.server.plugins.statuspages.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import java.util.* | ||
|
||
internal fun Application.configureSerialization() { | ||
install(ContentNegotiation) { json() } | ||
} | ||
|
||
internal fun Application.configureRequestValidation(configure: RequestValidationConfig.() -> Unit) { | ||
install(RequestValidation) { configure.invoke(this) } | ||
} | ||
|
||
internal fun Application.configureStatusPages(configure: (StatusPagesConfig.() -> Unit)? = null) { | ||
install(StatusPages) { | ||
exception<RequestValidationException> { call, cause -> | ||
call.response.headers.append( | ||
HttpHeaders.ContentType, | ||
ContentType.Application.ProblemJson.toString() | ||
) | ||
call.respond(HttpStatusCode.BadRequest, ValidationProblemResponse(cause.reasons)) | ||
} | ||
exception<Throwable> { call, _ -> | ||
call.respondText("", ContentType.Any, status = HttpStatusCode.InternalServerError) | ||
} | ||
configure?.invoke(this) | ||
} | ||
} | ||
|
||
internal fun Application.configureCallId() { | ||
install(CallId) { | ||
generate { UUID.randomUUID().toString() } | ||
replyToHeader(HttpHeaders.XRequestId) | ||
} | ||
} | ||
|
||
internal fun Application.configureCallLogging() { | ||
install(CallLogging) { | ||
callIdMdc("request-id") | ||
disableDefaultColors() | ||
mdc("http-method") { call -> call.request.httpMethod.value } | ||
mdc("request-url") { call -> call.request.uri } | ||
mdc("status-code") { call -> call.response.status()?.value?.toString() } | ||
} | ||
} | ||
|
||
internal fun Application.configureRouting(configure: Routing.() -> Unit) { | ||
routing { configure.invoke(this) } | ||
} |
15 changes: 15 additions & 0 deletions
15
server/src/main/kotlin/io/connorwyatt/common/server/Server.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.connorwyatt.common.server | ||
|
||
import io.ktor.server.cio.* | ||
import io.ktor.server.engine.* | ||
|
||
class Server(port: Int, private val applicationConfiguration: ApplicationConfiguration) { | ||
private val applicationEngine = | ||
embeddedServer(CIO, port = port, host = "localhost") { | ||
applicationConfiguration.applyTo(this) | ||
} | ||
|
||
fun start() { | ||
applicationEngine.start(wait = true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters