Backend fixed and features
This commit is contained in:
@ -13,6 +13,8 @@ from fastapi.encoders import jsonable_encoder
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse, RedirectResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sshecret_backend.db import DatabaseSessionManager
|
||||
from starlette.middleware.sessions import SessionMiddleware
|
||||
@ -68,6 +70,14 @@ def create_admin_app(
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
app.add_middleware(SessionMiddleware, secret_key=settings.secret_key)
|
||||
origins = [ settings.frontend_origin ]
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"]
|
||||
)
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_exception_handler(
|
||||
|
||||
@ -2,8 +2,10 @@
|
||||
|
||||
import asyncio
|
||||
import code
|
||||
import json
|
||||
import logging
|
||||
from collections.abc import Awaitable
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
|
||||
import click
|
||||
@ -13,6 +15,7 @@ from sqlalchemy import select, create_engine
|
||||
from sqlalchemy.orm import Session
|
||||
from sshecret_admin.auth.authentication import hash_password
|
||||
from sshecret_admin.auth.models import AuthProvider, PasswordDB, User
|
||||
from sshecret_admin.core.app import create_admin_app
|
||||
from sshecret_admin.core.settings import AdminServerSettings
|
||||
from sshecret_admin.services.admin_backend import AdminBackend
|
||||
|
||||
@ -169,3 +172,20 @@ def cli_repl(ctx: click.Context) -> None:
|
||||
banner = "Sshecret-admin REPL\nAdmin backend API bound to 'admin'. Run async functions with run()"
|
||||
console = code.InteractiveConsole(locals=locals, local_exit=True)
|
||||
console.interact(banner=banner, exitmsg="Bye!")
|
||||
|
||||
@cli.command("openapi")
|
||||
@click.argument("destination", type=click.Path(file_okay=False, dir_okay=True, path_type=Path))
|
||||
@click.pass_context
|
||||
def cli_generate_openapi(ctx: click.Context, destination: Path) -> None:
|
||||
"""Generate openapi schema.
|
||||
|
||||
A openapi.json file will be written to the destination directory.
|
||||
"""
|
||||
settings = cast(AdminServerSettings, ctx.obj)
|
||||
app = create_admin_app(settings, with_frontend=False)
|
||||
schema = app.openapi()
|
||||
output_file = destination / "openapi.json"
|
||||
with open(output_file, "w") as f:
|
||||
json.dump(schema, f)
|
||||
|
||||
click.echo(f"Wrote schema to {output_file.absolute()}")
|
||||
|
||||
@ -39,6 +39,7 @@ class AdminServerSettings(BaseSettings):
|
||||
debug: bool = False
|
||||
password_manager_directory: Path | None = None
|
||||
oidc: OidcSettings | None = None
|
||||
frontend_origin: str = Field(default="*")
|
||||
|
||||
@property
|
||||
def admin_db(self) -> URL:
|
||||
|
||||
Reference in New Issue
Block a user