-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
80 lines (67 loc) · 2.11 KB
/
index.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
/**
* Main program. Starts the {@link Lockmaster} and {@link module:lib/data-server}.
* @module index
*/
var path = require('path'),
url = require('url'),
_ = require('lodash'),
express = require('express'),
buildStaticSite = require('./lib/site-builder'),
RiverFactory = require('./lib/river-factory'),
Lockmaster = require('./lib/lockmaster'),
startDataService = require('./lib/data-server'),
configuration = require('./lib/configuration'),
RedisClient = require('./lib/redis-client'),
appConfig = path.join(__dirname, 'config.yml'),
CONFIG, REDIS_URL, redisClient, lockmaster, rivers = [],
webapp = express();
// read application configuration
CONFIG = configuration.parseYaml(appConfig);
// Make local config substitutions
if (process.env['PORT']) {
CONFIG.port = process.env['PORT'];
}
if (process.env['HOST']) {
CONFIG.host = process.env['HOST'];
}
if (CONFIG.host == 'http://localhost') {
CONFIG.baseurl = CONFIG.host + ':' + CONFIG.port;
} else {
CONFIG.baseurl = CONFIG.host;
}
// Fail fast if no Redis connection URL is set.
if (!process.env[CONFIG.redisEnv]) {
throw new Error('Expected Redis connection to be set into environment variable "' + CONFIG.redisEnv + '".');
} else {
REDIS_URL = process.env[CONFIG.redisEnv];
}
// Connect to redis
redisClient = new RedisClient(
REDIS_URL,
CONFIG.maxDataPointsPerRequest,
CONFIG.defaults.json.snapto
);
redisClient.initialize(function(err) {
if (err) throw err;
// Look for data-sources.
rivers = RiverFactory.createRivers(
CONFIG.riverDir, CONFIG.defaults.river, redisClient
);
console.log('Starting with %s rivers:', rivers.length);
lockmaster = new Lockmaster({
config: CONFIG,
rivers: rivers,
redisClient: redisClient
});
lockmaster.start()
buildStaticSite(CONFIG, function(err) {
if (err) throw err;
webapp.use('/static', express.static('build'));
startDataService({
app: webapp,
redisClient: redisClient,
rivers: rivers,
config: CONFIG
});
});
});