forked from jessykate/geopad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
268 lines (234 loc) · 9.73 KB
/
app.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
var express = require('express');
var pg = require('pg');
var url = require('url'); // hack to workaround non-working POST request
var dateutils = require('date-utils');
var uuid = require('node-uuid');
var config = require('./config.js')
var cronJob = require('cron').CronJob;
/* * * * * * * * * * * * * * * * * * * * * *
* APP SETUP *
* * * * * * * * * * * * * * * * * * * * * */
// connect to postgres database
if (process.env.GEOPAD_DATABASE_URL) {
console.log("using process.env.GEOPAD_DATABASE_URL");
var client = new pg.Client(process.env.GEOPAD_DATABASE_URL);
} else {
var client = new pg.Client({user: config.db_user, database: config.db_name, password: config.db_password});
}
client.connect(function(err) { if(err) {console.log(err);}});
// create the express app
var pub = __dirname + '/media';
// setup middleware
var app = express();
app.use(app.router);
app.use(express.static(pub));
app.use(require('connect').bodyParser());
app.use(express.errorHandler());
app.use(express.favicon(__dirname + "/media/img/favicon.ico"));
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
io.set('log level', 2);
server.listen(3000);
// set default template engine to jade (note that express defaults to looking
// for view files in views/ directory under CWD, but set it here explicitly
// for clarity).
app.set('views', __dirname + '/views');
app.set("view engine", "jade");
// make the domain available to all templates (for connecting sockets)
app.locals.domain = config.domain;
/* * * * * * * * * * * * * * * * * * * * * *
* TASKS *
* * * * * * * * * * * * * * * * * * * * * */
// check for expired pads every hour on the 1st minute
hourly = '00 01 * * * *';
expiry_watch = new cronJob(hourly, function() {
console.log("checking for expired pads... ");
// check for
var in_the_next_hour = new Date();
in_the_next_hour.add({hours:1});
expiring_query = "select active.uuid, pad_meta.expiry from pad_meta, active WHERE pad_meta.uuid = active.uuid AND pad_meta.expiry <= $1";
expiring_args = [in_the_next_hour,];
client.query(expiring_query, expiring_args, function(err, result) {
if (result.rows.length > 0) {
// create a one-off scheduled job for each pad this hour that
// needs to be de-activated (also looks out for any
// previously expired pads that were missed for any reason).
var now = new Date();
for (idx in result.rows) {
pad = result.rows[idx];
console.log("a pad expiring this hour:");
console.log(pad);
if (pad.expiry <= now ) {
client.query("delete from active where uuid='" + pad.uuid + "';");
} else {
var at_expiry_time = pad.expiry;
console.log("scheduling expiry of pad " + pad.uuid + " at " + at_expiry_time);
new cronJob(at_expiry_time, function() {
client.query("delete from active where uuid='" + pad.uuid + "';");
}, true);
}
}
console.log(now.toFormat("YYYY-MM-DDTHH24:MI:SS") + ": scheduled removal of " + result.rows.length + " pads from active list.");
} else {
console.log((new Date()).toFormat("YYYY-MM-DDTHH24:MI:SS") + ": no pads expired at this time.");
}
});
}, null, true);
/* * * * * * * * * * * * * * * * * * * * * *
* ROUTES *
* * * * * * * * * * * * * * * * * * * * * */
app.get("/", function(req, res) {
res.render('home');
});
app.get("/api/pads/nearby/", function(req, res) {
// return pads where the current location falls within the covered area
// (origin + radius)
var data = url.parse(req.url, true);
console.dir("retrieving nearby pads with request data:");
console.dir(data);
client.query(
"SELECT * FROM pad_meta INNER JOIN active ON pad_meta.uuid = active.uuid where ST_Intersects('POINT(" + data.query.user_lng +
" " + data.query.user_lat + ")'::geometry, area) ORDER BY created DESC;", function(err, result) {
result.rows.forEach(function(item) {
if (item.expiry) {
item.expiry = item.expiry.toFormat("YYYY-MM-DDTHH24:MI:SS")
}
});
app.render('snippets/pad_list', {pads: result.rows}, function(err, html) {
console.log('retrieved nearby pads. sending list to client with user_id ' + data.query.user_id);
io.sockets.in(data.query.user_id).emit('padlist', html)
res.send(200);
})
});
});
app.get("/api/post/new", function(req, res) {
var data = url.parse(req.url, true);
var pad_id = data.query.pad_id;
var post_body = data.query.post_body;
var user_id = data.query.user_id;
var post_insert_string = "INSERT INTO pad_"+ pad_id +"(created, body, userid) VALUES (now(), $1, $2) RETURNING id, created, body, userid";
var post_insert_args = [post_body, user_id];
client.query(post_insert_string, post_insert_args, function(err, result) {
if (!err) {
app.render("snippets/post_detail", {post: result.rows[0] }, function(err, html) {
io.sockets.in(pad_id).emit('newpost-notify', html);
// need to close out the http request
res.send(200);
});
} else {
console.log(err);
res.send(500);
}
});
});
app.get("/api/pad/new", function(req, res) {
var data = url.parse(req.url, true);
var radius_meters = parseInt(data.query.pad_radius);
// because our lat and long are stored in degrees, we need to convert the
// radius in degrees too. one degree is approximately 111.12 kilometers or
// 111120 meters
var radius_degrees = radius_meters/111120;
// use a uuid for the table's primary key
var the_uuid = uuid.v4().replace(/-/g,'');
if (data.query.pad_expiry) {
var expiry_timestamp = new Date();
expiry_timestamp.setHours(expiry_timestamp.getHours() + parseInt(data.query.pad_expiry));
console.log(expiry_timestamp);
var padmeta_insert_string = "INSERT INTO pad_meta (uuid, created, updated, name, origin, radius, area, expiry) VALUES ($1, now(), now(), $2, ST_GeomFromText('POINT(" + data.query.user_long + " " + data.query.user_lat + ")', 26910), $3, ST_Buffer(ST_GeomFromText('POINT(" + data.query.user_long + " " + data.query.user_lat + ")'), $4), $5) RETURNING name, uuid, radius, expiry";
var padmeta_insert_args = [the_uuid, data.query.pad_name, radius_meters, radius_degrees, expiry_timestamp]
} else {
var padmeta_insert_string = "INSERT INTO pad_meta (uuid, created, updated, name, origin, radius, area) VALUES ($1, now(), now(), $2, ST_GeomFromText('POINT(" + data.query.user_long + " " + data.query.user_lat + ")', 26910), $3, ST_Buffer(ST_GeomFromText('POINT(" + data.query.user_long + " " + data.query.user_lat + ")'), $4)) RETURNING name, uuid, radius, expiry";
var padmeta_insert_args = [the_uuid, data.query.pad_name, radius_meters, radius_degrees]
}
client.query(padmeta_insert_string, padmeta_insert_args, function(err, result) {
if (!err) {
console.log("result of insert:");
console.log(result.rows[0]);
var pad_meta;
if (result.rows[0].expiry) {
pad_meta = {name: result.rows[0].name, uuid: result.rows[0].uuid, radius: result.rows[0].radius, expiry: result.rows[0].expiry.toFormat("YYYY-MM-DDTHH24:MI:S")}
} else {
pad_meta = {name: result.rows[0].name, uuid: result.rows[0].uuid, radius: result.rows[0].radius, expiry: result.rows[0].expiry}
}
var table_name = "pad_" + the_uuid;
var paddetail_insert_string = "CREATE TABLE IF NOT EXISTS "+ table_name +" (id SERIAL PRIMARY KEY, created TIMESTAMP, body TEXT, userid CHAR(32) )";
client.query(paddetail_insert_string, function(err, result) {
if (!err) {
var active_insert_string = "INSERT INTO active (uuid) VALUES ($1)";
var active_insert_args = [the_uuid];
client.query(active_insert_string, active_insert_args, function(err, result) {
if (!err) {
res.render('snippets/pad_meta', {pad: pad_meta}, function(err, html) {
console.log("pad created. html being sent:");
console.log(html);
io.sockets.in('home').emit('newpad-notify', html);
res.send(200);
});
} else {
console.log("error inserting into table 'active'");
console.log(err);
res.send(500);
}
});
} else {
console.log("error creating table 'pad_" + the_uuid +"'");
console.log(err);
res.send(500);
}
});
} else {
console.log("error inserting into table 'pad_meta'");
console.log(err);
res.send(500);
}
});
})
// must go *after* the /api/pad/new due to regex matching
app.get("/pad/:padid", function(req, res) {
var padid = req.params.padid;
console.log("retrieving pad " + padid);
client.query("select uuid, created, updated, name, ST_AsText(origin), radius, expiry, salt, password from pad_meta where uuid='"+ padid +"'", function (err, result) {
if (!err) {
// lazily assumes the row actually IS unique since it's supposed to
// be a UUID (should really double check).
var pad = result.rows[0];
if (pad.expiry) {
pad.expiry = pad.expiry.toFormat("YYYY-MM-DDTHH24:MI:SS");
}
console.log("obtained pad metadata:");
console.log(pad);
client.query("SELECT * FROM pad_" + padid + " ORDER BY created DESC;", function(err, result) {
if (!err) {
var posts = result.rows;
res.render('paddetail', {pad: pad, posts: posts});
} else {
console.log(err);
res.send(500);
}
});
} else {
// probably, pad does not exist
console.log(err);
res.send(500);
}
});
});
/************************************************
*
* socketsessss
*
*************************************************/
io.sockets.on('connection', function(socket) {
console.log("socket connected");
socket.on('join', function(room_name) {
console.log('client joining ' + room_name);
socket.join(room_name);
socket.in(room_name).emit('join-success-'+room_name)
});
});
/************************************************
*
* get the party started
*
*************************************************/
console.log("geopad server listening on port 3000");