Skip to content
Permalink
Browse files

Configure app via ktor

Signed-off-by: Till Kottmann <me@deletescape.ch>
  • Loading branch information
deletescape committed Oct 22, 2019
1 parent 4127132 commit 27b86f067ff7b9b52fb21218aa2e603e7f6d77c6
@@ -4,7 +4,7 @@
<option name="actions">
<set>
<option value="activate" />
<option value="reformatChangedCode" />
<option value="reformat" />
</set>
</option>
<option name="configurationPath" value="" />
@@ -1,9 +1,23 @@
ktor {
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [ dog.del.app.ApplicationKt.module ]
}
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [dog.del.app.ApplicationKt.module]
}
}
dogbin {
db {
location = "dev.xdb"
location = ${?DB_LOCATION}

environment = "dev"
environment = ${?DB_ENVIRONMENT}
}
keys {
// DO NOT USE THIS DEFAULT IN PRODUCTION
session = "DEADBEEF"
session = ${?SESSION_KEY}
}
}
@@ -1,6 +1,7 @@
package dog.del.app

import com.mitchellbosecke.pebble.loader.ClasspathLoader
import dog.del.app.config.AppConfig
import dog.del.app.frontend.admin
import dog.del.app.frontend.frontend
import dog.del.app.frontend.legacyApi
@@ -20,6 +21,9 @@ import io.ktor.http.content.resource
import io.ktor.http.content.resources
import io.ktor.http.content.static
import io.ktor.pebble.Pebble
import io.ktor.server.engine.applicationEngineEnvironment
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.sessions.SessionTransportTransformerMessageAuthentication
import io.ktor.sessions.Sessions
import io.ktor.sessions.cookie
@@ -29,27 +33,31 @@ import jetbrains.exodus.database.TransientEntityStore
import ktor_health_check.Health
import org.koin.ktor.ext.Koin
import org.koin.ktor.ext.get
import org.slf4j.LoggerFactory
import java.io.File

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
val appConfig = AppConfig(environment.config)

install(Compression) {
gzip {
priority = 1.0
}
deflate {
priority = 10.0
minimumSize(1024) // condition
minimumSize(1024)
}
}


install(Koin) {
val appModule = org.koin.dsl.module {
// TODO: introduce config system
single { Database.init(File("dev.xdb"), "dev") }
single { appConfig }
single { Database.init(appConfig.db.location, appConfig.db.environment) }
single { Config.getConfig(get()) }
single<KeyGenerator> { PhoneticKeyGenerator() }
}
@@ -80,13 +88,11 @@ fun Application.module(testing: Boolean = false) {
}

install(Sessions) {
// TODO: move these to config and make them more secure
val key = hex("DEADBEEF")
cookie<WebSession>("doggie_session", XdSessionStorage()) {
transform(SessionTransportTransformerMessageAuthentication(key))
transform(SessionTransportTransformerMessageAuthentication(appConfig.keys.session))
}
header<ApiSession>("session", XdSessionStorage()) {
transform(SessionTransportTransformerMessageAuthentication(key))
transform(SessionTransportTransformerMessageAuthentication(appConfig.keys.session))
}
}

@@ -0,0 +1,20 @@
package dog.del.app.config

import io.ktor.config.ApplicationConfig
import io.ktor.util.hex
import java.io.File


class AppConfig(config: ApplicationConfig) {
val db = DbConfig(
location = File(config.property("dogbin.db.location").getString()),
environment = config.property("dogbin.db.environment").getString()
)

val keys = Keys(
session = hex(config.property("dogbin.keys.session").getString())
)

data class DbConfig(val location: File, val environment: String)
data class Keys(val session: ByteArray)
}

0 comments on commit 27b86f0

Please sign in to comment.
You can’t perform that action at this time.