forked from Georeactor/city-namer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (114 loc) · 3.53 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
const fs = require('fs');
// express setup
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const compression = require('compression');
const mongoose = require('mongoose');
const csrf = require('csurf');
// querying Overpass
const request = require('request');
// my MongoDB data
const Place = require('./models/place');
console.log('Connecting to MongoDB (required)');
mongoose.connect(process.env.MONGOLAB_URI || process.env.MONGODB_URI || 'localhost');
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express['static'](__dirname + '/static'));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(compression());
app.use(cookieParser());
app.use(session({
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
secret: process.env.SESSION || 'fj23f90jfoijfl2mfp293i019eoijdoiqwj129',
resave: false,
saveUninitialized: false
}));
const csrfProtection = csrf({ cookie: true });
// user registration and management
require('./login')(app, csrfProtection);
// task manager / projects interface
require('./project')(app, csrfProtection);
// Facebook Messenger bot interface
require('./bot')(app, csrfProtection);
// homepage for testing
app.get('/', (req, res) => {
res.render('index', {
user: req.user
});
});
// map for naming
app.get('/app', csrfProtection, (req, res) => {
res.render('naming', {
lat: 39.8985,
lng: 116.3989,
zoom: 12,
fromLanguages: ['es', 'fr'],
toLanguage: 'en',
endlang: 'English',
csrfToken: req.csrfToken()
});
});
// JSON to check which places are already named in the database
app.post('/named', csrfProtection, (req, res) => {
Place.find({ osm_id: { $in: req.body.osm_ids } }).select('osm_id').exec((err, places) => {
return res.json(err || places);
});
});
app.post('/overpass', csrfProtection, (req, res) => {
// this query should return a string with OSM XML
var query =
"node \
[place] \
[name] \
['name:TARGETLANG'!~'.'] \
(SOUTH,WEST,NORTH,EAST); \
(._;>;); \
out;";
query = query.replace('NORTH', req.body.north);
query = query.replace('SOUTH', req.body.south);
query = query.replace('EAST', req.body.east);
query = query.replace('WEST', req.body.west);
query = query.replace('TARGETLANG', req.body.targetLang);
request('http://overpass-api.de/api/interpreter?data=' + query.replace(/\s+/g, ''), (err, resp, body) => {
if (err) {
console.log('Overpass API error');
return res.json(err);
}
res.send(body);
});
});
// save a suggested place-name translation to the database
app.post('/name', csrfProtection, (req, res) => {
if (!req.user) {
return res.redirect('/login');
}
var p = new Place({
user_id: req.user._id,
osm_user_id: req.user.osm_id,
osm_place_id: req.body.osm_place_id,
name: req.body.name,
suggested: req.body.suggested,
language: req.body.language,
saved: new Date()
});
p.save((err) => {
return res.json(err || { status: 'success', _id: p._id });
});
});
// browse to verify everything makes sense
app.get('/names', csrfProtection, (req, res) => {
Place.find({}).sort('-saved').limit(10).exec( (err, places) => {
return res.json(err || places);
});
});
app.listen(process.env.PORT || 8080, () => {
console.log('app is running');
});
module.exports = app;