forked from alombide/SoLoMIDEM_usecase_Cityroute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
189 lines (158 loc) · 6.37 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
/*
* @author: Andoni Lombide Carreton
* @copyright: SoLoMIDEM ICON consortium
*
* Code based on original implementation by Thomas Stockx, copyright OKFN Belgium
* See: https://github.com/oSoc13/Cityroute
*
* Node.js entry point
*
* This code assumes either a local deployment or a cloud hosting deployment: tested on Heroku.
* The database is a MongoDB hosted on MongoHQ. Swap the MongoHQ-specific code for code opening
* a connection to a local database for a pure local deployment.
*
*/
// declare external files
var WebSocketServer = require('ws').Server
var express = require("express");
var utils = require("./utils");
var moment = require('moment');
var users = require("./urlroutes/users");
var spots = require("./urlroutes/spots");
var routes = require("./urlroutes/routes");
var groups = require("./urlroutes/groups");
var messages = require("./urlroutes/messages");
var cultuurnet = require("./urlroutes/cultuurnet");
var irail = require("./urlroutes/irail");
var config = require("./auth/dbconfig.js");
// declare credentials
var databaseUrl = "CityRoute";
var collections = ["users", "routes", "messages"];
var db = require("mongojs").connect(databaseUrl, collections);
exports.db = db;
// use express and its bodyParser for POST requests.
var app = express();
app.use(express.bodyParser());
app.use(express.static(__dirname + '/clientpage'));
// prevent server death in case of uncaught exceptions
process.on('uncaughtException', function(exception) {
console.log(exception);
});
/**
* Check if there is a process.env db config (such as on Heroku) that stores the URL to the MongoDB.
* If not, use the direct URL to a mongoDB stored in the db config.
*/
var mongourl;
if (process.env.MONGOHQ_URL) {
mongourl = process.env.MONGOHQ_URL;
} else {
mongourl = config.mongourl;
}
exports.mongourl = mongourl;
// This function can be used to open a connection to the MongoDB.
// In case of a succesful connect or an error, the callback is called.
// In the first case the opened db is passed as a parameter.
function mongoConnectAndAuthenticate(callback) {
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(mongourl, function(err, db) {
// Maybe we should do this somewhere else, checking every single db connect for indexes
// is probably overkill.
(db.collection(config.groupscollection)).ensureIndex({
name: 1
}, function(err, idxName) {
(db.collection(config.collection)).ensureIndex({
startDate: 1,
endDate: 1
}, function(err, idxName) {
if (err) {
console.log(err);
}
callback(err, null, db);
});
});
});
}
exports.mongoConnectAndAuthenticate = mongoConnectAndAuthenticate;
// define the users API url routes.
app.post("/users/login", users.login);
app.get("/users/logout/:token", users.logout);
app.post("/users/profile", users.getProfile);
app.post("/cultuurnet/linkuitid", users.linkUitId);
app.post("/cultuurnet/onrequesttokenreceived", users.onRequestTokenReceived);
app.post("/cultuurnet/events", cultuurnet.getEventsByLatLong);
//app.get("/users/:key", users.dropAll);
app.post("/irail/stations", irail.getStationsInRadius);
app.get("/irail/stations-checkedin", irail.getStationsCheckedIn);
// define the spots API url routes.
app.get("/spots/details", spots.getSpotDetails);
app.get("/spots", spots.findSpotsByLatLong);
app.get("/spots/checkin", spots.checkIn);
//app.get("/spots/relevant", spots.findRelevantSpots);
app.get("/spots/search", spots.search);
app.get("/spots/findbyid", spots.findById);
app.get("/spots/usersnearby", spots.getCompleteGroupsForRouteStartingAtSpot);
// define the routes API url routes.
app.post("/routes/routesatspot", routes.findRoutesStartingAtSpot);
app.post("/routes", routes.addRoute);
app.get("/routes/generate/:channelname", routes.generateRoute);
app.get("/routes/generate", routes.generateRouteFromChannelArray);
app.get("/routes/find", routes.findById);
// define the groups API url routes.
app.post("/groups/id", groups.findById);
app.post("/groups/name", groups.findByName);
app.post("/groups/member", groups.findByMember);
app.post("/groups/addgroup", groups.addGroup);
app.post("/groups/deletegroup", groups.deleteGroup);
app.post("/groups/acceptmembershiprequest", groups.acceptMembershipRequest);
app.post("/groups/requestmembership", groups.addRequestingUser);
app.post("/groups/cancelmembershiprequest", groups.cancelMembershipRequest);
app.post("/groups/declinemembership", groups.declineRequestingUser);
app.post("/groups/removeuser", groups.removeUser);
app.post("/groups/profileformembership", groups.getProfileForMembership);
app.post("/messages/send", messages.sendMessage);
app.post("/messages/foruser", messages.getMessages);
app.post("/messages/sendtogroup", messages.sendMessageToGroup);
app.post("/messages/markasread", messages.markMessagesAsRead);
/*
Login status with linkID via Websockets using "socket.io" library
Use the server instance of Express such that sockets connect to the same port
*/
var linkidauth = require('./urlroutes/linkidauth.js');
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.get('/auth/success', linkidauth.onAuthSuccess);
app.get('/auth/irail', linkidauth.onConnectIrail);
io.on('connection', linkidauth.onConnection);
exports.io = io;
/*
Start server on port 8888
OR on the port in the cloud deployment config.
*/
server.listen(process.env.PORT || 8888);
console.log("Listening on port " + (process.env.PORT || 8888) + "...");
/* Messaging via Websockets using "ws" library */
var CONNECTED_WEBSOCKETS_UNREADMESSAGES = {};
function sendNumberOfUnreadMessages(userid, number) {
var socket = CONNECTED_WEBSOCKETS_UNREADMESSAGES[userid];
if (socket) {
CONNECTED_WEBSOCKETS_UNREADMESSAGES[userid].send("" + number);
};
}
exports.sendNumberOfUnreadMessages = sendNumberOfUnreadMessages;
var messagesWebSocketServer = new WebSocketServer({
port: (process.env.PORT || 5000)
});
console.log('Messages websocket server created');
messagesWebSocketServer.on('connection', function(ws) {
console.log("websocket connection opened.");
ws.send('new_messages_for_user?');
ws.on('message', function(userid) {
CONNECTED_WEBSOCKETS_UNREADMESSAGES[userid] = ws;
ws.on('close', function() {
delete CONNECTED_WEBSOCKETS_UNREADMESSAGES[userid];
});
messages.getNumberOfUnreadMessages(userid, function(number) {
sendNumberOfUnreadMessages(userid, number);
});
});
});