Check in backend in working state
This commit is contained in:
@ -1,11 +1,18 @@
|
||||
"""CLI and main entry point."""
|
||||
|
||||
import code
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import cast
|
||||
from dotenv import load_dotenv
|
||||
import click
|
||||
from sqlmodel import Session, create_engine, select
|
||||
import uvicorn
|
||||
|
||||
from .db import generate_api_token
|
||||
from .db import create_api_token
|
||||
|
||||
from .models import Client, ClientSecret, ClientAccessPolicy, AuditLog, APIClient
|
||||
from .settings import BackendSettings
|
||||
|
||||
DEFAULT_LISTEN = "127.0.0.1"
|
||||
DEFAULT_PORT = 8022
|
||||
@ -14,18 +21,59 @@ WORKDIR = Path(os.getcwd())
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.option("--database", help="Path to the sqlite database file.")
|
||||
def cli(database: str) -> None:
|
||||
@click.pass_context
|
||||
def cli(ctx: click.Context, database: str) -> None:
|
||||
"""CLI group."""
|
||||
if database:
|
||||
# Hopefully it's enough to set the environment variable as so.
|
||||
os.environ["SSHECRET_DB_FILE"] = str(Path(database).absolute())
|
||||
settings = BackendSettings(db_url=f"sqlite:///{Path(database).absolute()}")
|
||||
else:
|
||||
settings = BackendSettings()
|
||||
|
||||
ctx.obj = settings
|
||||
|
||||
|
||||
@cli.command("generate-token")
|
||||
def cli_generate_token() -> None:
|
||||
@click.pass_context
|
||||
def cli_generate_token(ctx: click.Context) -> None:
|
||||
"""Generate a token."""
|
||||
token = generate_api_token()
|
||||
settings = cast(BackendSettings, ctx.obj)
|
||||
engine = create_engine(settings.db_url)
|
||||
with Session(engine) as session:
|
||||
token = create_api_token(session, True)
|
||||
click.echo("Generated api token:")
|
||||
click.echo(token)
|
||||
|
||||
@cli.command("run")
|
||||
@click.option("--host", default="127.0.0.1")
|
||||
@click.option("--port", default=8022, type=click.INT)
|
||||
@click.option("--dev", is_flag=True)
|
||||
@click.option("--workers", type=click.INT)
|
||||
def cli_run(host: str, port: int, dev: bool, workers: int | None) -> None:
|
||||
"""Run the server."""
|
||||
uvicorn.run("sshecret_backend.main:app", host=host, port=port, reload=dev, workers=workers)
|
||||
|
||||
@cli.command("repl")
|
||||
@click.pass_context
|
||||
def cli_repl(ctx: click.Context) -> None:
|
||||
"""Run an interactive console."""
|
||||
settings = cast(BackendSettings, ctx.obj)
|
||||
engine = create_engine(settings.db_url)
|
||||
|
||||
with Session(engine) as session:
|
||||
locals = {
|
||||
"session": session,
|
||||
"select": select,
|
||||
"Client": Client,
|
||||
"ClientSecret": ClientSecret,
|
||||
"ClientAccessPolicy": ClientAccessPolicy,
|
||||
"APIClient": APIClient,
|
||||
"AuditLog": AuditLog,
|
||||
}
|
||||
|
||||
console = code.InteractiveConsole(locals=locals, local_exit=True)
|
||||
banner = "Sshecret-backend REPL.\nUse 'session' to interact with the database."
|
||||
console.interact(banner=banner, exitmsg="Bye!")
|
||||
|
||||
Reference in New Issue
Block a user