forked from jwvanderstam/LocalChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_fastapi.py
More file actions
180 lines (140 loc) · 6.72 KB
/
Copy pathapp_fastapi.py
File metadata and controls
180 lines (140 loc) · 6.72 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
"""FastAPI application factory — wiring only, no startup I/O.
Call create_app() to get a fully wired FastAPI app. For production,
follow up with bootstrap_app(app) from src.app_bootstrap.
Tests call only create_app(), which is safe with zero mocking.
Pass config_override={'TESTING': True} to disable JWT and rate limiting.
"""
from __future__ import annotations
import os
from pathlib import Path
from typing import Any
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
from slowapi.errors import RateLimitExceeded
from . import config
from .utils.logging_config import get_logger
logger = get_logger(__name__)
def create_app(config_override: dict[str, Any] | None = None) -> FastAPI:
"""Create and wire a FastAPI application instance (no I/O).
Safe to call in tests without mocking.
"""
config.validate_secrets()
_cfg = config_override or {}
testing = _cfg.get("TESTING", False)
root_dir = Path(__file__).parent.parent
upload_folder = _cfg.get("UPLOAD_FOLDER", config.UPLOAD_FOLDER)
os.makedirs(upload_folder, exist_ok=True)
app = FastAPI(
title="LocalChat",
version="1.0.0",
docs_url="/api/docs/",
openapi_url="/api/openapi.json",
)
# ── App state ──────────────────────────────────────────────────────────
from .db import db
from .ollama_client import ollama_client
from .rag import doc_processor
app.state.db = db
app.state.ollama_client = ollama_client
app.state.doc_processor = doc_processor
app.state.cloud_client = _init_cloud_client()
app.state.testing = testing
app.state.upload_folder = upload_folder
app.state.static_folder = str(root_dir / "static")
app.state.template_folder = str(root_dir / "templates")
# Placeholders overwritten by bootstrap
app.state.sync_worker = None
app.state.connector_registry = None
app.state.embedding_cache = None
app.state.query_cache = None
app.state.plugin_loader = None
app.state.startup_status = {"ollama": False, "database": False, "ready": False}
# ── Security ───────────────────────────────────────────────────────────
_init_security(app, testing)
# ── Routes ─────────────────────────────────────────────────────────────
_register_routers(app)
# ── Static files ───────────────────────────────────────────────────────
static_dir = root_dir / "static"
if static_dir.exists():
app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")
# ── Error handlers ─────────────────────────────────────────────────────
_register_error_handlers(app)
logger.info("FastAPI application created (testing=%s)", testing)
return app
def _init_security(app: FastAPI, testing: bool) -> None:
from slowapi import _rate_limit_exceeded_handler
from .security_fastapi import limiter, setup_cors
setup_cors(app)
if not testing:
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
@app.middleware("http")
async def _log_requests(request: Request, call_next):
from .utils.logging_config import sanitize_log_value as _slv
logger.info("%s %s", _slv(request.method), _slv(request.url.path))
response = await call_next(request)
logger.debug("%s %s → %s", _slv(request.method), _slv(request.url.path), response.status_code)
return response
logger.info("Security middleware initialised")
def _init_cloud_client() -> Any:
if not config.CLOUD_FALLBACK_ENABLED:
return None
if not config.CLOUD_MODEL:
logger.warning("[CloudFallback] CLOUD_FALLBACK_ENABLED=true but CLOUD_MODEL is not set")
return None
try:
from .llm_client import LiteLLMClient
return LiteLLMClient(
provider=config.CLOUD_PROVIDER,
api_key=config.CLOUD_API_KEY,
model=config.CLOUD_MODEL,
)
except ImportError as e:
logger.warning(f"[CloudFallback] {e} — disabled")
return None
except Exception:
logger.exception("[CloudFallback] Failed to initialise")
return None
def _register_routers(app: FastAPI) -> None:
from .routes_fastapi import (
annotation_routes,
api_routes,
auth_routes,
connector_routes,
document_routes,
feedback_routes,
longterm_memory_routes,
memory_routes,
model_routes,
oauth_routes,
settings_routes,
web_routes,
workspace_routes,
)
app.include_router(web_routes.router)
app.include_router(api_routes.router, prefix="/api")
app.include_router(document_routes.router, prefix="/api/documents")
app.include_router(model_routes.router, prefix="/api/models")
app.include_router(memory_routes.router, prefix="/api")
app.include_router(longterm_memory_routes.router, prefix="/api/memory")
app.include_router(feedback_routes.router, prefix="/api")
app.include_router(workspace_routes.router, prefix="/api")
app.include_router(connector_routes.router, prefix="/api")
app.include_router(settings_routes.router, prefix="/api")
app.include_router(auth_routes.router, prefix="/api")
app.include_router(oauth_routes.router, prefix="/api")
app.include_router(annotation_routes.router, prefix="/api")
logger.debug("Routers registered")
def _register_error_handlers(app: FastAPI) -> None:
@app.exception_handler(404)
async def not_found(_request: Request, _exc):
return JSONResponse({"success": False, "error": "NotFound", "message": "Resource not found"}, status_code=404)
@app.exception_handler(405)
async def method_not_allowed(_request: Request, _exc):
return JSONResponse({"success": False, "error": "MethodNotAllowed", "message": "Method not allowed"}, status_code=405)
@app.exception_handler(500)
async def internal_error(_request: Request, _exc):
logger.error("Unhandled exception", exc_info=_exc)
return JSONResponse({"success": False, "error": "InternalServerError", "message": "An unexpected error occurred"}, status_code=500)
__all__ = ["create_app"]