Skip to content

Commit

Permalink
feat: add static endpoints for weather proxy (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
moT01 authored Nov 21, 2024
1 parent 47ac582 commit 0fb885f
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 17 deletions.
188 changes: 188 additions & 0 deletions apps/weather-proxy/data/cities.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
{
"new york": {
"coord": { "lon": -74.0059, "lat": 40.7127 },
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "https://cdn.freecodecamp.org/weather-icons/04d.png"
}
],
"base": "stations",
"main": {
"temp": 14.33,
"feels_like": 13.2,
"temp_min": 13.21,
"temp_max": 15.54,
"pressure": 1008,
"humidity": 53,
"sea_level": 1008,
"grnd_level": 1007
},
"visibility": 10000,
"wind": { "speed": 3.6, "deg": 40 },
"clouds": { "all": 100 },
"dt": 1732123426,
"sys": {
"type": 1,
"id": 4610,
"country": "US",
"sunrise": 1732103363,
"sunset": 1732138471
},
"timezone": -18000,
"id": 5128581,
"name": "New York",
"cod": 200
},
"chicago": {
"coord": { "lon": -87.6298, "lat": 41.8781 },
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "https://cdn.freecodecamp.org/weather-icons/03d.png"
}
],
"base": "stations",
"main": {
"temp": 8.91,
"feels_like": 4.91,
"temp_min": 7.86,
"temp_max": 9.44,
"pressure": 1009,
"humidity": 50,
"sea_level": 1009,
"grnd_level": 987
},
"visibility": 10000,
"wind": { "speed": 9.39, "deg": 285, "gust": 12.52 },
"clouds": { "all": 40 },
"dt": 1732123645,
"sys": {
"type": 2,
"id": 2010190,
"country": "US",
"sunrise": 1732106817,
"sunset": 1732141557
},
"timezone": -21600,
"id": 4887398,
"name": "Chicago",
"cod": 200
},
"los angeles": {
"coord": { "lon": -118.2437, "lat": 34.0522 },
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "https://cdn.freecodecamp.org/weather-icons/01d.png"
}
],
"base": "stations",
"main": {
"temp": 17.24,
"feels_like": 15.88,
"temp_min": 14.84,
"temp_max": 19.62,
"pressure": 1023,
"humidity": 33,
"sea_level": 1023,
"grnd_level": 1003
},
"visibility": 10000,
"wind": { "speed": 2.57, "deg": 70 },
"clouds": { "all": 0 },
"dt": 1732123664,
"sys": {
"type": 2,
"id": 2075946,
"country": "US",
"sunrise": 1732113063,
"sunset": 1732150008
},
"timezone": -28800,
"id": 5368361,
"name": "Los Angeles",
"cod": 200
},
"tokyo": {
"coord": { "lon": 139.6917, "lat": 35.6895 },
"weather": [
{
"id": 501,
"main": "Rain",
"description": "moderate rain",
"icon": "https://cdn.freecodecamp.org/weather-icons/10n.png"
}
],
"base": "stations",
"main": {
"temp": 8.71,
"feels_like": 5.38,
"temp_min": 8.08,
"temp_max": 9.81,
"pressure": 1015,
"humidity": 92,
"sea_level": 1015,
"grnd_level": 1014
},
"visibility": 7000,
"wind": { "speed": 6.69, "deg": 330 },
"rain": { "1h": 2.05 },
"clouds": { "all": 75 },
"dt": 1732123711,
"sys": {
"type": 2,
"id": 268395,
"country": "JP",
"sunrise": 1732137787,
"sunset": 1732174284
},
"timezone": 32400,
"id": 1850144,
"name": "Tokyo",
"cod": 200
},
"london": {
"coord": { "lon": -0.1278, "lat": 51.5074 },
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "https://cdn.freecodecamp.org/weather-icons/01n.png"
}
],
"base": "stations",
"main": {
"temp": 2.62,
"feels_like": 0.84,
"temp_min": 1.72,
"temp_max": 3.49,
"pressure": 1010,
"humidity": 81,
"sea_level": 1010,
"grnd_level": 1005
},
"visibility": 10000,
"wind": { "speed": 1.79, "deg": 285, "gust": 3.13 },
"clouds": { "all": 1 },
"dt": 1732123462,
"sys": {
"type": 2,
"id": 2075535,
"country": "GB",
"sunrise": 1732087658,
"sunset": 1732118707
},
"timezone": 0,
"id": 2643743,
"name": "London",
"cod": 200
}
}
13 changes: 13 additions & 0 deletions apps/weather-proxy/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('dotenv').config();
var request = require('request');
var path = require('path');
var imgLinks = require('./data/imgLinks.json');
var cities = require('./data/cities.json');
var express = require('express');
var app = express();

