Skip to content

Commit

Permalink
Feature: Live service check weather proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
a2937 committed Nov 11, 2023
1 parent 21872d8 commit b9f2613
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
13 changes: 6 additions & 7 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const { ESLint } = require('eslint');

const cli = new ESLint();

// This lets us abort if we've already run a stage for all files
const completedStages = new Set();

// if a lot of files are changed, it's faster to run prettier/eslint on the
// whole project than to run them on each file separately
module.exports = {
'*.(js|jsx|ts|tsx)': async files => {
if (completedStages.has('js')) return [];
Expand All @@ -15,13 +11,16 @@ module.exports = {
files.map(file => cli.isPathIgnored(file))
);
const lintableFiles = files.filter((_, i) => !ignoredIds[i]);

if (files.length > 10) {
completedStages.add('js');
return ['eslint --max-warnings=0 --cache --fix .', 'prettier --write .'];
} else {
return [
'eslint --max-warnings=0 --cache --fix ' + lintableFiles.join(' '),
...files.map(filename => `prettier --write '${filename}'`)
`eslint --max-warnings=0 --cache --fix ${lintableFiles
.map(file => `"${file}"`)
.join(' ')}`,
...files.map(filename => `prettier --write "${filename}"`)
];
}
},
Expand All @@ -33,7 +32,7 @@ module.exports = {
return 'prettier --write .';
} else {
return files.map(
filename => `prettier --write --ignore-unknown '${filename}'`
filename => `prettier --write --ignore-unknown "${filename}"`
);
}
}
Expand Down
69 changes: 39 additions & 30 deletions apps/weather-proxy/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,81 @@ var express = require('express');
var app = express();

var requestTimes = [];
var weatherAPI = 'http://api.openweathermap.org/data/2.5/weather?units=metric&appid=' + process.env.OPEN_WEATHER_API_KEY;
var weatherAPI =
'http://api.openweathermap.org/data/2.5/weather?units=metric&appid=' +
process.env.OPEN_WEATHER_API_KEY;

function replaceIconsWithLinks(data){
for(var i = 0; i < data.weather.length; ++i){
if('icon' in data.weather[0]){
function replaceIconsWithLinks(data) {
for (var i = 0; i < data.weather.length; ++i) {
if ('icon' in data.weather[0]) {
data.weather[0].icon = imgLinks[data.weather[0].icon];
}
}
}

function getBestCachedData(){
function getBestCachedData() {
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('./data/cache.json', 'utf8'));
return obj;
}

function addToCache(){
function addToCache() {
//For now does nothing. We aren't saving data.
}

function removeDatesOlderThan(milliseconds){
for(var i = 0; i < requestTimes.length; ++i){
if(Date.now() - requestTimes[i] > milliseconds){
function removeDatesOlderThan(milliseconds) {
for (var i = 0; i < requestTimes.length; ++i) {
if (Date.now() - requestTimes[i] > milliseconds) {
requestTimes.splice(i, 1);
}
}
}

var cors = require('cors')
app.use(cors({optionSuccessStatus: 200}))
var cors = require('cors');
app.use(cors({ optionSuccessStatus: 200 }));

app.get("/", function(req, res){
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/views/index.html'));
});

app.get('/status/ping', (req, res) => {
res.send({ msg: 'pong' }).status(200);
});

app.use('/images', express.static('images'));

app.get("/api/current", function(req, res) {
app.get('/api/current', function (req, res) {
var longitude = req.query.lon;
var latitude = req.query.lat;
var callback = req.query.callback;
if(!isNaN(longitude) && !isNaN(latitude)){
if (!isNaN(longitude) && !isNaN(latitude)) {
var url = weatherAPI + '&lon=' + longitude + '&lat=' + latitude;
removeDatesOlderThan(60000);
if(requestTimes.length < 60){
if (requestTimes.length < 60) {
requestTimes.push(new Date());
request(url, function(err, resAPI, body){
if(err){
request(url, function (err, resAPI, body) {
if (err) {
console.log(err);
}
var data = JSON.parse(body);
if(data.cod != 200){
if (data.cod != 200) {
var codError = data.cod;
console.log("COD ERROR:" + codError);
console.log("ERROR:" + err);
console.log("RESPONSE ERROR:" + JSON.stringify(resAPI));
console.log("BODY:" + body);
console.log("DATA:" + JSON.stringify(data));
console.log("REQUEST:" + req.body);
console.log("REQUESTED URL:" + url);
console.log('COD ERROR:' + codError);
console.log('ERROR:' + err);
console.log('RESPONSE ERROR:' + JSON.stringify(resAPI));
console.log('BODY:' + body);
console.log('DATA:' + JSON.stringify(data));
console.log('REQUEST:' + req.body);
console.log('REQUESTED URL:' + url);
}
if('weather' in data){
if ('weather' in data) {
addToCache(data);
} else {
data = getBestCachedData(longitude, latitude);
console.log(data);
}
replaceIconsWithLinks(data);
if(callback){
if (callback) {
res.jsonp(data);
} else {
res.json(data);
Expand All @@ -83,20 +89,23 @@ app.get("/api/current", function(req, res) {
} else {
var data = getBestCachedData(longitude, latitude);
replaceIconsWithLinks(data);
if(callback){
if (callback) {
res.jsonp(data);
} else {
res.json(data);
}
}
} else {
res.json({error:"Please provide longitude as lon and latitude as lat as numbers/floats."});
res.json({
error:
'Please provide longitude as lon and latitude as lat as numbers/floats.'
});
}
});

const portNum = process.env.PORT || 3000;

//Start our server and tests!
app.listen(portNum, function () {
console.log("Listening on port " + portNum);
console.log('Listening on port ' + portNum);
});

0 comments on commit b9f2613

Please sign in to comment.