forked from jurisv/WebRTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
193 lines (164 loc) · 6.41 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var fs = require('fs'), // file system
bodyParser = require('body-parser'), // used for POST and QueryString Parsing
nconf = require('nconf'), // node config Key/Value pairs
express = require('express'), // The web routing engine and framework
moment = require('moment'), // moment is a friendly time library
ejs = require('ejs'), // ejs is a template engine for JSON to HTML
wrap = require('./lib/wrapjsonresponse.js'),
data,
firebase,
serverConfig;
if(process.env.NODE_ENV == 'production'){
nconf.argv().env().file( __dirname + '/server-config.json'); // path to config JSON
}else{
nconf.argv().env().file( __dirname + '/local-server-config.json'); // path to config JSON
}
serverConfig = nconf.get("ServerConfig"); // load environment config
var app = module.exports = require('express')(); // Setup express app
//to parse form body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
if(nconf.get('adminsettings')['serviceprovider'] != undefined){
data = require('./lib/data')(nconf); // load data package for routes
}
// simple logger placing first and using next()
// allows this to run as well as other matching methods.
if(serverConfig.logAllCalls) {
app.use(function (req, res, next) {
console.log('%s %s', req.method, req.url);
// logger.info('%s %s', req.method, req.url);
next();
});
}
//All browser crashes are logged to here.
app.route('/crashlog')
.get(function(req, res, id) {
res.status(200).send(null);
});
//Route all data related calls as a single route with an id or not
app.route('/data/:store/:id')
.get(function(req, res, id) {
var store = 'get'+ req.params.store;
data[store](req,res, req.params.id);
})
.post(function(req, res, id) {
var store = 'add'+ req.params.store;
data[store](req,res, req.params.id);
})
.put(function(req, res, id) {
var store = 'set'+ req.params.store;
data[store](req,res, req.params.id);
})
.delete(function(req, res) {
var store = 'delete'+ req.params.store;
data[store](req,res);
});
app.route('/data/:store')
.get(function(req, res) {
var store = 'get'+ req.params.store;
data[store](req,res);
})
.post(function(req, res) {
var store = 'add'+ req.params.store;
data[store](req,res);
})
.put(function(req, res) {
var store = 'set'+ req.params.store;
data[store](req,res);
})
.delete(function(req, res) {
var store = 'delete'+ req.params.store;
data[store](req,res);
});
//There's only one config but the REST UI sends an id so just ignore it.
app.route('/config/:id')
.get(function(req, res) {
var config = nconf.get('adminsettings'),
data;
//Get only sends public data
data=config;
// For demo don't send live keys
// todo: remove later
data={
"serviceprovider": {
"opentok": {
"otDefaultSessionId": "",
"ApiKey": "yourkeyhere",
"SecretKey": "yoursecrethere"
},
"firebase": {
"Url": process.env.FirebaseUrl || config.serviceprovider.firebase.Url ,
"ApiKey": "yourkeyhere",
"SecretKey": "yoursecrethere"
}
}};
res.status(200).send(wrap(data));
})
.post(function(req, res) {
var data = req.body, // JSON.parse(req.body.data),
config = nconf.get('adminsettings');
if(!config.serviceprovider.firebase.Url && data){
nconf.set('adminsettings', data);
nconf.save();
}
res.status(200).send(wrap(data));
})
.put(function(req, res) {
var data = req.body;
//for demo don't save
// app.config.set('adminsettings', data);
// app.config.save();
res.status(200).send(wrap(data));
})
.delete(function(req, res) {
var data = JSON.parse(req.body.data);
res.status(200).send(wrap(data));
});
app.use(express.static( __dirname + serverConfig.webRoot));
//server side compression of assets
if(serverConfig.enableCompression){
var compress = require('compression');
app.use(compress());
}
//CORS Supports
if(serverConfig.enableCORS){
app.use( function(req, res, next) {
res.header('Access-Control-Allow-Origin', serverConfig.AccessControlAllowOrigin); // allowed hosts
res.header('Access-Control-Allow-Methods', serverConfig.AccessControlAllowMethods); // what methods should be allowed
res.header('Access-Control-Allow-Headers', serverConfig.AccessControlAllowHeaders); //specify headers
res.header('Access-Control-Allow-Credentials', serverConfig.AccessControlAllowCredentials); //include cookies as part of the request if set to true
res.header('Access-Control-Max-Age', serverConfig.AccessControlMaxAge); //prevents from requesting OPTIONS with every server-side call (value in seconds)
if (req.method === 'OPTIONS') {
res.send(204);
}
else {
next();
}
});
}
// middleware with an arity of 4 are considered
// error handling middleware. When you next(err)
// it will be passed through the defined middleware
// in order, but ONLY those with an arity of 4, ignoring
// regular middleware.
app.use(function(err, req, res, next){
// whatever you want here, feel free to populate
// properties on `err` to treat it differently in here.
res.status(err.status || 500).send({ error: err.message });
});
// our custom JSON 404 middleware. Since it's placed last
// it will be the last middleware called, if all others
// invoke next() and do not respond.
app.use(function(req, res){
var template = fs.readFileSync(__dirname + '/views/404.html', 'utf8');
var body = ejs.render(template, {
filename: __dirname + '/views/404.html',
title: 'Not Found'
});
res.status(404).send(body) ;
});
var http = require('http').Server(app); // http on top of express for websocket handling
http.listen(process.env.PORT || serverConfig.port);
if(nconf.get('adminsettings')['serviceprovider'] != undefined){
require('./lib/sockets')(http, nconf); // seperate module for all websocket requests
}