-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
executable file
·125 lines (101 loc) · 2.69 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
var project = require('pillars'),
Scheduled = require("scheduled"),
GDB = require("goblindb"),
acusticData = require('./scheduled_tasks/acustic_data'),
fluData = require('./scheduled_tasks/flu_data'),
pollenData = require('./scheduled_tasks/pollen_data'),
pollutionData = require('./scheduled_tasks/pollution_data'),
stationsData = require('./scheduled_tasks/station_data'),
weatherData = require('./scheduled_tasks/weather_data'),
apiManagement = require('./routes/api');
/* --- Goblin Setup --- */
var goblinDB = GDB();
/* --- Manual Recovery --- */
if(process.argv[2] && process.argv[2] === "-clean"){
require('./wakeup')(goblinDB);
}
/* --- HTTP SERVER --- */
// Starting the project
project.services.get('http').configure({
port: process.env.PORT || 3000
}).start();
// Routes definition
var apiDetails = new Route({
id: 'apidetails',
path: 'api/*:version',
cors: true
}, function(gw) {
// to -> Documentation
gw.redirect("http://docs.airemad.apiary.io/");
});
var apiRoutes = new Route({
id: 'apiRoutes',
path: 'api/v1/:path/*:id',
cors: true,
method: "GET"
}, function(gw) {
apiManagement(gw, goblinDB);
});
var rootRoutes = new Route({
id: 'rootRoutes',
path: '/*',
cors: true,
method: "GET",
directory: {
path: './public/index.html',
listing: true
}
});
var staticFiles = new Route({
id: 'estatics',
path: '/*:path',
directory: {
path: './public',
listing: true
}
});
// Adding Routes to Pillars
project.routes.add(apiRoutes);
project.routes.add(apiDetails);
project.routes.add(rootRoutes);
project.routes.add(staticFiles);
/* --- CRON Tasks --- */
// Every hour
var everyHourTasks = new Scheduled({
id: "everyHourTasks",
pattern: "0 * * * * *", // At xx:00
task: function() {
pollutionData(goblinDB);
}
}).start();
// Every 2 hours...
var everyTwoHoursTasks = new Scheduled({
id: "everyTwoHoursTasks",
pattern: "0 */2 * * * *", // Every two hours
task: function() {
weatherData(goblinDB);
}
}).start();
// Every Day
var everyDayTasks = new Scheduled({
id: "everyWorkingDayTasks",
pattern: "0 8 * * * *", // Every Day 08:00
task: function() {
pollenData(goblinDB); // Not sure about weekends support
acusticData(goblinDB);
}
}).start();
// Every Week
var everyWeekTasks = new Scheduled({
id: "everyWeekTasks",
pattern: "0 8 * * 1 *", // Monday 08:00
task: function() {
fluData(goblinDB);
}
}).start();
/* --- INITIAL Tasks --- */
stationsData(goblinDB);
everyHourTasks.launch();
everyTwoHoursTasks.launch();
everyDayTasks.launch();
everyWeekTasks.launch();