1
+ 'use strict' ;
2
+
3
+ /**
4
+ * The Module Dynamically loads the configurations for
5
+ * the heroku deployed project. This way of managing the configuration
6
+ * is done because of the heroku suggestion for
7
+ * Multiple Environments for the App article.
8
+ */
9
+
10
+ const url = require ( 'url' ) ;
11
+
12
+
13
+ /**
14
+ * Returns the Redis config object for the staging,
15
+ * testing and production servers
16
+ * @returns {{port: *, host: (*|string), pass: *} }
17
+ * @private
18
+ */
19
+ function redisConfig ( ) {
20
+ if ( ! process . env . REDISCLOUD_URL || process . env . REDISCLOUD_URL === 'undefined' ) {
21
+ return null ;
22
+ }
23
+ var redisURL = url . parse ( process . env . REDISCLOUD_URL ) ;
24
+ return {
25
+ port : redisURL . port ,
26
+ host : redisURL . hostname ,
27
+ pass : redisURL . auth . split ( ':' ) [ 1 ]
28
+ } ;
29
+ }
30
+
31
+ /**
32
+ * Returns the mongo db config for the staging,
33
+ * testing and production servers
34
+ * @returns {* }
35
+ * @private
36
+ */
37
+ function mongoConfig ( ) {
38
+ return process . env . MONGOHQ_URL !== 'undefined' && process . env . MONGOHQ_URL ||
39
+ process . env . MONGOLAB_URI !== 'undefined' && process . env . MONGOLAB_URI ;
40
+ }
41
+
42
+
43
+ function mergeSharedConfigs ( shared , config ) {
44
+ for ( var key in shared ) {
45
+ config [ key ] = config [ key ] || shared [ key ] ;
46
+ }
47
+
48
+ return config
49
+ }
50
+
51
+
52
+ /**
53
+ * Creates a config object dynamically for the application.
54
+ * @returns {* }
55
+ * @private
56
+ */
57
+ function createConfig ( ) {
58
+ const env = process . env . NODE_ENV || 'development' ;
59
+ var config = require ( './config' ) ;
60
+
61
+ config = mergeSharedConfigs ( config . shared , config [ env ] ) ;
62
+
63
+ config . fbPageToken = process . env . FB_VERIFY_TOKEN || config . fbPageToken ;
64
+ config . fbPageID = process . env . FB_PAGE_ID || config . fbPageID ;
65
+ config . fbWebhookVerifyToken = process . env . FB_WEBHOOK_VERIFY_TOKEN || config . fbWebhookVerifyToken ;
66
+ config . witToken = process . env . WIT_TOKEN || config . witToken ;
67
+
68
+ config . redis = redisConfig ( ) || config . redis ;
69
+ config . db = mongoConfig ( ) || config . db ;
70
+
71
+ return config ;
72
+ }
73
+
74
+ module . exports = createConfig ( ) ;
0 commit comments