forked from talkpython/modern-apis-with-fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
44 lines (32 loc) · 1.19 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
import json
from pathlib import Path
import fastapi
import uvicorn
from starlette.staticfiles import StaticFiles
from api import weather_api
from services import openweather_service
from views import home
api = fastapi.FastAPI()
def configure():
configure_routing()
configure_api_keys()
def configure_api_keys():
file = Path('settings.json').absolute()
if not file.exists():
print(f'WARNING: {file} file not found, you cannot continue, please see settings_template.json')
raise Exception('settings.json file not found, you cannot continue, please see settings_template.json')
with open(file) as fin:
settings = json.load(fin)
openweather_service.api_key = settings.get('api_key')
def configure_routing():
api.mount('/static', StaticFiles(directory='static'), name='static')
api.include_router(home.router)
api.include_router(weather_api.router)
if __name__ == '__main__':
configure()
# uvicorn was updated, and it's type definitions don't match FastAPI,
# but the server and code still work fine. So ignore PyCharm's warning:
# noinspection PyTypeChecker
uvicorn.run(api, port=8000, host='127.0.0.1')
else:
configure()