-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
150 lines (133 loc) · 3.7 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
"use strict";
// Configuration
global.config = require("./config");
global.db = require("./databases/"+config.db_driver); // This is a bit of a hack and outdated way of doing things...
// Required node modules
var restify = require("restify");
var restifyOAuth2 = require("restify-oauth2");
var cluster = require('cluster');
var hooks = require("./hooks");
// Process variables
var server = restify.createServer({
name: "Very Simple Application Analytics Server",
version: "0.0.3",
formatters: {
"application/hal+json": function (req, res, body) {
return res.formatters["application/json"](req, res, body);
}
}
});
var port = config.listen; // Default port to listen
if (process.argc >= 2) {
port = parseInt(process.argv[2]); // Port can be also passed as an argument, this overrides config...
}
var RESOURCES = Object.freeze({
INITIAL: "/",
TOKEN: "/login",
EVENT: "/event",
TEST: "/test"
});
server.use(restify.authorizationParser());
server.use(restify.bodyParser({ mapParams: false }));
restifyOAuth2.cc(server, { tokenEndpoint: RESOURCES.TOKEN, hooks: hooks });
server.get(RESOURCES.INITIAL, function (req, res) {
var response = {
_links: {
self: { href: RESOURCES.INITIAL }
}
};
if (req.clientId) {
response._links["http://rel.example.com/event"] = { href: RESOURCES.EVENT };
} else {
response._links["oauth2-token"] = {
href: RESOURCES.TOKEN,
"grant-types": "client_credentials",
"token-types": "bearer"
};
}
res.contentType = "application/hal+json";
res.send(response);
});
server.post(RESOURCES.EVENT, function (req, res) {
if (!req.clientId) {
return res.sendUnauthenticated();
}
var fields = [
req.body.DeviceId,
req.body.Description,
req.body.ApiKey
];
var response = {
"success": "event recorded successfully",
_links: {
self: { href: RESOURCES.EVENT },
parent: { href: RESOURCES.INITIAL }
}
}
db.createEvent(fields, function (err, result) {
if (err) {
response = {
"error": "err",
_links: {
self: { href: RESOURCES.EVENT },
parent: { href: RESOURCES.INITIAL }
}
}
}
});
if (req.body.message_type === 'APPLICATION_EVENT_QUIT')
hooks.invalidateClientToken(req.authorization.credentials);
res.contentType = "application/hal+json";
res.send(response);
});
server.get(RESOURCES.TEST, function (req, res) {
var response = Math.floor((Math.random() * 100) + 1);
console.log(response);
if (response >= 50) {
db.createLog(function (err, result) {
if (err) {
response = {
"error": "err",
_links: {
self: { href: RESOURCES.EVENT },
parent: { href: RESOURCES.INITIAL }
}
}
}
});
}
else {
db.getLog(function (err, result) {
if (err) {
response = {
"error": "err",
_links: {
self: { href: RESOURCES.EVENT },
parent: { href: RESOURCES.INITIAL }
}
}
}
});
}
res.contentType = "application/hal+json";
res.send(response);
});
// Adding error information...
process.on('uncaughtException', function (err) {
console.log("UNCAUGHT EXCEPTION ");
console.log("[Inside 'uncaughtException' event] " + err.stack || err.message);
});
// Clustering to utilize all CPU cores
if (cluster.isMaster) {
// Fork workers
for (var i = 0; i < config.workers; i++) {
cluster.fork();
}
cluster.on('exit', function (worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
cluster.fork();
});
}
else {
server.listen(port);
}