-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
115 lines (92 loc) · 2.85 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
const config = require('config');
const express = require('express');
const app = express();
const rpn = require('request-promise-native');
const schedule = require('node-schedule');
let seriesInfo;
const updateTimeSlices = (apiKey) => {
const uri = `https://api.weather.com/v3/TileServer/series/productSet/PPAcore?apiKey=${apiKey}`;
const params = {
uri,
json: true
};
return rpn(params)
.then(body => {
seriesInfo = body.seriesInfo;
return seriesInfo;
})
.catch(error => {
throw new Error(error);
});
};
app.use('/:layerId/:level/:row/:col/:apiKey', (req, res, next) => {
if (!seriesInfo) {
console.log('initializing seriesInfo');
const apiKey = req.params.apiKey;
updateTimeSlices(apiKey)
.then(() => {
const everyFiveMinutes = '*/5 * * * *';
schedule.scheduleJob(everyFiveMinutes, () => {
updateTimeSlices(apiKey);
});
next();
});
} else {
next();
}
});
app.use('/:layerId/:level/:row/:col/:apiKey', (req, res) => {
const layerId = req.params.layerId;
const apiKey = req.params.apiKey;
const level = req.params.level;
const row = req.params.row;
const col = req.params.col;
const timeSlice = seriesInfo[layerId].series[0].ts;
let requestUrl = `https://api.weather.com/v3/TileServer/tile/${layerId}?ts=${timeSlice}&xyz=${col}:${row}:${level}&apiKey=${apiKey}`;
if (seriesInfo[layerId].series[0].fts) {
const fts = seriesInfo[layerId].series[0].fts[0];
requestUrl = `${requestUrl}&fts=${fts}`;
}
// request
// .get(requestUrl)
// .pipe(res);
// lambda can't hang with sending images back; only json. so just do some redirects (?)
res.redirect(302, requestUrl);
});
app.use('/layers', (req, res) => {
if (!req.query.apiKey) {
return res.send('you must provide the apiKey as a parameter in your URL. ex: ?apiKey=12345678910');
}
updateTimeSlices(req.query.apiKey)
.then(seriesInfo => {
const originUrl = `${req.protocol}://${req.get('host')}`;
const layers = Object.keys(seriesInfo).map(key => {
return {
layerId: key,
webTileLayerTemplateUrl: `${originUrl}/${key}/{level}/{row}/{col}/${req.query.apiKey}`
};
});
res
.status(200)
.json(layers);
})
.catch(err => {
return res.send(`error getting seriesInfo: ${err.message}`);
});
});
if (process.env.DEPLOY === 'export') {
module.exports = app;
} else {
// Start listening for HTTP traffic
// Set port for configuration or fall back to default
const port = config.port || 8080;
app.listen(port, () => {
const message = `
now listening on ${port}
Try it out in your browser: http://localhost:${port}/layers
Or on the command line: curl --silent http://localhost:${port}/layers
Press control + c to exit
`;
console.log(message);
});
}