-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.js
143 lines (137 loc) · 3.49 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict'
const convict = require('convict')
const fs = require('fs')
const mkdirp = require('mkdirp')
const path = require('path')
const conf = convict({
env: {
doc: 'The applicaton environment',
format: String,
default: 'development',
env: 'NODE_ENV'
},
queue: {
host: {
doc: 'The queue server hostname or IP',
format: '*',
default: '127.0.0.1'
},
port: {
doc: 'The queue server port number',
format: 'port',
default: 6379
}
},
broker: {
queue: {
doc: 'The queue name',
format: String,
default: '',
arg: 'queue'
},
interval: {
doc: 'The polling intervals, in seconds',
format: Array,
default: [ 0, 1, 5, 10 ]
},
retries: {
doc: 'The number of times a message will be retried after failing',
format: Number,
default: 10
},
timeout: {
doc: 'The number of seconds until a message is placed back on the queue',
format: Number,
default: 30
},
throttle: {
workers: {
doc: 'The number of workers that should execute concurrently',
format: Number,
default: 5
},
queue: {
unit: {
doc: 'The unit of measurement used for queue throttling',
format: ['second', 'minute', 'five-minute', 'quarter-hour', 'half-hour', 'hour', 'day'],
default: 'minute'
},
value: {
doc: 'The value used for queue throttling. The rate will be limited to value/unit. Zero implies no limit.',
format: Number,
default: 0
},
discard: {
doc: 'Whether or not to discard throttled messages',
format: Boolean,
default: false
}
},
messages: {
doc: 'Message specific throttle limits',
format: '*',
default: {}
}
}
},
workers: {
path: {
doc: 'The absolute or relative path to the directory for worker modules',
format: String,
default: './workers'
}
},
logging: {
enabled: {
doc: 'Enable or disable logging',
format: Boolean,
default: false
},
level: {
doc: 'The minimum error level to be logged',
format: String,
default: 'info'
},
path: {
doc: 'The absolute or relative path to the directory for log files',
format: String,
default: './log'
},
filename: {
doc: 'The name to use for the log file, without extension',
format: String,
default: 'error'
},
extension: {
doc: 'The extension to use for the log file',
format: String,
default: 'log'
},
accessLog: {
enabled: {
doc: 'Enable or disable access logging',
format: Boolean,
default: false
}
}
}
})
function loadConfig () {
const configPath = path.join(process.cwd(), 'config/config.development.json')
const configSamplePath = path.join(__dirname, 'config/config.development.json.sample')
const sampleConfig = fs.readFileSync(configSamplePath, { encoding: 'utf-8'})
try {
const s = fs.readFileSync(configPath, { encoding: 'utf-8'})
} catch (err) {
if (err.code === 'ENOENT') {
const made = mkdirp.sync(path.join(process.cwd(), 'config'))
fs.writeFileSync(configPath, sampleConfig)
console.log('\nCreated configuration file at ' + configPath + '\n')
}
} finally {
const env = conf.get('env')
conf.loadFile('config/config.' + env + '.json')
}
}
loadConfig()
module.exports = conf