Expand Down Expand Up @@ -49,6 +50,18 @@ app.get('/status/ping', (req, res) => {

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

app.get('/api/city/:city', function (req, res) {
const city = req.params.city.toLocaleLowerCase();

if (cities[city]) {
res.status(200).json(cities[city]);
} else {
res.status(404).json({
error: `Weather information for city '${city}' not found.`
});
}
});

app.get('/api/current', function (req, res) {
var longitude = req.query.lon;
var latitude = req.query.lat;
Expand Down
51 changes: 34 additions & 17 deletions apps/weather-proxy/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,44 @@
background-color: #ff9;
padding: 3px;
}
p.usage {

p.usage,
p.examples {
display: block;
background-color: #d0e7d0;
padding: 15px;
border-radius: 15px;
max-width: 900px;
line-height: 1.5em;
margin: 0 auto;
margin: 0 auto 20px auto;
}
</style>
</head>
<body>
<h2>freeCodeCamp Weather API Passthrough</h2>
<p class="usage">
USAGE:<br />
Use the endpoint:
<code>https://weather-proxy.freecodecamp.rocks/</code>. Use this endpoint
to get the weather at a location. To prevent abuses this server accepts
<code>GET</code> requests only, and serves only the route
<code>/api/current?lon=:longitude&lat=:latitude</code>. Images links are
included in the JSON under <code>weather[0].icon</code>. This is enough to
complete the challenge.
<br />
Example:
<br />
Request:
<strong>Usage:</strong><br />
Use <code>https://weather-proxy.freecodecamp.rocks/</code> to get weather
information for a location in JSON format. This server only accepts
<code>GET</code> requests, and only serves the routes
<code>/api/city/:city</code> and
<code>/api/current?lon=:longitude&lat=:latitude</code>.
</p>

<p class="examples">
<strong>Example Requests:</strong><br />
<a
href="https://weather-proxy.freecodecamp.rocks/api/city/new york"
target="_blank"
><code
>https://weather-proxy.freecodecamp.rocks/api/city/new york</code
></a
><br />
This endpoint does not return the current weather and the only available
cities for this endpoint are <code>New York</code>, <code>Chicago</code>,
<code>Los Angeles</code>, <code>Tokyo</code>, and
<code>London</code>.<br />
To get the current weather for any location use:<br />
<a
href="https://weather-proxy.freecodecamp.rocks/api/current?lat=35&lon=139"
target="_blank"
Expand All @@ -51,7 +63,10 @@ <h2>freeCodeCamp Weather API Passthrough</h2>
></a
>
<br />
Response:
</p>

<p class="examples">
<strong>Example Response:</strong><br />
<code>
{ "coord": { "lon": 139, "lat": 35 }, "weather": [ { "id": 801, "main":
"Clouds", "description": "few clouds", "icon":
Expand All @@ -61,8 +76,10 @@ <h2>freeCodeCamp Weather API Passthrough</h2>
"wind": { "speed": 1.34, "deg": 177, "gust": 2.68 }, "clouds": { "all":
16 }, "dt": 1597922946, "sys": { "type": 3, "id": 2019346, "country":
"JP", "sunrise": 1597867673, "sunset": 1597915636 }, "timezone": 32400,
"id": 1851632, "name": "Shuzenji", "cod": 200 }
</code>
"id": 1851632, "name": "Shuzenji", "cod": 200 } </code
><br />
Images links are included in the JSON under <code>weather[0].icon</code>.
This is enough to complete the challenge.
</p>
<script>
(function (i, s, o, g, r, a, m) {
Expand Down

0 comments on commit 0fb885f

Please sign in to comment.