Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using api from openweather /inject some secrets #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions weather/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,27 @@ router.get('/', function(req, res) {
res.render('index', { weather: null, err: null });
});

router.post('/get_weather', async function (req,res) {
let city = req.body.city;
let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=${UNITS}&appid=${OWM_API_KEY}`;
router.post('/get_weather', async function (req, res) {
let lat = req.body.lat; // Latitude from the form
let lon = req.body.lon; // Longitude from the form
let url = `http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=${UNITS}&appid=${OWM_API_KEY}`;

try {
let data = await fetch(url);
let weather = await data.json();
console.log(weather);
if(weather.cod == '404' && weather.main == undefined) {
res.render('index', {weather: null, error: 'Error: Unknown city'});
if (weather.cod == '404' && weather.main == undefined) {
res.render('index', { weather: null, error: 'Error: Unknown location' });
} else if (weather.cod == '401' && weather.main == undefined) {
res.render('index', { weather: null, error: 'Error: Invalid API Key. Please see http://openweathermap.org/faq#error401 for more info.' });
} else {
let unit_hex = (UNITS == 'imperial') ? '℉' : '℃';
res.render('index', { weather: weather, error: null, units: unit_hex });
}
else if (weather.cod == '401' && weather.main == undefined) {
res.render('index', {weather: null, error: 'Error: Invalid API Key. Please see http://openweathermap.org/faq#error401 for more info.'});
}
else {
let unit_hex = (UNITS == 'imperial') ? '&#8457' : '&#8451';
res.render('index', {weather: weather, error: null, units: unit_hex});
}
}
catch (err) {
} catch (err) {
console.log(err);
res.render('index', {weather: null, error: 'Error: Unable to invoke OpenWeatherMap API'});
res.render('index', { weather: null, error: 'Error: Unable to invoke OpenWeatherMap API' });
}

});

module.exports = router;
53 changes: 28 additions & 25 deletions weather/views/index.pug
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
extends layout

block content
.container
h2
p
| OpenShift Weather App
br
h2
p OpenShift Weather App
br
br
.row
.col-md-3
form(method="post", action="get_weather")
.form-group
label.sr-only(for="lat") Latitude
input.form-control(name="lat" type="number" step="any" placeholder="Enter Latitude" required='required')
.form-group
label.sr-only(for="lon") Longitude
input.form-control(name="lon" type="number" step="any" placeholder="Enter Longitude" required='required')
button.btn.btn-success(type="submit") Get Weather
if error
br
.alert.alert-danger #{error}
if weather
.row
.col-md-3
form(method="post", action="get_weather")
.form-group
label.sr-only(for="city") City Name
input.form-control( name="city" type="text" placeholder="Enter City Name (for ex: New York)" required='required')
button.btn.btn-success(type="submit") Get Weather
if error
br
.alert.alert-danger #{error}
if weather
.row
.col-md-5
p
br
.panel.panel-primary
.panel-heading Weather for: #{weather.name}, #{weather.sys.country}
.panel-body
p Current Temperature = #{weather.main.temp} !{units}, #{weather.weather[0].description}
p Humidity = #{weather.main.humidity} %
p Min = #{weather.main.temp_min} !{units}
p Max = #{weather.main.temp_max} !{units}
.col-md-5
p
br
.panel.panel-primary
.panel-heading Weather for: #{weather.name}, #{weather.sys.country}
.panel-body
p Current Temperature = #{weather.main.temp} !{units}, #{weather.weather[0].description}
p Humidity = #{weather.main.humidity} %
p Min = #{weather.main.temp_min} !{units}
p Max = #{weather.main.temp_max} !{units}