-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
243 lines (186 loc) · 7.94 KB
/
main.py
File metadata and controls
243 lines (186 loc) · 7.94 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import asyncio
import logging
import os
from contextlib import asynccontextmanager, suppress
from pathlib import Path
from typing import Annotated
from fastapi import FastAPI, Header, HTTPException, Request, status
from fastapi.concurrency import run_in_threadpool
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from backend.app import version
from . import routers
from .cleanup import start_cleanup_loop
from .config import settings
from .db import engine
from .migrate import run_migrations
from .postprocessing import ProcessingQueue
def create_app() -> FastAPI:
from .embed_preview import (
get_token,
is_embed_bot,
render_embed_preview,
)
logging.basicConfig(level=logging.INFO, format="%(levelname)s [%(name)s] %(message)s")
Path(settings.storage_path).mkdir(parents=True, exist_ok=True)
Path(settings.config_path).mkdir(parents=True, exist_ok=True)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Application lifespan context manager.
Args:
app (FastAPI): The FastAPI application instance.
"""
if not settings.skip_migrations:
await run_in_threadpool(run_migrations)
queue = ProcessingQueue()
queue.start_worker()
app.state.processing_queue = queue
if not settings.skip_cleanup:
app.state.cleanup_task = asyncio.create_task(start_cleanup_loop(), name="cleanup_loop")
yield
await queue.stop_worker()
if not settings.skip_cleanup:
task: asyncio.Task | None = getattr(app.state, "cleanup_task", None)
if task:
task.cancel()
with suppress(asyncio.CancelledError):
await task
await engine.dispose()
app = FastAPI(
title=settings.app_name,
lifespan=lifespan,
version=version.APP_VERSION,
redirect_slashes=True,
docs_url="/docs" if bool(os.getenv("FBC_DEV_MODE", "0") == "1") else None,
redoc_url=None,
)
if settings.trust_proxy_headers:
@app.middleware("http")
async def proxy_headers_middleware(request: Request, call_next):
"""
Middleware to trust proxy headers for scheme and host.
Args:
request (Request): The incoming HTTP request.
call_next: Function to call the next middleware or route handler.
Returns:
Response: The HTTP response.
"""
if forwarded_proto := request.headers.get("X-Forwarded-Proto"):
request.scope["scheme"] = forwarded_proto
if forwarded_host := request.headers.get("X-Forwarded-Host"):
request.scope["server"] = (forwarded_host.split(":")[0], 443 if forwarded_proto == "https" else 80)
return await call_next(request)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.allowed_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
expose_headers=[
"Upload-Offset",
"Upload-Length",
"Tus-Resumable",
"Tus-Version",
"Tus-Extension",
"Upload-Metadata",
"Upload-Defer-Length",
"Upload-Concat",
"Location",
],
)
@app.middleware("http")
async def log_exceptions(request: Request, call_next):
"""
Middleware to log unhandled exceptions.
Args:
request (Request): The incoming HTTP request.
call_next: Function to call the next middleware or route handler.
Returns:
Response: The HTTP response.
"""
try:
return await call_next(request)
except Exception as exc:
logging.exception("Unhandled exception", exc_info=exc)
return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": "Internal Server Error"})
@app.get("/api/health", name="health")
def health() -> dict[str, str]:
"""Health check endpoint."""
return {"status": "ok"}
@app.get("/api/version", name="version")
def app_version() -> dict[str, str]:
"""Get application version information."""
return {
"version": version.APP_VERSION,
"commit_sha": version.APP_COMMIT_SHA,
"build_date": version.APP_BUILD_DATE,
"branch": version.APP_BRANCH,
}
for _route in routers.__all__:
app.include_router(getattr(routers, _route).router)
frontend_dir: Path = Path(settings.frontend_export_path).resolve()
def serve_static():
if frontend_dir.exists():
index_file = frontend_dir / "index.html"
if index_file.exists():
return FileResponse(index_file, status_code=status.HTTP_200_OK)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@app.get("/e/{token}", name="token_embed")
@app.get("/e/{token}/")
async def token_embed(request: Request, token: str):
"""Render a static embed preview page."""
if not settings.allow_public_downloads:
return serve_static()
from backend.app.db import get_db
async for db in get_db():
if not (token_row := await get_token(db, token)):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Token not found")
return await render_embed_preview(request, db, token_row, user=True)
@app.get("/f/{token}", name="share_page")
@app.get("/f/{token}/")
async def share_page(token: str, request: Request, user_agent: Annotated[str | None, Header()] = None):
"""Handle /f/{token} with bot detection for embed preview."""
from backend.app.db import get_db
if is_embed_bot(user_agent) and settings.allow_public_downloads:
async for db in get_db():
if token_row := await get_token(db, token):
return await render_embed_preview(request, db, token_row)
return serve_static()
@app.get("/t/{token}", name="upload_page")
@app.get("/t/{token}/")
async def upload_page(token: str, request: Request, user_agent: Annotated[str | None, Header()] = None):
"""Handle /t/{token} with bot detection for embed preview."""
if not frontend_dir.exists():
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
index_file = frontend_dir / "index.html"
if not index_file.exists():
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return FileResponse(index_file, status_code=status.HTTP_200_OK)
@app.get("/{full_path:path}", name="static_frontend")
async def frontend(full_path: str) -> FileResponse:
"""
Serve static frontend files.
Args:
full_path (str): The requested file path.
Returns:
FileResponse: The response containing the requested file.
"""
if full_path.startswith("api/"):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
if not frontend_dir.exists():
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
if not full_path or "/" == full_path:
index_file: Path = frontend_dir / "index.html"
if index_file.exists():
return FileResponse(index_file, status_code=status.HTTP_200_OK)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
requested_file: Path = frontend_dir / full_path
if requested_file.is_file():
return FileResponse(requested_file, status_code=status.HTTP_200_OK)
index_file: Path = frontend_dir / "index.html"
if index_file.exists():
return FileResponse(index_file, status_code=status.HTTP_200_OK)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return app
app: FastAPI = create_app()