-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchimney-swap-server.js
407 lines (351 loc) · 9.76 KB
/
chimney-swap-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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
var express = require('express');
var https = require('https');
var bodyParser = require('body-parser');
var passport = require('passport');
var session = require('express-session');
var multipart = require('connect-multiparty')({uploadDir: __dirname+'/tmp'});
var config = require(__dirname+'/config.json');
var path = require('path');
var fs = require('fs');
var GOOGLE_CONSUMER_KEY = config.apiKey;
var GOOGLE_CONSUMER_SECRET = config.apiSecret;
var PATHFINDER_ID = config.pathfinderId;
var DOMAIN_NAME = config.domainName;
var PORT = config.port;
var SERVER_URL = "http://"+DOMAIN_NAME;
var app = express();
function geocode(address,done,error){
console.log("geocoding address: "+address);
https.request({
host:'maps.googleapis.com',
path:'/maps/api/geocode/json?address='+encodeURIComponent(address),
method:'GET'
}, function(response){
var str = '';
response.on('data',function(dat){
str += dat;
});
response.on('end',function(){
var data = JSON.parse(str);
console.log(data);
var pos = data.results[0].geometry.location
done(pos);
});
response.on('error',function(err){
error(err);
});
}
).end();
}
var database = {
users:{},
chimneys:{},
sharedChimneys:{}
}
function Record(table,constructor){
this.table = table;
}
Record.prototype.insert = function(){
this.table[this.id] = this;
this.insertCascade();
return this;
};
Record.prototype.insertCascade = function(){};
Record.prototype.remove = function(){
delete this.table[this.id];
this.deleteCascade();
return this;
};
Record.prototype.deleteCascade = function(){};
function User(profile, idToken){
var rec = this.table[this.id];
if(rec){
rec.firstName = profile.name.givenName;
rec.lastName = profile.name.familyName;
rec.email = profile.email;
return rec;
}
this.id = profile.id;
this.firstName = profile.name.givenName;
this.lastName = profile.name.familyName;
this.email = profile.email;
this.idToken = idToken;
this.chimneys = {};
}
User.prototype = new Record(database.users);
function homeView(session, tab, swap){
console.log(session);
var obj;
if(session && session.passport && session.passport.user){
obj = database.users[session.passport.user].homeView(tab, swap);
} else {
obj = {
chimneys:database.chimneys,
theme:'slate',
tab:tab || 'search',
pathfinderId: PATHFINDER_ID,
idToken: session.idToken
};
}
if(swap && swap.theirs){
obj.swap = {
mine: database.chimneys[swap.mine],
theirs: database.chimneys[swap.theirs]
};
var mine = database.chimneys[swap.mine];
var theirs = database.chimneys[swap.theirs];
if(mine){
mine.remove();
}
if(theirs){
theirs.remove();
}
}
return obj;
}
User.prototype.homeView = function(tab){
return {
myChimneys:database.users[this.id].chimneys,
chimneys:database.chimneys,
pathfinderId: PATHFINDER_ID,
idToken: this.idToken,
theme:'slate',
tab:(tab || 'search')
};
};
function Chimney(name, user, position){
this.id = null;
this.name = name;
this.user = user;
this.position = position;
}
Chimney.prototype = new Record(database.chimneys);
Chimney.nextId = 1;
Chimney.prototype.insert = function(){
if(this.id === null){
this.id = Chimney.nextId++;
}
console.log('inserting new chimney with id: '+this.id);
var ret = Record.prototype.insert.call(this);
ret.share();
return ret;
};
Chimney.prototype.remove = function(){
delete database.sharedChimneys[this.id];
if(this.user){
delete this.user.chimneys[this.id];
}
return Record.prototype.remove.call(this);
};
Chimney.prototype.insertCascade = function(){
if(this.user){
this.user.chimneys[this.id] = this;
}
};
Chimney.prototype.resource = function(){
if(this.id === null)
throw new Error("Can't create resource, chimney has no id");
var obj = {
id: this.id,
name: this.name,
position: this.position,
address: this.address,
image: this.imageUrl()
};
if(this.user){
obj.userId = this.user.id;
}
return JSON.stringify(obj);
};
Chimney.prototype.imageUrl = function(){
return SERVER_URL+'/images/'+this.id+'.png';
};
Chimney.prototype.imagePath = function(){
return __dirname+'/images/'+this.id+'.png';
};
Chimney.prototype.share = function(){
database.sharedChimneys[this.id] = this;
};
Chimney.prototype.unshare = function(){
delete database.sharedChimneys[this.id];
};
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
done(null, database.users[id]);
});
function getId(req,res,next){
if(!req.session.idToken){
if(req.query.id_token){
req.session.idToken = req.query.id_token;
next();
return;
}
res.redirect(
'https://auth.thepathfinder.xyz/auth/google?return_url='+encodeURIComponent(req.protocol+'://'+req.hostname+':'+PORT+req.path)
);
return
}
next();
}
app.use(bodyParser.json({limit:'50mb'}));
app.use(bodyParser.urlencoded());
app.use(session({
secret:'shh! this is a secret',
resave:false,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/images', express.static(__dirname + '/images'));
app.get('/logout',function(req, res){
req.logout();
req.redirect('/');
});
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.get('/', function(req, res){
res.render('index',{theme:"slate"});
});
app.get('/home', getId, function(req,res){
var tab = req.param('tab') || 'search';
var swap = req.param('swap');
var mine = req.param('mine');
res.render('home',homeView(req.session, tab,{
mine: mine, theirs: swap
}));
});
app.get('/sign-out', function(req,res){
delete req.session;
res.redirect('/');
});
app.post('/swap', getId, function(req,res){
var mine = req.param('mine');
var theirs = req.param('theirs');
res.render('home',homeView(req.session,'swap',{mine:mine,theirs:theirs}));
});
app.post('/chimney', getId, multipart, function (req, res) {
var user = null;
if(req.session && req.session.passport && req.session.passport.user){
user = database.users[req.session.passport.user];
}
var address = req.param('address');
var latitude = req.param('latitude');
var longitude = req.param('longitude');
var position = req.param('position');
var name = req.param('name');
var img = req.param('image');
var tab = req.param('tab');
var swap = req.param('swap');
function makeChimney(position){
console.log("Sample application trying to make chimney in makeChimney at position: "+position);
var chimney = new Chimney(name, user, position);
chimney.insert();
var targetPath = path.resolve(chimney.imagePath());
console.log(req.files);
if(img){
console.log("WRITING IMAGE");
fs.writeFile(targetPath, img.file_data, 'base64', function(err){
if(err) {
console.log(err);
res.setHeader('Content-Type', 'text/plain');
res.status(400).send('bad image');
} else if(tab){
res.redirect('/home?tab='+tab+'&swap='+swap+'&mine='+chimney.id);
return;
} else
res.status(201).send();
});
} else if (req.files.image) {
var tempPath = req.files.image.path;
fs.rename(tempPath, targetPath, function(err) {
if (err) {
console.log(err);
res.setHeader('Content-Type', 'text/plain');
res.status(400).send('bad image');
fs.unlink(tempPath)
} else if(tab){
res.redirect('/home?tab='+tab+'&swap='+swap+'&mine='+chimney.id);
return;
} else
res.status(201).send()
});
} else {
res.setHeader('Content-Type', 'text/plain');
res.status(400).send('no images');
}
}
if(latitude){
if(longitude){
makeChimney({lat: latitude, lng: longitude});
} else {
res.setHeader('Content-Type', 'text/plain');
req.status(400).send('no longitude provided');
return;
}
} else if(position){
makeChimney(position);
} else if(address){
geocode(address, makeChimney, function(err){
console.error(err);
res.setHeader('Content-Type', 'text/plain');
res.status(400).send("invalid address: "+address);
});
} else {
res.setHeader('Content-Type', 'text/plain');
res.status(400).send("Address or Position Required");
}
});
app.get('/chimney', function (req, res){
console.log("GET WAS CALLED");
var id = req.param('id');
var chimney = database.chimneys[id];
if(!chimney){
res.setHeader('Content-Type', 'text/plain');
res.status(404).send("Chimney Not Found");
return;
}
res.setHeader('Content-Type', 'application/json');
res.status(200).send(chimney.resource());
});
app.delete('/chimney', function(req, res){
var id = req.param('id');
if(!id){
res.setHeader('Content-Header', 'text/plain');
res.status(401).send('deleting a chimney requires an id');
return;
}
var chimney = database.chimneys[id];
if(chimney){
chimney.remove();
res.status(200).send();
return;
}
res.setHeader('Content-Header', 'text/plain');
res.status(404).send("No chimney with id: "+id);
});
app.get('/chimneys', function (req, res){
res.setHeader('Content-Type', 'application/json');
res.status(200).send(
'['+Object.keys(database.sharedChimneys).map(
function(k){
return database.sharedChimneys[k].resource();
}
).join(',')+']'
);
});
app.use('/', express.static(__dirname + '/public'));
app.listen(PORT);
console.log("started server on: "+SERVER_URL);
var user = new User({"id":"109986445607396986536", "name":{"givenName":"Dan","familyName":"Hanson"}, "email":"danielghanson93@gmail.com"}).insert();
var pos = {lat:39.44,lng:-87.34};
var x = new Chimney("Red Chimney", user, pos).insert();
x = new Chimney("Silver Stack", user, pos).insert();
x = new Chimney("Cinder Smokeshaft", null, pos).insert();
x = new Chimney("Amber Pillar", null, pos).insert();
x = new Chimney("Bricked Square Shaft", null, pos).insert();
x = new Chimney("Victorian Ventilator", null, pos).insert();
x = new Chimney("Chimney Rock", null, pos).insert();
x = new Chimney("Twisted Tube", null, pos).insert();
x = new Chimney("Cubed Chimney", null, pos).insert();