UNPKG

12.1 kBJavaScriptView Raw
1var convict = require('convict')
2var fs = require('fs')
3
4var conf = convict({
5 app: {
6 name: {
7 doc: 'The applicaton name',
8 format: String,
9 default: 'DADI API Repo Default'
10 }
11 },
12 publicUrl: {
13 host: {
14 doc: 'The host of the URL where the API instance can be publicly accessed at',
15 format: '*',
16 default: null,
17 env: 'URL_HOST'
18 },
19 port: {
20 doc: 'The port of the URL where the API instance can be publicly accessed at',
21 format: '*',
22 default: null,
23 env: 'URL_PORT'
24 },
25 protocol: {
26 doc: 'The protocol of the URL where the API instance can be publicly accessed at',
27 format: 'String',
28 default: 'http',
29 env: 'URL_PROTOCOL'
30 }
31 },
32 server: {
33 host: {
34 doc: 'Accept connections on the specified address. If the host is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available, or any IPv4 address (0.0.0.0) otherwise.',
35 format: '*',
36 default: null,
37 env: 'HOST'
38 },
39 port: {
40 doc: 'Accept connections on the specified port. A value of zero will assign a random port.',
41 format: Number,
42 default: 8081,
43 env: 'PORT'
44 },
45 redirectPort: {
46 doc: 'Port to redirect http connections to https from',
47 format: 'port',
48 default: 0,
49 env: 'REDIRECT_PORT'
50 },
51 protocol: {
52 doc: 'The protocol the web application will use',
53 format: String,
54 default: 'http',
55 env: 'PROTOCOL'
56 },
57 sslPassphrase: {
58 doc: 'The passphrase of the SSL private key',
59 format: String,
60 default: '',
61 env: 'SSL_PRIVATE_KEY_PASSPHRASE'
62 },
63 sslPrivateKeyPath: {
64 doc: 'The filename of the SSL private key',
65 format: String,
66 default: '',
67 env: 'SSL_PRIVATE_KEY_PATH'
68 },
69 sslCertificatePath: {
70 doc: 'The filename of the SSL certificate',
71 format: String,
72 default: '',
73 env: 'SSL_CERTIFICATE_PATH'
74 },
75 sslIntermediateCertificatePath: {
76 doc: 'The filename of an SSL intermediate certificate, if any',
77 format: String,
78 default: '',
79 env: 'SSL_INTERMEDIATE_CERTIFICATE_PATH'
80 },
81 sslIntermediateCertificatePaths: {
82 doc: 'The filenames of SSL intermediate certificates, overrides sslIntermediateCertificate (singular)',
83 format: Array,
84 default: [],
85 env: 'SSL_INTERMEDIATE_CERTIFICATE_PATHS'
86 }
87 },
88 datastore: {
89 doc: 'The name of the npm module that implements the data connector used for storing documents',
90 format: String,
91 default: '@dadi/api-mongodb'
92 },
93 auth: {
94 tokenUrl: {
95 doc: 'The endpoint used for token generation',
96 format: String,
97 default: '/token'
98 },
99 tokenTtl: {
100 doc: 'The amount of time (in seconds) which bearer tokens are valid for',
101 format: Number,
102 default: 1800
103 },
104 tokenKey: {
105 doc: 'The private key used to sign JWT tokens',
106 format: String,
107 default: 'YOU-MUST-CHANGE-ME!'
108 },
109 accessCollection: {
110 doc: 'The name of the internal collection used to store aggregate permissions data',
111 format: String,
112 default: 'accessStore'
113 },
114 clientCollection: {
115 doc: 'The name of the internal collection used to store clients',
116 format: String,
117 default: 'clientStore'
118 },
119 roleCollection: {
120 doc: 'The name of the internal collection used to store roles',
121 format: String,
122 default: 'roleStore'
123 },
124 datastore: {
125 doc: 'The name of the npm module that implements the data connector used for authentication',
126 format: String,
127 default: '@dadi/api-mongodb'
128 },
129 database: {
130 doc: 'The name of the database used to store authentication data',
131 format: String,
132 default: 'test',
133 env: 'DB_AUTH_NAME'
134 }
135 },
136 search: {
137 enabled: {
138 doc: 'If true, API responds to collection /search endpoints',
139 format: Boolean,
140 default: false
141 },
142 minQueryLength: {
143 doc: 'Minimum search string length',
144 format: Number,
145 default: 3
146 },
147 wordCollection: {
148 doc: 'The name of the datastore collection that will hold tokenized words',
149 format: String,
150 default: 'words'
151 },
152 datastore: {
153 doc: 'The datastore to use for storing and querying indexed documents',
154 format: String,
155 default: '@dadi/api-mongodb'
156 },
157 database: {
158 doc: 'The name of the database to use for storing and querying indexed documents',
159 format: String,
160 default: 'search',
161 env: 'DB_SEARCH_NAME'
162 }
163 },
164 caching: {
165 ttl: {
166 doc: '',
167 format: Number,
168 default: 300
169 },
170 directory: {
171 enabled: {
172 doc: 'If enabled, cache files will be saved to the filesystem',
173 format: Boolean,
174 default: true
175 },
176 path: {
177 doc: 'The relative path to the cache directory',
178 format: String,
179 default: './cache/api'
180 },
181 extension: {
182 doc: 'The extension to use for cache files',
183 format: String,
184 default: 'json'
185 },
186 autoFlush: {
187 doc: '',
188 format: Boolean,
189 default: true
190 },
191 autoFlushInterval: {
192 doc: '',
193 format: Number,
194 default: 60
195 }
196 },
197 redis: {
198 enabled: {
199 doc: 'If enabled, cache files will be saved to the specified Redis server',
200 format: Boolean,
201 default: false,
202 env: 'REDIS_ENABLED'
203 },
204 host: {
205 doc: 'The Redis server host',
206 format: String,
207 default: '127.0.0.1',
208 env: 'REDIS_HOST'
209 },
210 port: {
211 doc: 'The port for the Redis server',
212 format: 'port',
213 default: 6379,
214 env: 'REDIS_PORT'
215 },
216 password: {
217 doc: '',
218 format: String,
219 default: '',
220 env: 'REDIS_PASSWORD'
221 }
222 }
223 },
224 logging: {
225 enabled: {
226 doc: 'If true, logging is enabled using the following settings.',
227 format: Boolean,
228 default: true
229 },
230 level: {
231 doc: 'Sets the logging level.',
232 format: ['debug', 'info', 'warn', 'error', 'trace'],
233 default: 'info'
234 },
235 path: {
236 doc: 'The absolute or relative path to the directory for log files.',
237 format: String,
238 default: './log'
239 },
240 filename: {
241 doc: 'The name to use for the log file, without extension.',
242 format: String,
243 default: 'api'
244 },
245 extension: {
246 doc: 'The extension to use for the log file.',
247 format: String,
248 default: 'log'
249 },
250 accessLog: {
251 enabled: {
252 doc: 'If true, HTTP access logging is enabled. The log file name is similar to the setting used for normal logging, with the addition of "access". For example `api.access.log`.',
253 format: Boolean,
254 default: true
255 },
256 kinesisStream: {
257 doc: 'An AWS Kinesis stream to write to log records to.',
258 format: String,
259 default: '',
260 env: 'KINESIS_STREAM'
261 }
262 }
263 },
264 paths: {
265 collections: {
266 doc: 'The relative or absolute path to collection specification files',
267 format: String,
268 default: 'workspace/collections'
269 },
270 endpoints: {
271 doc: 'The relative or absolute path to custom endpoint files',
272 format: String,
273 default: 'workspace/endpoints'
274 },
275 hooks: {
276 doc: 'The relative or absolute path to hook specification files',
277 format: String,
278 default: 'workspace/hooks'
279 }
280 },
281 feedback: {
282 doc: '',
283 format: Boolean,
284 default: false
285 },
286 status: {
287 enabled: {
288 doc: 'If true, status endpoint is enabled.',
289 format: Boolean,
290 default: false
291 },
292 routes: {
293 doc: 'An array of routes to test. Each route object must contain properties `route` and `expectedResponseTime`.',
294 format: Array,
295 default: []
296 }
297 },
298 query: {
299 useVersionFilter: {
300 doc: 'If true, the API version parameter is extracted from the request URL and passed to the database query',
301 format: Boolean,
302 default: false
303 }
304 },
305 media: {
306 defaultBucket: {
307 doc: 'The name of the default media bucket',
308 format: String,
309 default: 'mediaStore'
310 },
311 buckets: {
312 doc: 'The names of media buckets to be used',
313 format: Array,
314 default: []
315 },
316 tokenSecret: {
317 doc: 'The secret key used to sign and verify tokens when uploading media',
318 format: String,
319 default: 'catboat-beatific-drizzle'
320 },
321 tokenExpiresIn: {
322 doc: 'The duration a signed token is valid for. Expressed in seconds or a string describing a time span (https://github.com/zeit/ms). Eg: 60, "2 days", "10h", "7d"',
323 format: '*',
324 default: '1h'
325 },
326 storage: {
327 doc: 'Determines the storage type for uploads',
328 format: ['disk', 's3'],
329 default: 'disk'
330 },
331 basePath: {
332 doc: 'Sets the root directory for uploads',
333 format: String,
334 default: 'workspace/media'
335 },
336 pathFormat: {
337 doc: 'Determines the format for the generation of subdirectories to store uploads',
338 format: ['none', 'date', 'datetime', 'sha1/4', 'sha1/5', 'sha1/8'],
339 default: 'date'
340 },
341 s3: {
342 accessKey: {
343 doc: 'The access key used to connect to an S3-compatible storage provider',
344 format: String,
345 default: '',
346 env: 'AWS_S3_ACCESS_KEY'
347 },
348 secretKey: {
349 doc: 'The secret key used to connect to an S3-compatible storage provider',
350 format: String,
351 default: '',
352 env: 'AWS_S3_SECRET_KEY'
353 },
354 bucketName: {
355 doc: 'The name of the S3 bucket in which to store uploads',
356 format: String,
357 default: '',
358 env: 'AWS_S3_BUCKET_NAME'
359 },
360 region: {
361 doc: 'The region for an S3-compatible storage provider',
362 format: String,
363 default: '',
364 env: 'AWS_S3_REGION'
365 },
366 endpoint: {
367 doc: 'The endpoint for an S3-compatible storage provider',
368 format: String,
369 default: ''
370 }
371 }
372 },
373 env: {
374 doc: 'The applicaton environment.',
375 format: String,
376 default: 'development',
377 env: 'NODE_ENV',
378 arg: 'node_env'
379 },
380 cluster: {
381 doc: 'If true, API runs in cluster mode, starting a worker for each CPU core',
382 format: Boolean,
383 default: false
384 },
385 cors: {
386 doc: 'If true, responses will include headers for cross-domain resource sharing',
387 format: Boolean,
388 default: true
389 },
390 internalFieldsPrefix: {
391 doc: 'The character to be used for prefixing internal fields',
392 format: 'String',
393 default: '_'
394 },
395 databaseConnection: {
396 maxRetries: {
397 doc: 'The maximum number of times to reconnection attempts after a database fails',
398 format: Number,
399 default: 10
400 }
401 },
402 i18n: {
403 defaultLanguage: {
404 doc: 'ISO-639-1 code of the default language',
405 format: String,
406 default: 'en'
407 },
408 languages: {
409 doc: 'List of ISO-639-1 codes for the supported languages',
410 format: Array,
411 default: []
412 },
413 fieldCharacter: {
414 doc: 'Special character to denote a translated field',
415 format: String,
416 default: ':'
417 }
418 },
419 featureQuery: {
420 enabled: {
421 doc: 'Whether feature query via custom headers is enabled',
422 format: Boolean,
423 default: true
424 }
425 }
426})
427
428// Load environment dependent configuration
429var env = conf.get('env')
430conf.loadFile('./config/config.' + env + '.json')
431
432// Load domain-specific configuration
433conf.updateConfigDataForDomain = function (domain) {
434 var domainConfig = './config/' + domain + '.json'
435
436 try {
437 var stats = fs.statSync(domainConfig)
438 // no error, file exists
439 conf.loadFile(domainConfig)
440 } catch (err) {
441 if (err.code === 'ENOENT') {
442 // console.log('No domain-specific configuration file: ' + domainConfig)
443 } else {
444 console.log(err)
445 }
446 }
447}
448
449module.exports = conf
450module.exports.configPath = function () {
451 return './config/config.' + conf.get('env') + '.json'
452}