I'd like to run `install(CORS) { anyHost() }` just...
# ktor
m
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
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
👍 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
What other frameworks do? Do you have a link to the documentation so I can check?
m
Play let's you check
environment.isDev
https://stackoverflow.com/a/38816414/61298
d
and how does it differentiate? how do you set the environment?
Copy code
{
    "env": "${build.environment}",
    // app.json...
}
is this?
m
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
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
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
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
Cheers!