-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
244 lines (198 loc) · 7.94 KB
/
dev-server.js
File metadata and controls
244 lines (198 loc) · 7.94 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* dev-server - serves static resources for developing "earth" locally
*/
"use strict";
console.log("============================================================");
console.log(new Date().toISOString() + " - Starting");
const open = require('open');
var util = require("util");
var nc = require("./public/libs/earth/1.0.0/nomadsClient.js");
const url = require('url');
var express = require("express");
const fs = require("fs");
const querystring = require('querystring');
const Server = require('http').Server;
const path = require('path');
const { tryToDownloadCurrentData } = require('./public/libs/earth/1.0.0/nomadsClient.js');
const port = process.env.PORT || 3000;
/**
* Returns true if the response should be compressed.
*/
function compressionFilter(req, res) {
return (/json|text|javascript|font/).test(res.getHeader('Content-Type'));
}
/**
* Adds headers to a response to enable caching.
*/
function cacheControl() {
return function(req, res, next) {
res.setHeader("Cache-Control", "public, max-age=300");
return next();
};
}
function logger() {
express.logger.token("date", function() {
return new Date().toISOString();
});
express.logger.token("response-all", function(req, res) {
return (res._header ? res._header : "").trim();
});
express.logger.token("request-all", function(req, res) {
return util.inspect(req.headers);
});
return express.logger(
':date - info: :remote-addr :req[cf-connecting-ip] :req[cf-ipcountry] :method :url HTTP/:http-version ' +
'":user-agent" :referrer :req[cf-ray] :req[accept-encoding]\\n:request-all\\n\\n:response-all\\n');
}
var app = express();
const server = new Server(app);
app.use(cacheControl());
app.use(express.compress({filter: compressionFilter}));
app.use(logger());
app.use(express.static("./public"));
server.listen(port, () => {
console.log("Server at " + port);
});
app.use('/', express.static(getDir() + '/public'));
app.get('/', function(req, res) {
res.sendFile(getDir() + '/public/index.html');
});
app.get('/download', (req, res) => {
var parsedUrl = url.parse(req.url);
var parsedQs = querystring.parse(parsedUrl.query);
console.log('parsedQs=' +JSON.stringify(parsedQs));
if (!parsedQs.date || !parsedQs.time || !parsedQs.level) {
res.status(400);
res.send('Wrong parameters of the GET request. Call this endpoint with following parameters: ?date=[YYYYMMDD]&time=[XXXX]&level=[XX]');
}
nc.downloadAndSaveNomadData(parsedQs.date,parsedQs.time,parsedQs.level,res);
});
app.get('/data/weather/current/:file', (req, res) => {
console.log("Trying to load current file data file");
console.log(req.params.file);
var level = req.params.file.split('-')[3].replace('hPa','');
var variable = req.params.file.split('-')[1];
var supposedCurrentPath = pathFromDate(level,variable);
try {
console.log('File path ' + supposedCurrentPath);
if (fs.existsSync(supposedCurrentPath)) {
console.log(supposedCurrentPath +' File exists. Sending it.');
res.status(200);
res.send(fs.readFileSync(supposedCurrentPath, 'utf8').toString());
} else { //download current date
//nc.downloadAndSavaNomadData()
var NDateObj = nc.toNOMADSDate();
var date = NDateObj.year+NDateObj.month+NDateObj.day;
nc.tryToDownloadCurrentData(date,NDateObj.time,level,variable,5,res);
}
} catch (e) {
console.log("Error reading current file " + req.params.file + " from disk. " + e);
res.status(500);
res.send("Error reading current file " + req.params.file + " from disk. " + e);
}
});
/**
* Returns path to level's current .json datafile according to given date, if date is null current datetime is used
* @param {*} level
* @param {*} variable
* @param {*} date
*/
function pathFromDate(level,variable,date = null) {
if (!date) date = new Date();
var NDateObj = nc.toNOMADSDate(date);
var path = "./data/weather/";
path += NDateObj.year + "/" + NDateObj.month + "/" + NDateObj.day + "/" + NDateObj.time+"-"+variable+"-isobaric-"+level+"hPa-gfs-1.0.json";
return path;
}
app.get('/data/weather/:year/:month/:day/:file', (req, res) => {
console.log('/data/weather/:year/:month/:day/:file');
console.log(req.params);
if (req.params.month > 12 || req.params.day > 31) {
res.status(400);
res.send('Bad request. Day or month out of bounds.');
}
var path = "/data/weather/" + req.params.year + "/" + req.params.month + "/" + req.params.day + "/" + req.params.file;
console.log('Trying to load ' + process.cwd()+path);
try {
if (fs.existsSync(process.cwd()+path)) {
console.log('File exist');
fs.readFile(process.cwd()+path, function(err,data) {
if(err) {
console.log('Err while sending data file ' + err);
res.status(500);
res.send('Err while sending data file ' + err);
} else {
console.log('OK');
res.status(200);
res.send(data.toString());
}
});
} else {
console.log('File doesn\'t exist');
var date = req.params.year+req.params.month+req.params.day;
var fileNameParts = req.params.file.split("-");
const PRESSURE_UNIT = "hPa";
var time = fileNameParts[0];
var level = fileNameParts[3].includes(PRESSURE_UNIT) ? fileNameParts[3].replace(PRESSURE_UNIT,'') : fileNameParts[3];
var variable = fileNameParts[1];
console.log('isForeCast='+isForeCast(req.params.day,req.params.month,req.params.year,time,res));
if (!isForeCast(req.params.day,req.params.month,req.params.year,time,res)) nc.downloadAndSaveNomadData(date,time,level,variable,res,2); //TODO osetrit, kdyz bude 404
else {
const MAX_TRIES = 6;
var requestDate = nc.YYYYMMDDHHToDate(date,time);
var currentDateObj = nc.toNOMADSDate();
var date = currentDateObj.year+currentDateObj.month+currentDateObj.day
nc.downloadForeCastData(requestDate,date,currentDateObj.time,level,variable,MAX_TRIES,res)
}
}
} catch(err) {
console.log('Err while sending data file ' + err);
res.status(500);
res.send('Err while sending data file ' + err);
}
});
/**
* Returns true if given date is from the future
* @param {*} day
* @param {*} month
* @param {*} year
* @returns
*/
function isForeCast(day,month,year,time,res) {
var date = nc.YYYYMMDDHHToDate(year+month+day,time);
return date > new Date();
}
// Using a function to set default app path
function getDir() {
if (process.pkg) {
return path.resolve(process.execPath + "/..");
} else {
return path.join(require.main ? require.main.path : process.cwd());
}
}
function cleanData() {
try {
const DATA_DIR_PATH = './data/weather';
fs.rmSync(DATA_DIR_PATH, { recursive: true, force: true });
} catch (e) {
console.log('Error while cleaning the download data. ' + e);
}
}
function exitHandler(options, exitCode) {
if (options.cleanup) {
console.log('cleaning');
cleanData();
}
if (exitCode || exitCode === 0) console.log(exitCode);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
open("http://localhost:"+port);