-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
154 lines (124 loc) · 4.9 KB
/
server.js
File metadata and controls
154 lines (124 loc) · 4.9 KB
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
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var existConfig = require('./app/model/exist');
var analytics = require('./app/model/analytics');
var proxy = require('express-http-proxy');
var PeriodicTask = require('periodic-task');
var moment = require('moment');
var google = require('googleapis');
var https = require("https");
//latest data
var analyticsMostVisited = {};
// add cross origin access
app.use(function(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var port = process.env.PORT || 9090; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// middleware to use for all requests
router.use(function(req, res, next) {
next(); // make sure we go to the next routes and don't stop here
});
// ----------------------------------------------------
// ROUTES
// ----------------------------------------------------
//Proxy al /db/digesto del exist de desarrollo
app.use('/desa/exist/*', proxy(existConfig.urlExistDesa, {
forwardPath: function(req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
return existConfig.pathExist + req._parsedUrl.path.replace('/desa/exist','')
}
}));
//Proxy al js de globales del exist de desarrollo
app.use('/desa/common/js/globals.js', proxy(existConfig.urlExistDesa, {
forwardPath: function(req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
return '/exist/rest/db' + req._parsedUrl.path.replace('/desa','')
}
}));
//Proxy al /db/digesto del exist de produccion
app.use('/exist/*', proxy(existConfig.urlExistProd, {
forwardPath: function(req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
return existConfig.pathExist + req._parsedUrl.path.replace('/exist','')
}
}));
app.use('/test/*', proxy(existConfig.urlExistProd, {
forwardPath: function(req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
return req._parsedUrl.path.replace('/test','');
}
}));
//Proxy al js de globales del exist de produccion
app.use('/common/js/globals.js', proxy(existConfig.urlExistProd, {
forwardPath: function(req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
return '/exist/rest/db' + req._parsedUrl.path
}
}));
app.use('/api/mostVisited', proxy(existConfig.urlExistProd, {
forwardPath: function(req, res) {
var concatenateLaws = function(laws){
var out = '';
for (i=0;i<laws.length;i++){
out += 'ley=' + laws[i] + '&';
}
return out;
};
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
return existConfig.pathExist + existConfig.details + '?' + concatenateLaws(analyticsMostVisited);
}
}));
// all of our routes will be prefixed with /api
//app.use('/api', router);
router.get('/mostVisited', function(req, res) {
res.send(analyticsMostVisited);
});
//Refresca informacion de google analytics cada hora
var task = new PeriodicTask(moment.duration(30,'minutes'), function () {
var jwtClient = new google.auth.JWT(
analytics.serviceAccount,
analytics.keyPath,
null,
analytics.scope);
jwtClient.authorize(function(err, tokens) {
if (err) {
console.log(err);
return;
}
var query = analytics.query + tokens.access_token;
//llama a la api de analytics
https.request(query, function(res){
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
if (response.rows !== undefined){
analyticsMostVisited = analytics.formatMostVisited(response.rows,existConfig);
}
});
}).on('error', function(err) {
console.log(err);
}).end();
});
});
task.run();
// START THE SERVER
// =============================================================================
app.listen(port);