2021-06-13 09:43:30 +00:00
|
|
|
"""App entrypoint."""
|
|
|
|
|
2021-06-13 14:24:03 +00:00
|
|
|
import logging
|
|
|
|
|
2021-06-13 09:43:30 +00:00
|
|
|
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 keycloak_collective_portal.config import (
|
2021-06-13 14:24:03 +00:00
|
|
|
APP_LOG_LEVEL,
|
2021-06-13 09:43:30 +00:00
|
|
|
APP_SECRET_KEY,
|
|
|
|
APP_THEME,
|
|
|
|
REDIS_DB,
|
|
|
|
REDIS_HOST,
|
|
|
|
REDIS_PORT,
|
|
|
|
STATIC_DIR,
|
|
|
|
TEMPLATE_DIR,
|
|
|
|
)
|
|
|
|
from keycloak_collective_portal.exceptions import RequiresLoginException
|
|
|
|
from keycloak_collective_portal.keycloak import init_keycloak
|
|
|
|
from keycloak_collective_portal.oidc import init_oidc
|
|
|
|
from keycloak_collective_portal.redis import Redis
|
|
|
|
from keycloak_collective_portal.routes import (
|
|
|
|
health,
|
|
|
|
invite,
|
|
|
|
oidc,
|
|
|
|
register,
|
|
|
|
root,
|
|
|
|
)
|
|
|
|
|
2021-06-13 14:24:03 +00:00
|
|
|
log = logging.getLogger("uvicorn")
|
|
|
|
log.setLevel(APP_LOG_LEVEL)
|
|
|
|
|
2021-06-13 09:43:30 +00:00
|
|
|
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")
|
2021-06-13 10:50:13 +00:00
|
|
|
async def startup_event():
|
2021-06-13 09:43:30 +00:00
|
|
|
redis = Redis()
|
|
|
|
app.state.redis = await redis.create_pool(
|
|
|
|
f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}?encoding=utf-8"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
|
|
async def shutdown_event():
|
2021-06-13 10:58:24 +00:00
|
|
|
await app.state.redis.close()
|
2021-06-13 09:43:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
app.add_middleware(SessionMiddleware, secret_key=APP_SECRET_KEY)
|
|
|
|
|
|
|
|
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
|
|
|
|
|
|
|
app.state.oidc = init_oidc()
|
2021-06-13 14:24:03 +00:00
|
|
|
log.info("Initialised OpenID Connect client (for Keycloak logins)")
|
|
|
|
|
2021-06-13 09:43:30 +00:00
|
|
|
app.state.keycloak = init_keycloak()
|
2021-06-13 14:24:03 +00:00
|
|
|
log.info("Initialised Keycloak admin client (for Keycloak REST API)")
|
|
|
|
|
2021-06-13 09:43:30 +00:00
|
|
|
app.state.templates = Jinja2Templates(directory=TEMPLATE_DIR)
|
|
|
|
app.state.theme = APP_THEME
|
2021-06-13 14:24:03 +00:00
|
|
|
app.state.log = log
|
2021-06-13 09:43:30 +00:00
|
|
|
|
|
|
|
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)
|