This repository has been archived by the owner on Nov 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
index.js
executable file
·125 lines (82 loc) · 2.4 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
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
/*
* Copyright (c) 2011 Eran Hammer-Lahav. All rights reserved. Copyrights licensed under the New BSD License.
* See LICENSE file included with this code project for license terms.
*/
// Load modules
var Hapi = require('hapi');
var Db = require('./db');
var Stream = require('./stream');
var Config = require('./config');
var Session = require('./session');
var Routes = require('./routes');
var Suggestions = require('./suggestions');
var Tips = require('./tips');
var Vault = require('./vault');
// Declare internals
var internals = {};
// Post handler extension middleware
internals.onPostHandler = function (request, next) {
if (request.response &&
request.response.result) {
var result = request.response.result;
// Sanitize database fields
if (result._id) {
result.id = result._id;
delete result._id;
}
if (result instanceof Object) {
for (var i in result) {
if (result.hasOwnProperty(i)) {
if (i[0] === '_') {
delete result[i];
}
}
}
}
}
next();
};
// Catch uncaught exceptions
process.on('uncaughtException', function (err) {
Hapi.Utils.abort('Uncaught exception: ' + err.stack);
});
var configuration = {
name: 'http',
// Extension points
ext: {
onPostHandler: internals.onPostHandler
},
// Authentication
authentication: {
loadClientFunc: Session.loadClient,
loadUserFunc: Session.loadUser,
extensionFunc: Session.extensionGrant,
checkAuthorizationFunc: Session.checkAuthorization,
aes256Keys: {
oauthRefresh: Vault.oauthRefresh.aes256Key,
oauthToken: Vault.oauthToken.aes256Key
},
tos: {
min: '20110623'
}
},
debug: true
};
var server = new Hapi.Server(Config.host.api.domain, Config.host.api.port, configuration);
server.addRoutes(Routes.endpoints);
// Initialize database connection
Db.initialize(function (err) {
if (err === null) {
// Load in-memory cache
Suggestions.initialize();
Tips.initialize();
// Start Server
server.start();
Stream.initialize(server.listener);
}
else {
// Database connection failed
Hapi.Log.event('err', err);
process.exit(1);
}
});