generated from billymfl/simple-microservices-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
48 lines (42 loc) · 1.48 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @fileOverview Contains the environment variables used by the app and the schema to
* validate agaist the input.
*
* @author Billy Marin
*
* @requires NPM:@hapi/Joi
*
*/
const env = require('dotenv').config();
if (env.error) {
console.error('No .env file found or error trying to parse it.');
}
const pkg = require('./package');
const Joi = require('@hapi/joi');
exports.NODE_ENV = process.env.NODE_ENV || 'development';
exports.APPNAME = pkg.name;
exports.VERSION = pkg.version;
// debug is only active if DEBUG=appname is passed in
exports.debug = require('debug')(exports.APPNAME);
// define the schema of the environment variables
const schema = Joi.object({
PORT: Joi.number().integer().min(80).max(65535).default(80),
HOST: Joi.string().hostname().default('0.0.0.0'),
SERVICES: Joi.string().required(),
MAILGUN_API: Joi.string(),
MAILGUN_DOMAIN: Joi.string(),
MAILJET_API: Joi.string(),
TIMEOUT: Joi.number().integer().min(1).default(2),
});
// if there is a validation error set _error, else assign env vars. usually we should exit on error.
const result = schema.validate(process.env, {stripUnknown: true});
if (result.error) {
exports._error = 'Invalid configuration:\n' + result.error.details
.map((error) => error.message)
.join('.\n');
} else {
// do any other processing here on the variables before they are assigned to the exports
const rv = result.value;
rv.SERVICES = rv.SERVICES.split(',');
Object.assign(exports, result.value);
}