https://kotlinlang.org logo
#ktor
Title
m

Matt

06/21/2018, 12:06 PM
I'd like to run
install(CORS) { anyHost() }
just when I'm running locally in developer mode -- is there a standard way to check that programatically?
1
📝 1
❤️ 1
d

Deactivated User

06/21/2018, 12:09 PM
Actually, first you need a mechanism to differentiate environments. Usually an environment variable does the trick. What about:
if (System.getenv("ENV") != "prod") { install(CORS) { anyHost() } }
then in prod, you would have to set
ENV=prod
when launching
m

Matt

06/21/2018, 12:12 PM
👍 Thanks, yeah, I can do that (just wondered if there was any special feature in ktor for doing that, as other frameworks I've used before have had similar things)
d

Deactivated User

06/21/2018, 12:12 PM
What other frameworks do? Do you have a link to the documentation so I can check?
m

Matt

06/21/2018, 12:17 PM
Play let's you check
environment.isDev
https://stackoverflow.com/a/38816414/61298
d

Deactivated User

06/21/2018, 12:18 PM
and how does it differentiate? how do you set the environment?
Copy code
{
    "env": "${build.environment}",
    // app.json...
}
is this?
m

Matt

06/21/2018, 12:19 PM
No, if you run it locally with SBT, it'll be in dev mode If you create a dist for production, it'll be in prod mode
d

Deactivated User

06/21/2018, 12:20 PM
I see
I’m not sure if we can/should do that kind of detection since it could be misleading in some cases. Some could run locally a fatjar or from gradle in production. What I can do is to create a page in the documentation website with suggestions about setting this. Maybe we can set the environment in the HOCON file and allow to grab it from an environment variable, while providing a default. For example:
Copy code
ktor {
    environment = dev
    environment = ${?ENV}
}
With a simple extension method we can get this value and decide or even create several extension properties that tries things like dev or prod. What do you think?
m

Matt

06/21/2018, 12:26 PM
Sounds good -- it'd be nice to have a standard convention for what var to use, e.g. Node have a `NODE_ENV`: https://dzone.com/articles/what-you-should-know-about-node-env
d

Deactivated User

06/21/2018, 12:30 PM
Maybe KTOR_ENV? I’m going to add a page about environments in the meantime. But if you think it should be standardised, I would add an issues here: https://github.com/ktorio/ktor/issues Or even a PR if you have some ideas on how could it be.
m

Matt

06/21/2018, 12:42 PM
Cheers!
5 Views