-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
180 lines (144 loc) · 5.1 KB
/
index.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
var fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser');
var handler_module = require('./lib/handler');
/*--------------- Express configuration goes here: -------------------*/
var app = express();
var port = process.env.PORT || 3302;
var host = process.env.HOST || "127.0.0.1";
app.use(
function crossOrigin(req,res,next){
res.header('Access-Control-Allow-Origin','*');
res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers','Content-Type, Authorization, Content-Length, X-Requested-With');
next(); //pass the controll to the next handler
}
);
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.listen(port, host, function() {
//configuration
handler = new handler_module.Handler("http://" + host + ":" + port);
console.log("Server configured");
console.log("Server listening to %s:%d", host, port);
});
/*--------------- HTTP Calls -------------------*/
var getDescriptions = function(req, res){
var uriPattern = new RegExp(req.params[0].replace(/\//g, '-').replace(/\d+/g, 'id') + '.*');
fs.readdir(__dirname + "/public/RESTdesc_descriptions/descriptions" , function (err, fileNames) {
if(err) throw err;
fileNames = fileNames.filter(function (file) { return file.match(uriPattern); })
.sort(function (a,b) { return b.length - a.length; });
if(!fileNames.length){
handler.StatusCode.notFound(function(jsonld_data){
res.send(jsonld_data);
});
return -1;
}
readFiles(__dirname + "/public/RESTdesc_descriptions/descriptions", fileNames, function (files) {
console.log("Send RESTdesc descriptions");
res.header('Content-Type', 'text/n3');
res.send(joinN3Documents(files));
});
});
}
var getEntryPoint = function(req, res){
handler.EntryPoint.getEntryPoint(function(jsonld_data){
res.send(jsonld_data);
});
}
var getCurrentWeather = function(req, res){
if(req.query.hasOwnProperty('city')){
var city = req.query.city;
console.log("I have received the city: " + city);
handler.StatusCode.notImplemented(function(jsonld_data){
res.send(jsonld_data);
});
}
else{
var latitude = req.query.lat;
var longitude = req.query.lon;
console.log("lat: " + latitude + " - lon: " + longitude);
handler.CurrentWeather.getByCoordinates(latitude, longitude, function(jsonld_data){
res.send(jsonld_data);
});
}
}
var getDailyForecastWeather = function(req, res){
var numberDays = req.params.days;
//MAX 1 WEEK!
if(numberDays > 8){
handler.StatusCode.notImplementedMoreThanOneWeek(function(jsonld_data){
res.send(jsonld_data);
});
return -1;
}
if(req.query.hasOwnProperty('city')){
var city = req.query.city;
console.log("I have received the city: " + city);
handler.StatusCode.notImplemented(function(jsonld_data){
res.send(jsonld_data);
});
}
else{
var latitude = req.query.lat;
var longitude = req.query.lon;
console.log("lat: " + latitude + " - lon: " + longitude);
handler.DailyForecastWeather.getByCoordinates(latitude, longitude, numberDays, function(jsonld_data){
res.send(jsonld_data);
});
}
}
var getHourlyForecastWeather = function(req, res){
var numberHours = req.params.hours;
//Max number of hours == 10
if(numberHours > 10){
handler.StatusCode.notImplementedMoreThanTenHours(function(jsonld_data){
res.send(jsonld_data);
});
return -1;
}
if(req.query.hasOwnProperty('city')){
var city = req.query.city;
console.log("I have received the city: " + city);
handler.StatusCode.notImplemented(function(jsonld_data){
res.send(jsonld_data);
});
}
else{
var latitude = req.query.lat;
var longitude = req.query.lon;
console.log("lat: " + latitude + " - lon: " + longitude);
handler.HourlyForecastWeather.getByCoordinates(latitude, longitude, numberHours, function(jsonld_data){
res.send(jsonld_data);
});
}
}
/*--------------- Helpers Functions -------------------*/
function readFiles(directory, fileNames, callback) {
var files = [];
fileNames.forEach(function (fileName) {
fs.readFile(directory + '/' + fileName, 'utf-8', function (err, data) {
files.push(data);
if(files.length == fileNames.length)
callback(files);
});
});
}
function joinN3Documents(documents) {
var namespaces = '', usedNamespaces = {}, triples = '',
match, prefixMatcher = /^@prefix.*\.$\n/gm;
documents.forEach(function (document) {
while((match = prefixMatcher.exec(document)) && (match = match[0]))
if(!usedNamespaces[match])
namespaces += (usedNamespaces[match] = match);
triples += document.replace(prefixMatcher, '');
});
return namespaces + triples;
}
/*--------------- Routes -------------------*/
app.options(/^\/([\d\w\/]*)$/, getDescriptions);
app.get("/", getEntryPoint);
app.get("/weather/current", getCurrentWeather);
app.get("/weather/forecast/days/:days", getDailyForecastWeather);
app.get("/weather/forecast/hours/:hours", getHourlyForecastWeather);