Add CORS config

This commit is contained in:
Hubert Van De Walle 2020-05-07 23:53:30 +02:00
parent cf52525926
commit 04ded6de17
5 changed files with 12 additions and 6 deletions

View File

@ -2,4 +2,5 @@ MYSQL_ROOT_PASSWORD=
MYSQL_DATABASE= MYSQL_DATABASE=
MYSQL_USER= MYSQL_USER=
MYSQL_PASSWORD= MYSQL_PASSWORD=
JWT_SECRET= JWT_SECRET=
CORS=false

View File

@ -10,6 +10,7 @@ database:
server: server:
host: 0.0.0.0 host: 0.0.0.0
port: 8081 port: 8081
cors: ${CORS:-true}
jwt: jwt:
secret: ${JWT_SECRET} secret: ${JWT_SECRET}

View File

@ -19,7 +19,6 @@ import org.kodein.di.generic.singleton
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import javax.sql.DataSource import javax.sql.DataSource
import kotlin.system.exitProcess
val kodein = Kodein { val kodein = Kodein {
import(serviceModule) import(serviceModule)
@ -57,7 +56,7 @@ fun serve(kodein: Kodein) {
fun Application.module() { fun Application.module() {
loadFeatures() loadFeatures(kodein)
log.debug(kodein.container.tree.bindings.description()) log.debug(kodein.container.tree.bindings.description())

View File

@ -23,7 +23,7 @@ val configurationModule = Kodein.Module(name = "Configuration") {
} }
data class DatabaseConfig(val host: String, val port: Int, val name: String, val username: String, val password: Masked) data class DatabaseConfig(val host: String, val port: Int, val name: String, val username: String, val password: Masked)
data class ServerConfig(val host: String, val port: Int) data class ServerConfig(val host: String, val port: Int, val cors: Boolean)
data class JwtConfig(val secret: Masked, val auth: JwtValidity, val refresh: JwtValidity) data class JwtConfig(val secret: Masked, val auth: JwtValidity, val refresh: JwtValidity)
data class JwtValidity(val validity: Long, val unit: TimeUnit) data class JwtValidity(val validity: Long, val unit: TimeUnit)
data class Config(val database: DatabaseConfig, val server: ServerConfig, val jwt: JwtConfig) data class Config(val database: DatabaseConfig, val server: ServerConfig, val jwt: JwtConfig)

View File

@ -2,9 +2,14 @@ package be.vandewalleh.features
import be.vandewalleh.auth.authenticationModule import be.vandewalleh.auth.authenticationModule
import io.ktor.application.* import io.ktor.application.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
fun Application.loadFeatures() { fun Application.loadFeatures(kodein: Kodein) {
corsFeature() val config by kodein.instance<Config>()
if (config.server.cors) {
corsFeature()
}
contentNegotiationFeature() contentNegotiationFeature()
authenticationModule() authenticationModule()
handleErrors() handleErrors()