2022-01-10 12:34:17 +00:00
|
|
|
"""App entrypoint."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from fastapi import FastAPI, Request
|
|
|
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
from starlette.exceptions import HTTPException
|
|
|
|
from starlette.middleware.sessions import SessionMiddleware
|
|
|
|
|
|
|
|
from members_lumbung_space.config import (
|
|
|
|
APP_LOG_LEVEL,
|
|
|
|
APP_SECRET_KEY,
|
|
|
|
APP_THEME,
|
|
|
|
STATIC_DIR,
|
|
|
|
TEMPLATE_DIR,
|
|
|
|
)
|
|
|
|
from members_lumbung_space.exceptions import RequiresLoginException
|
|
|
|
from members_lumbung_space.keycloak import init_keycloak
|
2022-01-10 15:20:47 +00:00
|
|
|
from members_lumbung_space.nextcloud import init_resource_map
|
2022-01-10 12:34:17 +00:00
|
|
|
from members_lumbung_space.oidc import init_oidc
|
2022-01-10 15:20:47 +00:00
|
|
|
from members_lumbung_space.redis import Redis, init_redis
|
2022-01-10 15:31:09 +00:00
|
|
|
from members_lumbung_space.routes import (
|
|
|
|
health,
|
|
|
|
invite,
|
|
|
|
oidc,
|
|
|
|
register,
|
|
|
|
resource,
|
|
|
|
root,
|
|
|
|
)
|
2022-01-10 12:34:17 +00:00
|
|
|
|
|
|
|
log = logging.getLogger("uvicorn")
|
|
|
|
log.setLevel(APP_LOG_LEVEL)
|
|
|
|
|
|
|
|
app = FastAPI(docs_url=None, redoc_url=None)
|
|
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(RequiresLoginException)
|
|
|
|
async def requires_login(request, exception):
|
|
|
|
return RedirectResponse(request.url_for("login"))
|
|
|
|
|
|
|
|
|
|
|
|
@app.exception_handler(HTTPException)
|
|
|
|
async def http_exception_handler(request, exc):
|
|
|
|
home = request.url_for("login")
|
|
|
|
return HTMLResponse(f"<p>{exc.detail} (<a href='{home}'>home</a>)</p>")
|
|
|
|
|
|
|
|
|
|
|
|
@app.on_event("startup")
|
|
|
|
async def startup_event():
|
2022-01-10 15:20:47 +00:00
|
|
|
await init_redis(app)
|
|
|
|
log.info("Initialised redis connection")
|
|
|
|
|
|
|
|
await init_resource_map(app)
|
|
|
|
log.info("Initialised the resource map")
|
2022-01-10 12:34:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
|
|
async def shutdown_event():
|
|
|
|
await app.state.redis.close()
|
|
|
|
|
|
|
|
|
|
|
|
app.add_middleware(SessionMiddleware, secret_key=APP_SECRET_KEY)
|
|
|
|
|
|
|
|
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
|
|
|
|
|
|
|
app.state.oidc = init_oidc()
|
|
|
|
log.info("Initialised OpenID Connect client (for Keycloak logins)")
|
|
|
|
|
|
|
|
app.state.keycloak = init_keycloak()
|
|
|
|
log.info("Initialised Keycloak admin client (for Keycloak REST API)")
|
|
|
|
|
|
|
|
app.state.templates = Jinja2Templates(directory=TEMPLATE_DIR)
|
|
|
|
app.state.theme = APP_THEME
|
|
|
|
app.state.log = log
|
|
|
|
|
|
|
|
app.include_router(invite.router)
|
|
|
|
app.include_router(oidc.router)
|
|
|
|
app.include_router(register.router)
|
|
|
|
app.include_router(root.router)
|
|
|
|
app.include_router(health.router)
|
2022-01-10 15:31:09 +00:00
|
|
|
app.include_router(resource.router)
|