-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
201 lines (169 loc) · 4.63 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
193
194
195
196
197
198
199
200
201
var async = require('async');
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var r = require('rethinkdb');
var moment = require('moment')
var session = require('express-session');
var google = require('googleapis');
var everyauth = require('everyauth');
var freebase = require('freebase');
var glossary = require('glossary');
var config = require(__dirname + '/config.js');
var app = express();
//For serving the index.html and all the other front-end assets.
app.use(express.static(__dirname + '/static'));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({secret:'catdogdogcatcat', cookie:{maxAge:60000}}));
//The REST routes for lines.
app.route('/lines')
.get(listLines)
.post(createLine);
app.route('/lines/:id')
.get(getLine)
.put(updateLine)
.delete(deleteLine);
//If we reach this middleware the route could not be handled and must be unknown.
app.use(handle404);
//Generic error handling middleware.
app.use(handleError);
/*
* Retrieve all lines.
*/
function listLines(req, res, next) {
r.table('lines').orderBy({index: 'createdAt'}).run(req.app._rdbConn, function(err, cursor) {
if(err) {
return next(err);
}
//Retrieve all the lines in an array.
cursor.toArray(function(err, result) {
if(err) {
return next(err);
}
res.json(result);
});
});
}
/*
* Insert a new line.
*/
function createLine(req, res, next) {
var line = req.body;
line.createdAt = moment.utc().format();
console.dir(line);
r.table('lines').insert(line, {returnChanges: true}).run(req.app._rdbConn, function(err, result) {
if(err) {
return next(err);
}
res.json(result.changes);
});
}
/*
* Get a specific line.
*/
function getLine(req, res, next) {
var lineId = req.params.id;
r.table('lines').get(lineId).run(req.app._rdbConn, function(err, result) {
if(err) {
return next(err);
}
res.json(result);
});
}
/*
* Update a line.
*/
function updateLine(req, res, next) {
var line = req.body;
var lineId = req.params.id;
r.table('lines').get(lineId).update(line, {returnChanges: true}).run(req.app._rdbConn, function(err, result) {
if(err) {
return next(err);
}
res.json(result.changes[0].new_val);
});
}
/*
* Delete a line.
*/
function deleteLine(req, res, next) {
var lineId = req.params.id;
r.table('lines').get(lineId).delete().run(req.app._rdbConn, function(err, result) {
if(err) {
return next(err);
}
res.json({success: true});
});
}
//Page-not-found middleware.
function handle404(req, res, next) {
res.status(404).end('not found');
}
//generic error handling middleware.
function handleError(err, req, res, next) {
console.error(err.stack);
//send back a 500 page and log the error to the console.
res.status(500).json({err: err.message});
}
//store the db connection and start listening on a port.
function startExpress(connection) {
app._rdbConn = connection;
app.listen(config.express.port);
console.log('Listening on port ' + config.express.port);
}
//connect to rethinkdb, create the needed tables/indexes and then start express.
async.waterfall([
function connect(callback) {
r.connect(config.rethinkdb, callback);
},
function createDatabase(connection, callback) {
//Create the database if needed.
r.dbList().contains(config.rethinkdb.db).do(function(containsDb) {
return r.branch(
containsDb,
{created: 0},
r.dbCreate(config.rethinkdb.db)
);
}).run(connection, function(err) {
callback(err, connection);
});
},
function createTable(connection, callback) {
//Create the table if needed.
r.tableList().contains('lines').do(function(containsTable) {
return r.branch(
containsTable,
{created: 0},
r.tableCreate('lines')
);
}).run(connection, function(err) {
callback(err, connection);
});
},
function createIndex(connection, callback) {
//Create the index if needed.
r.table('lines').indexList().contains('createdAt').do(function(hasIndex) {
return r.branch(
hasIndex,
{created: 0},
r.table('lines').indexCreate('createdAt')
);
}).run(connection, function(err) {
callback(err, connection);
});
},
function waitForIndex(connection, callback) {
//Wait for the index to be ready.
r.table('lines').indexWait('createdAt').run(connection, function(err, result) {
callback(err, connection);
});
}
], function(err, connection) {
if(err) {
console.error(err);
process.exit(1);
return;
}
startExpress(connection);
});