I've been playing around a bit with configuration: https://github.com/tipsy/javalin/pull/507
The general idea is to move away from app.enabledXyz methods, and to reduce the amount of methods on the Javalin class:
val app = Javalin.create().config { c ->
c.contextPath = "/"
c.caseSensitiveUrls = false
c.devLogging = true
c.requestLogger { ctx, ms ->
println("${ctx.method()} ${ctx.path()}, took $ms}")
}
c.server { Server() }
c.addStaticFiles("/public")
c.addSinglePageHandler("/", "/public/subpage/index.html")
}.start()
Any thoughts?
This is a great idea! Maybe this could lead to having the option of using external config files?
@fwgreen The idea is to make it easier to configure Javalin via config files at least, but I wasn't planning on having an official "Javalin Config File" concept. When you configure your apps, do you usually have more than one config file?
When I use Javalin, the only config file in my project is my persistence stuff. My little projects are not sophisticated enough to require configuration management. I only just suggested a possible future direction as it occurred to me that if one wanted to, for some reason, alter the port number after deployment, they couldn't without going back into the code. I wasn't thinking of anything as comprehensive as Spring 馃槃. Maybe you can add it to the 4.0 wishlist?
You definitely can already use config files now, this is how I configure my projects:
object Config {
val port = ProcessBuilder().environment()["PORT"]?.toInt() ?: 7070 // use port 7070 for localhost
val isLocalhost = port == 7070
val resourcePath = if (isLocalhost) "src/main/resources/public" else "/public"
val resourceLocation = if (isLocalhost) Location.EXTERNAL else Location.CLASSPATH
val secrets = JavalinJson.fromJson(Util.readResource("/deploy/secrets.json"), Secrets::class.java)
}
data class Secrets(
val sendgridApiKey: String,
val pagerdutyApiKey: String,
val basicAuthPassword: String,
val githubBot: String,
val githubToken: String
)
...
val app = Javalin.create().apply {
enableStaticFiles(Config.resourcePath, Config.resourceLocation)
}.start(Config.port)
I'm reluctant to add a custom format, since then users will end up with more than 1 config file.
Wow! This library just keeps getting better: You've thought of everything. Could you please add some more documentation about this feature. I would love to be able to manage my app's port number or it's logging level from a config.json file.
It's not really a feature of Javalin, it's reading a file and mapping it to a Kotlin/Java object. I see that a lot of people are confused about this, so I can write a tutorial after Javalin 3 is done.
In #517 I split Javalin in to three classes. Config could look like this:
val app = Javalin.create().configureServer{ server ->
server.contextPath = "/"
server.caseSensitiveUrls = false
server.server { Server() }
}.configureServlet { servlet ->
servlet.devLogging = true
servlet.requestLogger { ctx, ms ->
println("${ctx.method()} ${ctx.path()}, took $ms}")
}
servlet.addStaticFiles("/public")
servlet.addSinglePageHandler("/", "/public/subpage/index.html")
}.start(0)
Alternative:
val app = Javalin.create().configure { server, servlet ->
server.contextPath = "/"
server.caseSensitiveUrls = false
server.server { Server() }
servlet.devLogging = true
servlet.requestLogger { ctx, ms ->
println("${ctx.method()} ${ctx.path()}, took $ms}")
}
servlet.addStaticFiles("/public")
servlet.addSinglePageHandler("/", "/public/subpage/index.html")
}.start(0)
PR up now: https://github.com/tipsy/javalin/pull/534
Most helpful comment
Alternative: