Configuration
How Total.js loads the config files and .env files, how values are parsed, and how to read and change them through CONF.
On this page
Total.js reads the configuration of an application from plain text files in its root directory. Every value ends up in the global object CONF, which you can read from any file of the application.
The config files
Total.js loads up to two files when the application starts:
| File | When |
|---|---|
config | always |
config-debug | only in debug mode |
config-release | only in release mode |
The mode-specific file is read after config, so its values replace the values from config. Keep the common settings in config and put only the differences in config-debug or config-release.
config
config-debug
config-release
controllers/
definitions/
index.jsBefore the config files, Total.js loads the .env files (see below). In debug mode the watcher restarts the application when you save one of the config files.
Syntax
Each line is key : value. Total.js splits the line at the first :, so a value can contain more colons, for example a URL. Spaces around the key and the value are removed.
// Application
name : My Shop
version : 1.2.0
author : Peter
# Server
default_port : 8000
default_timezone : Europe/Bratislava
default_request_timeout : 5000
url : https://www.example.comRules:
- Empty lines are skipped.
- A line that starts with
#or//is a comment. Do not put comments at the end of a line, they become part of the value. - A line without
:is skipped. - The keys
debugandresourcesare ignored in the file.
How values are parsed
Without a type, Total.js guesses the type from the value:
| Value | Result |
|---|---|
8000 | the number 8000 |
0.5 | the number 0.5 |
true, false | a boolean |
0042 | the string "0042" (a leading zero keeps the string) |
1,5 | the string "1,5" |
-1 | the string "-1" |
| anything else | a string |
Many built-in keys have a fixed type. Keys like default_request_timeout or default_request_maxlength are always parsed as integers, and keys like allow_gzip or allow_compile are booleans: true, 1 and on mean true, everything else means false. version, secret, secret_uid and secret_encryption always stay strings.
Typed values
Put the type in brackets after the key to control the parsing:
limit (number) : -1
price (currency) : 9.90
code (string) : 0042
newsletter (bool) : enabled
languages (array) : en, sk, de
api (json) : { "url": "https://api.example.com", "retries": 3 }
smtp (object) : { port: 465, secure: true }
started (date) : 2024-01-01
database (env) : DATABASE_URL
token (random) : 20
password (generate) : 16| Type | Result |
|---|---|
string | the raw text |
number, float, double, currency | a number |
boolean, bool | true when the value contains true, on, 1 or enabled |
array | a list split by commas, trimmed |
json | parsed JSON |
object, eval | a JavaScript expression, evaluated |
date, datetime, time | a Date |
env, environment | the value of the environment variable with that name |
random | a new random string of the given length (default 10) on every start |
generate, hash | a random string that is created once and kept |
Values for generate and hash are stored in databases/config.json, so they stay the same after a restart. Use them for secrets that must not change, but that you do not want to write into the file.
A value that starts with base64 or hex is decoded first:
mail_password : base64 c2VjcmV0Environment variables
Next to the config files you can keep .env files with KEY=value lines. Total.js loads .env and then .env-debug or .env-release. A variable that already exists in process.env is never replaced, so the real environment wins.
# .env
DATABASE_URL=postgresql://user:pass@localhost:5432/shop
APP_ROOT=/shop/
WELCOME="Hello\nworld"Quotes around a value are removed and \n becomes a new line. Lines starting with # or // are comments.
Read a variable in the config with the (env) type:
database (env) : DATABASE_URL
default_root (env) : APP_ROOTIn code you can also use process.env directly.
Reading the configuration
CONF is a plain object. Read keys as properties:
exports.install = function() {
ROUTE('GET /api/info/', info);
};
function info() {
this.json({ name: CONF.name, version: CONF.version, url: CONF.url });
}Keys with a dot or other characters need the bracket notation, for example CONF['security.txt']. In views, CONF is available too: @{CONF.name}.
You can assign a value in code, but the change is not written to the files and some keys are only read once at start (default_port, directory_*, default_timezone):
CONF.allow_gzip = false;Changing the configuration at runtime
LOADCONFIG() merges new values into CONF. It accepts an object, a string in the config syntax or an array of { id, type, value } items, which is handy when the settings are stored in a database:
LOADCONFIG({ name: 'My Shop', allow_tms: true });
LOADCONFIG('name : My Shop\nlimit (number) : 50');
LOADCONFIG([
{ id: 'mail_smtp', type: 'string', value: 'smtp.example.com' },
{ id: 'limit', type: 'number', value: '50' },
{ id: 'languages', type: '[string]', value: 'en, sk' }
]);Options of the start script
The start script can pass values with options.config. They are applied before the config files are read, so a key in config replaces them:
var options = {};
options.config = { name: 'Total.js' };
require('total4/debug')(options);Threads
An application started with threads loads /threads/<name>/.env, /threads/<name>/config and the mode-specific files of that folder after the files of the root.
Common keys
name : My Shop
version : 1.0.0
url : https://www.example.com
secret : replace-me
default_timezone : utc
default_root : /shop/
default_request_maxlength: 1024
default_cors : example.com, app.example.com
allow_tms : true
mail_smtp : smtp.example.com
mail_smtp_options : { port: 465, secure: true, user: 'info@example.com', password: '***' }
mail_from : info@example.com
logger : trueThe reference lists every key with its default value.