-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
39 lines (34 loc) · 1.45 KB
/
app.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
import cors from 'cors';
import express from 'express';
import helmet from 'helmet';
import { log } from './logger.js';
import qs from 'qs';
import { cacheClient } from './cacheService.js';
import { getOpenWeatherMapData, getWeatherStackData } from './weatherProviders.js';
const app = express();
app.use('*', helmet(), cors(), log);
app.get('/v1/weather', async (req, res) => {
const { city } = qs.parse(req.query);
let response = await cacheClient.get(city, (reply) => JSON.parse(reply));
if (response) {
req.log.info(`responding with data from cache: ${response}`);
} else {
response = await getWeatherStackData(`${city || 'Melbourne'},Australia`);
if (response) {
req.log.info(`responding with data from weatherStack: ${JSON.stringify(response)}`);
} else {
response = await getOpenWeatherMapData(`${city || 'Melbourne'},AU`);
if (response) {
req.log.info(`failover. responding with data from openWeatherMap: ${JSON.stringify(response)}`);
} else {
req.log.error(`weather services offline and no cached data for ${city}`);
res.status(500).json({ message: `weather services offline and no cached data for ${city}` });
}
}
await cacheClient.set(city, JSON.stringify(response));
await cacheClient.expire(city, 3); // set value in cache with a TTL of 3sec
}
res.status(200).json(response);
});
app.get('/health', (_req, res) => res.status(200).json({ status: 'OK' }));
export { app };