This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (104 loc) · 2.66 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
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
const express = require('express');
const redis = require('redis');
const bodyParser = require('body-parser');
const axios = require('axios');
require('dotenv').config();
const API_KEY = process.env.KEY;
const API_URL = process.env.URL;
const DEBUG = process.env.DEBUG;
const redis_client = redis.createClient({host: process.env.REDIS_HOST, port: 6379});
const app = express();
app.use(express.static('public'));
app.use(bodyParser.json());
//Middleware Function to Check Cache
checkCache = (req, res, next) => {
const city = req.body['city'];
redis_client.get(city, (err, data) => {
if (err) {
console.log(err);
res.status(500).send(err);
}
//if no match found
if (data != null) {
res.send(data);
} else {
//proceed to next middleware function
next();
}
});
};
app.post('/api', checkCache, (req, res) => {
let city = req.body['city'];
let name = city.split(',')[0];
let country = city.split(',')[1];
if(DEBUG === 'true') {
res.send(mockObj);
}else {
axios({
method: 'get',
url: API_URL + '?q=' + name + ',' + country + '&appid=' + API_KEY
}).then((response) => {
const data = JSON.stringify(response.data);
redis_client.setex(city, 1800, data);
res.send(data);
}).catch((error) => {
res.status(error.response.status).send(error.response.data);
});
}
})
app.post('/api/user', (req, res) => {
let lat = req.body['lat'];
let lon = req.body['lon'];
axios({
method: 'get',
url: API_URL + '?lat=' + lat + '&lon=' + lon + '&appid=' + API_KEY
}).then((response) => {
res.send(JSON.stringify(response.data));
}).catch((error) => {
res.status(error.response.status).send(error.response.data);
});
})
app.listen(3000);
const mockObj = {
"coord":{
"lon":-0.13,
"lat":51.51
},
"weather":[
{
"id":500,
"main":"Rain",
"description":"light rain",
"icon":"10d"
}
],
"base":"stations",
"main":{
"temp":289.21,
"feels_like":287.62,
"temp_min":288.71,
"temp_max":290.15,
"pressure":1009,
"humidity":82
},
"visibility":10000,
"wind":{
"speed":3.6,
"deg":240
},
"clouds":{
"all":100
},
"dt":1592476796,
"sys":{
"type":1,
"id":1414,
"country":"GB",
"sunrise":1592451764,
"sunset":1592511644
},
"timezone":3600,
"id":2643743,
"name":"London",
"cod":200
};