-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmain.py
204 lines (171 loc) · 6.17 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Copyright (c) 2022, 2023 Humanitarian OpenStreetMap Team
#
# This file is part of FMTM.
#
# FMTM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FMTM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FMTM. If not, see <https:#www.gnu.org/licenses/>.
#
"""Entrypoint for FastAPI app."""
import logging
import sys
from contextlib import asynccontextmanager
from typing import Optional
import sentry_sdk
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse, RedirectResponse
from loguru import logger as log
from osm_fieldwork.xlsforms import xlsforms_path
from .__version__ import __version__
from .auth import auth_routes
from .central import central_routes
from .config import settings
from .db.database import get_db
from .organization import organization_routes
from .projects import project_routes
from .projects.project_crud import read_xlsforms
from .submission import submission_routes
from .tasks import tasks_routes
from .users import user_routes
if not settings.DEBUG:
sentry_sdk.init(
dsn=settings.SENTRY_DSN,
traces_sample_rate=0.1,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup events
log.debug("Starting up FastAPI server.")
log.debug("Reading XLSForms from DB.")
read_xlsforms(next(get_db()), xlsforms_path)
yield
# Shutdown events
log.debug("Shutting down FastAPI server.")
def get_application() -> FastAPI:
"""Get the FastAPI app instance, with settings."""
_app = FastAPI(
title=settings.APP_NAME,
description="HOTOSM Field Tasking Manager",
version=__version__,
license_info={
"name": "GPL-3.0-only",
"url": "https://raw.githubusercontent.com/hotosm/fmtm/main/LICENSE",
},
debug=settings.DEBUG,
lifespan=lifespan,
root_path=settings.API_PREFIX,
)
# Set custom logger
_app.logger = get_logger()
_app.add_middleware(
CORSMiddleware,
allow_origins=settings.EXTRA_CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=["Content-Disposition"],
)
_app.include_router(user_routes.router)
_app.include_router(project_routes.router)
_app.include_router(tasks_routes.router)
_app.include_router(central_routes.router)
_app.include_router(auth_routes.router)
_app.include_router(submission_routes.router)
_app.include_router(organization_routes.router)
return _app
class InterceptHandler(logging.Handler):
"""Intercept python standard lib logging."""
def emit(self, record):
"""Retrieve context where the logging call occurred.
This happens to be in the 6th frame upward.
"""
logger_opt = log.opt(depth=6, exception=record.exc_info)
logger_opt.log(record.levelno, record.getMessage())
def get_logger():
"""Override FastAPI logger with custom loguru."""
# Hook all other loggers into ours
logger_name_list = [name for name in logging.root.manager.loggerDict]
for logger_name in logger_name_list:
logging.getLogger(logger_name).setLevel(10)
logging.getLogger(logger_name).handlers = []
if logger_name == "sqlalchemy":
# Don't hook sqlalchemy, very verbose
continue
if "." not in logger_name:
logging.getLogger(logger_name).addHandler(InterceptHandler())
log.remove()
log.add(
sys.stderr,
level=settings.LOG_LEVEL,
format=(
"{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} "
"| {name}:{function}:{line} | {message}"
),
enqueue=True, # Run async / non-blocking
colorize=True,
backtrace=True, # More detailed tracebacks
catch=True, # Prevent app crashes
)
# Only log to file in production
if not settings.DEBUG:
log.add(
"/opt/logs/fmtm.json",
level=settings.LOG_LEVEL,
enqueue=True,
serialize=True, # JSON format
rotation="00:00", # New file at midnight
retention="10 days",
# format=log_json_format, # JSON format func
)
log.add(
"/opt/logs/create_project.json",
level=settings.LOG_LEVEL,
enqueue=True,
serialize=True,
rotation="00:00",
retention="10 days",
filter=lambda record: record["extra"].get("task") == "create_project",
)
api = get_application()
@api.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
"""Exception handler for more descriptive logging."""
errors = []
for error in exc.errors():
# TODO Handle this properly
if error["msg"] in ["Invalid input", "field required"]:
status_code = 422 # Unprocessable Entity
else:
status_code = 400 # Bad Request
errors.append(
{
"loc": error["loc"],
"msg": error["msg"],
"error": error["msg"] + str([x for x in error["loc"]]),
}
)
return JSONResponse(status_code=status_code, content={"errors": errors})
@api.get("/")
async def home():
"""Redirect home to docs."""
return RedirectResponse("/docs")
@api.get("/items/{item_id}")
async def read_item(item_id: int, q: Optional[str] = None):
"""Get item IDs."""
return {"item_id": item_id, "q": q}
@api.get("/images/{image_filename}")
async def get_images(image_filename: str):
"""Download image files."""
path = f"./app/images/{image_filename}"
return FileResponse(path)