-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (47 loc) · 2.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import uvicorn
from fastapi import FastAPI, HTTPException, Query, Header
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import JSONResponse
import requests
import logging
from cachetools import cached, TTLCache
import os
logging.basicConfig(level=logging.INFO)
cache = TTLCache(maxsize=100, ttl=300)
OPENWEATHERMAP_API_KEY = os.getenv("OPENWEATHERMAP_API_KEY")
app = FastAPI()
origins = [
"http://localhost:3000", # URL do frontend se estiver em outra origem
"http://localhost:8000", # Para acessar via mesma origem
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Servir arquivos estáticos
app.mount("/static", StaticFiles(directory="static"), name="static")
@cached(cache)
def fetch_weather_data(url):
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
logging.error(f"Error fetching weather data: {e}")
return None
@app.get("/weather", summary="Get weather information", description="Retrieve weather data for a specific city and country.")
def get_weather(city: str = Query(..., description="Name of the city"), country: str = Query(..., description="Country code (e.g., 'BR' for Brazil)")):
logging.info(f"Receiving request for weather forecast for {city}, {country}")
url = f"http://api.openweathermap.org/data/2.5/weather?q={city},{country}&appid={OPENWEATHERMAP_API_KEY}&units=metric"
weather = fetch_weather_data(url)
if not weather:
logging.error(f"Failed to fetch weather data for {city}, {country}")
raise HTTPException(status_code=502, detail="Failed to fetch weather data")
logging.info(f"Weather data successfully retrieved for {city}, {country}")
return JSONResponse(content=weather)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)