mirror of
https://github.com/kirameki-cafe/Tenki.git
synced 2026-09-13 18:58:58 +00:00
Better response
This commit is contained in:
+51
-5
@@ -43,13 +43,34 @@ interface WeatherLookupCache {
|
|||||||
windspeed: number;
|
windspeed: number;
|
||||||
winddirection: number;
|
winddirection: number;
|
||||||
weathercode: number;
|
weathercode: number;
|
||||||
is_day: boolean;
|
is_day: number;
|
||||||
time: string;
|
time: string;
|
||||||
};
|
};
|
||||||
cacheExpireAt: Date;
|
cacheExpireAt: Date;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ApiResponse {
|
||||||
|
success: boolean;
|
||||||
|
error?: string;
|
||||||
|
data?: {
|
||||||
|
country: string;
|
||||||
|
country_code: string;
|
||||||
|
region: string;
|
||||||
|
region_name: string;
|
||||||
|
city: string;
|
||||||
|
timezone: string;
|
||||||
|
current_weather: {
|
||||||
|
temperature: number;
|
||||||
|
wind_speed: number;
|
||||||
|
wind_direction: number;
|
||||||
|
weather_code: number;
|
||||||
|
is_day: boolean;
|
||||||
|
time: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const geoIpCacheTime = 1000 * 60 * 60 * 24; // 24 hours
|
const geoIpCacheTime = 1000 * 60 * 60 * 24; // 24 hours
|
||||||
const weatherCacheTime = 1000 * 60 * 15; // 15 minutes
|
const weatherCacheTime = 1000 * 60 * 15; // 15 minutes
|
||||||
const geoIpLookupCache: GeoIpLookupCache = {};
|
const geoIpLookupCache: GeoIpLookupCache = {};
|
||||||
@@ -95,6 +116,10 @@ const getWeather = async (lat: number, lon: number) => {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const sendResponse = (res: any, data: ApiResponse) => {
|
||||||
|
res.json(data);
|
||||||
|
};
|
||||||
|
|
||||||
app.get("/", async (req, res) => {
|
app.get("/", async (req, res) => {
|
||||||
// Get the user's IP address
|
// Get the user's IP address
|
||||||
let ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
|
let ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
|
||||||
@@ -110,7 +135,6 @@ app.get("/", async (req, res) => {
|
|||||||
// Check if geo IP is cached
|
// Check if geo IP is cached
|
||||||
let geoIp = geoIpLookupCache[ip];
|
let geoIp = geoIpLookupCache[ip];
|
||||||
if (geoIp) {
|
if (geoIp) {
|
||||||
console.log(`Cache is valid for: ${geoIp.cacheExpireAt}`);
|
|
||||||
// Check if cached response is expired
|
// Check if cached response is expired
|
||||||
if (geoIp.cacheExpireAt < new Date()) {
|
if (geoIp.cacheExpireAt < new Date()) {
|
||||||
delete geoIpLookupCache[ip];
|
delete geoIpLookupCache[ip];
|
||||||
@@ -120,7 +144,9 @@ app.get("/", async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
geoIp = await getGeoIp(ip);
|
geoIp = await getGeoIp(ip);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return res.status(500).send("Error fetching location data");
|
return res
|
||||||
|
.status(500)
|
||||||
|
.json({ success: false, error: "Error fetching location data" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,11 +162,31 @@ app.get("/", async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
weather = await getWeather(geoIp.lat, geoIp.lon);
|
weather = await getWeather(geoIp.lat, geoIp.lon);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return res.status(500).send("Error fetching location data");
|
return res
|
||||||
|
.status(500)
|
||||||
|
.json({ success: false, error: "Error fetching weather data" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json(weather);
|
return sendResponse(res, {
|
||||||
|
success: true,
|
||||||
|
data: {
|
||||||
|
country: geoIp.country,
|
||||||
|
country_code: geoIp.countryCode,
|
||||||
|
region: geoIp.region,
|
||||||
|
region_name: geoIp.regionName,
|
||||||
|
city: geoIp.city,
|
||||||
|
timezone: geoIp.timezone,
|
||||||
|
current_weather: {
|
||||||
|
temperature: weather.current_weather.temperature,
|
||||||
|
wind_speed: weather.current_weather.windspeed,
|
||||||
|
wind_direction: weather.current_weather.winddirection,
|
||||||
|
weather_code: weather.current_weather.weathercode,
|
||||||
|
is_day: weather.current_weather.is_day === 1 ? true : false,
|
||||||
|
time: weather.current_weather.time,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user