-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
599f253
commit 7533a87
Showing
10 changed files
with
127 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ externalPort = 80 | |
|
||
[[ports]] | ||
localPort = 3001 | ||
externalPort = 3001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// pages/api/weather-by-ip.ts | ||
"use client" | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import axios from 'axios'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
// Get client IP address from the request | ||
const clientIp = req.headers['x-real-ip'] || req.socket.remoteAddress; | ||
|
||
// Example URL for geolocation API (replace with actual API) | ||
const geoApiUrl = `https://ipapi.co/${clientIp}/json/`; | ||
|
||
try { | ||
// Fetch geolocation data based on IP | ||
const geoResponse = await axios.get(geoApiUrl); | ||
const { latitude, longitude } = geoResponse.data; | ||
|
||
// Fetch weather data based on detected coordinates | ||
const weatherApiUrl = 'https://weatherapi-com.p.rapidapi.com/current.json'; | ||
const weatherOptions = { | ||
params: { q: `${latitude},${longitude}` }, | ||
headers: { | ||
'x-rapidapi-key': 'fb4e3b3dd0mshddc389caebd5192p146dc6jsn1901b7092089', | ||
'x-rapidapi-host': 'weatherapi-com.p.rapidapi.com' | ||
} | ||
}; | ||
|
||
const weatherResponse = await axios.get(weatherApiUrl, weatherOptions); | ||
res.status(200).json(weatherResponse.data); | ||
} catch (error) { | ||
console.error('Error fetching weather data:', error); | ||
res.status(500).json({ error: 'Failed to fetch weather data' }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// pages/index.tsx | ||
"use client" | ||
import { useEffect, useState } from 'react'; | ||
import axios from 'axios'; | ||
|
||
interface WeatherData { | ||
location: { | ||
name: string; | ||
region: string; | ||
country: string; | ||
}; | ||
current: { | ||
temp_c: number; | ||
condition: { | ||
text: string; | ||
icon: string; | ||
}; | ||
}; | ||
} | ||
|
||
const WeatherApp = () => { | ||
const [weather, setWeather] = useState<WeatherData | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchWeatherByIp = async () => { | ||
try { | ||
const response = await axios.get('/api/weather'); | ||
const data: WeatherData = response.data; | ||
setWeather(data); | ||
} catch (error) { | ||
console.error('Error fetching weather data:', error); | ||
} | ||
}; | ||
|
||
fetchWeatherByIp(); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h1>Weather Information</h1> | ||
{weather ? ( | ||
<div> | ||
<h2>{weather.location.name}, {weather.location.region}, {weather.location.country}</h2> | ||
<p>Temperature: {weather.current.temp_c}°C</p> | ||
<p>Condition: {weather.current.condition.text}</p> | ||
<img | ||
src={weather.current.condition.icon} | ||
alt="Weather Icon" | ||
width={64} | ||
height={64} | ||
/> | ||
</div> | ||
) : ( | ||
<p>Loading...</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default WeatherApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.