-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.js
More file actions
205 lines (175 loc) · 5.59 KB
/
Copy pathfrontend.js
File metadata and controls
205 lines (175 loc) · 5.59 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
/*---------------
LOAD DEPENDENCIES
---------------*/
var express = require('express');
app = express();
//Middleware to use POSTs
var bodyParser = require('body-parser');
//Custom body handling middleware
var handleBody = function(req,res,next) {
if (req.path.match(/\/(s|slice)\//)) { //If we're just going to send this to a slice, we don't want to parse the body--we want to pass on the raw body
req.rawBody = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
req.rawBody += chunk;
});
req.on('end', function() {
next(); //Only go on to the next middleware after we've received all of the data
});
} else {
bodyParser()(req,res,next); //Otherwise, we want to parse the body for use
}
}
app.use(handleBody);
//We use www as the containing folder for all front-facing webserver files
app.use(express.static(__dirname + '/www/public'))
app.set('views', __dirname + '/www/views');
app.set('view engine', 'jade');
app.use(require('static-favicon')((__dirname + '/www/public/images/geni.ico')));
var url = require('url');
var fs = require('fs');
var nconf = require('nconf');
/*---------------
SERVER CONFIG
---------------*/
nconf.argv().file('./config.json');
nconf.defaults({
auth_key: 0,
ip: 'localhost',
port: 80,
db_url: 'mongodb://db-admin:hellokitty@ds031087.mongolab.com:31087/rp-slivers'
});
//GENI-specific modules
var db = require('./db_api').db(nconf.get('db_url'));
var backend = require('./backend').backend(db);
/*---------------
ROUTES
---------------*/
//Use a route with get
app.get('/:type(s|slice)/:key*', function(req, res) {
var path;
req.params[0] == undefined ? path = '/' : path = req.params[0];
backend.get(req.params.key, req, res, path);
});
//Use of route with post
app.post('/:type(s|slice)/:key*', function(req, res){
var path;
req.params[0] == undefined ? path = '/' : path = req.params[0];
backend.post(req.params.key, req, res, path);
});
//Basic statistics and landing page
app.get('/', function(req, res) {
db.getAllData(function(items) {
items = appendURLs(items);
res.render('index', {items: items});
});
});
//List all local IP/slice name pairs
app.get('/list', function(req,res) {
db.getAllData(function(items) {
items = appendURLs(items);
res.render('list', {items: items});
});
});
/*---------------
API ROUTES
---------------*/
//List through API
app.get('/api/list', function(req, res) {
db.getAllData(function(items) {
res.json({items:items, worked: true});
});
});
//Remove through API
app.post('/api/remove', function(req, res) {
if (req.body.key){
if (req.body.auth_key) {
if (req.body.auth_key == auth_key) {
backend.removeRoute(req.body.key, function(worked) {
if (worked)
message = 'Successfully removed ' + req.body.key + '.';
else
message:'Could not remove ' + req.body.key + '.';
res.json({message:message, key:req.body.key, worked:worked});
});
} else {
res.json({message:'Could not remove ' + req.body.key + '--auth key not valid.', key:req.body.key, worked: false}); //Wrong auth key
}
} else {
res.json({message:'Could not remove ' + req.body.key + '--no auth key received.', key:req.body.key, worked: false}); //No auth key provided
}
} else {
res.json({message:'Could not remove ' + req.body.key + '.', key:req.body.key, worked: false}); //Key not received/not valid
}
});
//Add through API
app.post('/api/add', function(req,res) {
if (req.body.ip && req.body.key) {
if (req.body.auth_key) {
if (req.body.auth_key == auth_key) {
var newObj = {key:req.body.key, ip:req.body.ip, des:''};
backend.addRoute(newObj, function(worked) {
if (worked)
message = 'Successfully added ' + req.body.key + ' with an IP of ' + req.body.ip + '.';
else
message = 'Could not add ' + req.body.key + ' with an IP of ' + req.body.ip + '.'
res.json({message: message, key:req.body.key, ip: req.body.ip, worked:worked});
});
} else {
res.json({message:'Could not add ' + req.body.key + ' with an IP of ' + req.body.ip + '--auth key not valid.', key:req.body.key, ip:req.body.ip, worked: false}); //Wrong auth key
}
} else {
res.json({message:'Could not add ' + req.body.key + ' with an IP of ' + req.body.ip + '--no auth key received.', key:req.body.key, ip:req.body.ip, worked: false}); //No auth key provided
}
} else {
res.json({message: 'Could not add ' + req.body.key + ' with an IP of ' + req.body.ip + '.', key:req.body.key, ip:req.body.ip, worked: false}); //Key or IP not received/not valid
}
});
//Invalid API request--GET
app.get('/api/*', function (req, res) {
res.status(404);
res.json({error: 'No valid API request given.', worked: false});
});
//Invalid API request--POST
app.post('/api/*', function (req, res) {
res.status(404);
res.json({error: 'No valid API request given.', worked: false});
});
//404 handling
app.use(function(req, res, next){
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.render('404');
return;
}
// respond with json
if (req.accepts('json')) {
res.json({ error: '404 - Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
/*---------------
SERVER START
---------------*/
var auth_key = nconf.get('auth_key');
var ip = nconf.get('ip');
var port = nconf.get('port');
console.log('Listening on ' + ip + ':' + port);
app.listen(port, ip);
/*---------------
HELPER FUNCTIONS
---------------*/
//Function to append a URL to the route object
function appendURLs(items) {
for (i in items) {
items[i].url = generateURL(items[i].key);
}
return items;
}
//Generate a URL for a route
function generateURL(key) {
return ('/s/' + key);
}