package de.krisenvorrat.server

import de.krisenvorrat.server.db.DatabaseFactory
import de.krisenvorrat.server.plugins.configureAuthentication
import de.krisenvorrat.server.plugins.configureCallLogging
import de.krisenvorrat.server.plugins.configureRouting
import de.krisenvorrat.server.plugins.configureSerialization
import de.krisenvorrat.server.plugins.configureStatusPages
import io.ktor.server.application.*
import io.ktor.server.netty.*

private const val DEFAULT_JWT_SECRET = "change-me-to-a-secure-jwt-secret-at-least-32-chars"

fun main(args: Array<String>) {
    EngineMain.main(args)
}

internal fun Application.module() {
    val jwtSecret = environment.config.property("krisenvorrat.jwtSecret").getString()
    if (jwtSecret == DEFAULT_JWT_SECRET) {
        log.warn("WARNING: KRISENVORRAT_JWT_SECRET is set to the default value. Set a strong secret before deploying to production.")
    }
    DatabaseFactory.init()
    configurePlugins()
}

internal fun Application.configurePlugins() {
    configureSerialization()
    configureStatusPages()
    configureCallLogging()
    configureAuthentication()
    configureRouting()
}
