Introduction

The config directory contains all of the OstroJS framework's configuration files. Each option is described, so take a look at the files to familiarise yourself with your options.

These configuration files let you set up things like your database connection information, your cache server information, and other important settings like your application timezone and encryption key.

Environment Configuration

Different configuration variables dependent on the environment where the programme is executing are frequently useful. You could want to utilise a different cache driver locally than on your production server, for example.

OstroJS uses the DotEnv NPM package to make this easy. The root directory of your application will have a .env.example file that defines several typical environment variables in a new OstroJS installation. This file will be automatically transferred to .env throughout the OstroJS installation process.

The default .env file in OstroJS provides certain common configuration variables that may vary depending on whether your app is running locally or on a production web server. These values are then obtained using OstroJS's env function from multiple OstroJS configuration files in the config directory.

If you're working on an application with a group, you might want to keep include a .env.example file. Other developers on your team will be able to see which environment variables are required to execute your application if you use placeholder values in the example configuration file.

Environment Variable Types

Because all variables in .env files are generally processed as strings, several reserved values have been established to allow the env() method to return a larger variety of types:

.env Valueenv() Value
true(bool) true
false (bool) false
“”(string) “”
null(Object) null

 

Environment File Security

Because each developer / server running your application may require a different environment setting, your .env file should not be submitted to your application's source control. Furthermore, if an intruder obtains access to your source control repository, any confidential credentials would be revealed, posing a security concern.

Retrieving Environment Configuration

The process will be loaded with all of the variables mentioned in this file. When your application start, set process.env super-global. You may, however, use the env helper to get values from these variables in your configuration files using the env helper. In fact, if you go through the OstroJS configuration files, you'll see that this helper is already used by a lot of the options:

'debug' : env('APP_DEBUG', false),

The "default value" is the second parameter provided to the env function. If no environment variable for the specified key exists, this value will be returned.

Determining The Current Environment

The APP_ENV variable in your .env file determines the current application environment. This value may be accessed using the environment method on the app facade:

const app = require('@ostro/support/facades/app');

let $environment = app.environment();

To see if the environment matches a certain value, you can send parameters to the environment method. If any of the following values are present in the environment, the procedure will return true:

if (app.environment('local')) {
    // The environment is local
}

if (app.environment(['local', 'staging'])) {
    // The environment is either local OR staging...
}

Accessing Configuration Values

The global config helper function allows you to quickly retrieve your configuration variables from anywhere in your application. The configuration values may be retrieved using "dot" syntax, which contains the file and option names. If the configuration option does not exist, a default value can be given and will be returned:

let value = config('app.timezone');

// Retrieve a default value if the configuration value does not exist...
let value = config('app.timezone', 'Asia/Seoul');

To set configuration values at runtime, pass an object  to the config helper:

config({'app.timezone' : 'America/Chicago'});

Debug Mode

The debug option in your config/app.js configuration file controls how much error information is presented to the user. This option is set by default to respect the value of the APP_DEBUG environment variable contained in your .env file.

Set the APP_DEBUG environment variable to true for local development. This value should always be false in your production environment. You risk exposing sensitive configuration data to your application's end users if the variable is set to true in production.