Every now and then it's useful if your application tweaks itself to the environment its running in; be it development, testing or production.
For example, during development its great for your web application to serve up 500 error pages with stack traces, session dumps and other detailed system information. But in production, so not to scare users, you're gonna want to turn that off and serve up a plain Internal Application Error page instead.
Other reasons may include environment specific database connections, email accounts and so on.
IocEnv is a small utility library for Fantom that scans system information to determine what environment your application is running in. It looks at environment variables, program arguments and even offers a manual override option.
IocEnv is built on top of IoC and makes the IocEnv class available as an IoC service. It also integrates with IocConfig to give you a handful of injectable config values. All of these may be freely used by your IoC application.
The IocEnv class holds the environment setting and has handy utility methods for some common use cases.
You may manually set the environment when you create an IocEnv instance.
The ordering means the environment variables are checked first but may be overridden by program arguments. Subsequently, program arguments may be overridden by setting the environment manually.
The convention for prefixing program arguments with a hyphen is taken from the @Opt facet and AbstractMain. If no environment setting is found, it defaults to PRODUCTION as this is often the lowest common denominator. It also means it's easy to deploy your application to production, which is good, as prod deployments can be tricky enough!
To ensure apps always run in development mode on your local machine, set a new environment variable called ENV to the value dev or development.
Sometimes you want to override the environment setting programmatically - which is common in testing. You can do this by manually creating the IocEnv service in a test specific AppModule, passing in whatever environment you want.
As we can't change or un-make the existing IocEnv service, we create a new service instance and contribute it as a service override. IoC then uses the override everywhere in place of the original.
We have seen it is useful and important for an application to distinguish between the environments it runs in. We've looked at how the IocEnv library is environmentally aware and how your application can use it to make decisions dependent on the environment it runs in.
31 Jul 2016 - Updated examples to use IoC 3.0 and IocEnv 1.1.
3 Dec 2013 - Original article.