File size: 4,159 Bytes
9c56a9c
c5ca5f9
 
17f4503
c5ca5f9
 
17f4503
9c56a9c
17f4503
c5ca5f9
17f4503
 
 
c5ca5f9
9c56a9c
 
17f4503
 
 
 
c5ca5f9
17f4503
 
c5ca5f9
9c56a9c
 
 
 
 
 
 
17f4503
 
9c56a9c
 
17f4503
 
 
c5ca5f9
 
9c56a9c
c5ca5f9
 
17f4503
 
 
c5ca5f9
17f4503
 
 
 
c5ca5f9
17f4503
 
c5ca5f9
 
 
17f4503
 
c5ca5f9
17f4503
 
c5ca5f9
17f4503
 
 
 
 
c5ca5f9
17f4503
 
 
 
c5ca5f9
 
17f4503
 
 
 
 
 
 
 
c5ca5f9
17f4503
 
c5ca5f9
 
17f4503
 
 
 
c5ca5f9
 
17f4503
 
 
c5ca5f9
9c56a9c
17f4503
 
 
 
 
c5ca5f9
 
 
 
 
17f4503
c5ca5f9
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
"""Study AI API: app setup, error handling, CORS, and the route modules in backend/routes/."""
from dotenv import load_dotenv

# Load .env before importing backend modules: some of them read settings at import time.
load_dotenv()

import logging
import threading
from contextlib import asynccontextmanager

from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse

from backend import alerts, db, library, neon_auth, onenote_sync, preferences, rate_limit, reminders, settings
from backend.pipeline import models
from backend.auth import SESSION_HEADER
from backend.routes import account, admin, chat, files, onenote, study
from backend.routes import library as library_routes
from backend.settings import MAX_CHAT_FILES_PER_SESSION, MAX_UPLOAD_MB, allowed_origins

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
logger = logging.getLogger("notescanner")

def _warm_up_models() -> None:
    try:
        models.warm_up()
    except Exception:
        logger.exception("Loading the search models failed; they will load on the first question")


@asynccontextmanager
async def lifespan(_app: FastAPI):
    db.init()  # create the tables on first start (safe to repeat)
    threading.Thread(target=_warm_up_models, name="warm-up", daemon=True).start()
    reminders.start()  # daily review emails (only when SMTP is set up)
    library.start()  # re-checks the shared course library (only when LIBRARY_SYNC_HOURS is set)
    yield


app = FastAPI(title="Study AI API", lifespan=lifespan)


@app.middleware("http")
async def json_errors(request: Request, call_next):
    """Turn unexpected crashes into a JSON error. Added before CORS so the browser can read it."""
    try:
        return await call_next(request)
    except Exception as e:
        logger.exception("Unhandled error on %s %s", request.method, request.url.path)
        alerts.notify("server-error", f"{request.method} {request.url.path}: {type(e).__name__}: {e}")
        return JSONResponse(
            {"error": "Something went wrong on the server. Please try again."},
            status_code=500,
        )


@app.middleware("http")
async def request_limits(request: Request, call_next):
    """
    Overall cap on every request per network address and per signed-in browser, checked before
    any route runs (routes add their own tighter limits). Added before CORS so the 429 is readable.
    """
    if request.url.path not in settings.UNLIMITED_PATHS:
        rules = [("all-ip", rate_limit.client_ip(request), *settings.LIMIT_ALL_PER_IP, None)]
        session_id = (request.headers.get(SESSION_HEADER) or "").strip()
        if session_id:
            rules.append(("all-session", session_id, *settings.LIMIT_ALL_PER_SESSION, None))
        try:
            rate_limit.check_all(rules)
        except HTTPException as e:
            return JSONResponse({"detail": e.detail}, status_code=e.status_code, headers=e.headers)
    return await call_next(request)


app.add_middleware(
    CORSMiddleware,
    allow_origins=allowed_origins(),
    allow_credentials=False,  # auth uses the X-Session-Id header, not cookies
    allow_methods=["*"],
    allow_headers=["*"],
    expose_headers=["Content-Disposition", "Retry-After"],
)

for module in (account, files, chat, study, onenote, admin, library_routes):
    app.include_router(module.router)


@app.get("/health")
def health():
    """Liveness check for Docker / the hosting platform (doesn't touch the database)."""
    return {"status": "ok"}


@app.get("/config")
def public_config():
    """Feature flags the frontend needs before sign-in."""
    return {
        "password_reset_enabled": neon_auth.configured(),
        "onenote_configured": onenote_sync.is_configured(),
        "max_upload_mb": MAX_UPLOAD_MB,
        "max_chat_files": MAX_CHAT_FILES_PER_SESSION,
        "reminders_available": preferences.reminders_available(),
        "languages": list(settings.ANSWER_LANGUAGES),
    }


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)