forked from mozilla/api.webmaker.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
92 lines (78 loc) · 2.14 KB
/
server.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
require('habitat').load();
require('newrelic');
var Hapi = require('hapi'),
Hoek = require('hoek');
Hoek.assert(process.env.API_HOST, 'Must define API_HOST');
Hoek.assert(process.env.PORT, 'Must define PORT');
Hoek.assert(process.env.API_VERSION, 'Must define API_VERSION');
Hoek.assert(process.env.REDIS_URL, 'Must define REDIS_URL');
Hoek.assert(process.env.POSTGRE_CONNECTION_STRING, 'Must define POSTGRE_CONNECTION_STRING');
var serverConfig = {
connections: {
router: {
stripTrailingSlash: true
},
routes: {
security: true
}
}
};
if ( process.env.REDIS_URL ) {
var redisUrl = require('redis-url').parse(process.env.REDIS_URL);
serverConfig.cache = {
engine: require('catbox-redis'),
host: redisUrl.hostname,
port: redisUrl.port,
password: redisUrl.password
};
}
var server = new Hapi.Server(serverConfig);
Hoek.assert(process.env.API_HOST, 'Must define API_HOST');
Hoek.assert(process.env.PORT, 'Must define PORT');
var connection = {
uri: process.env.URI,
host: process.env.API_HOST,
port: process.env.PORT
};
server.connection(connection);
server.register({
register: require('hapi-bunyan'),
options: require('./lib/log-config')()
}, function(err) {
Hoek.assert(!err, err);
server.decorate('server', 'debug', function(logData) {
this.log.bind(this, 'debug').apply(this, arguments);
});
});
server.register(require('./adapters/plugins'), function(err) {
if ( err ) {
server.log('error', {
message: 'Error registering plugins',
error: err
});
throw err;
}
var authConfig = require('./lib/auth-config');
var tokenValidator = process.env.MOCKED_AUTH ?
require('./lib/mockValidator') : require('./lib/tokenValidator');
server.auth.strategy(
'token',
'bearer-access-token',
authConfig(tokenValidator)
);
});
server.register(require('./services'), function(err) {
if ( err ) {
server.log('error', {
message: 'Error registering services',
error: err
});
throw err;
}
server.start(function(err) {
if ( err ) {
Hoek.assert(!err, err);
}
console.log('Server started @ ' + server.info.uri);
});
});