-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (53 loc) · 1.63 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
60
61
62
63
64
65
66
67
68
69
from logging.config import dictConfig
from fastapi import FastAPI, Depends, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from models import CityWithWeather, CityBase, create_db
from typing import List
import os
from sqlmodel import Session
from weather import createCacheDir, getWeather
from typing import List, Union
import logging
from config import LogConfig
app = FastAPI()
dictConfig(LogConfig().dict())
log = logging.getLogger("snowbird_weather")
if "CLIENT_URL" in os.environ:
origins = [os.environ["CLIENT_URL"]]
else:
# Local development URLs
origins = [
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
log.info(f"CORS origins: {origins}")
engine = create_db()
def get_session():
with Session(engine) as session:
yield session
createCacheDir()
@app.post(
"/city_weather/", response_model=CityWithWeather, response_model_exclude_none=True
)
def city_weather(*, session: Session = Depends(get_session), city: CityBase):
log.info(f"Getting weather for {city}...")
try:
return getWeather(session, city)
except Exception as e:
log.error("Exception: " + e)
@app.post(
"/cities_weather/",
response_model=List[CityWithWeather],
response_model_exclude_none=True,
)
def city_weather(*, session: Session = Depends(get_session), cities: List[CityBase]):
cities_with_weather: List[CityWithWeather] = []
for city in cities:
cities_with_weather.append(getWeather(session, city))
return cities_with_weather