Some checks failed
CI / style (push) Has been cancelled
CI / build-client (push) Has been cancelled
CI / build-amd64 (push) Has been cancelled
CI / build-tester (push) Has been cancelled
CI / test (cockroachdb) (push) Has been cancelled
CI / test (mariadb) (push) Has been cancelled
CI / test (postgres) (push) Has been cancelled
CI / test (sqlite) (push) Has been cancelled
CI / build-arm64 (push) Has been cancelled
CI / build-armv7 (push) Has been cancelled
CI / publish-images (push) Has been cancelled
CI / publish-release (push) Has been cancelled
## Batch Operations (#13) - Add POST /spool/bulk/archive, /bulk/delete, /bulk/update endpoints - Add row selection with checkboxes to spool list - Add floating BatchActionBar with archive/unarchive/delete/move actions - Add confirmation modals for all batch operations ## Dashboard Redesign (#12) - Install recharts for pie charts - Add AlertCards (low stock, pending jobs, needs weighing) - Add MaterialDistributionChart (pie chart by material) - Add QuickStats row (total spools, filaments, vendors, weight) - Refactor home page layout with new components ## Hierarchical Locations (#9) - Add Location model with self-referential parent relationship - Add migration for location table and spool.location_id FK - Add location database operations (CRUD, tree, full path) - Add location API endpoints (list, tree, CRUD) - Add LocationTreeSelect component with nested tree display - Add LocationCreateModal for inline location creation - Keep legacy location string field for backward compatibility Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
120 lines
3.6 KiB
Python
120 lines
3.6 KiB
Python
"""Router setup for the v1 version of the API."""
|
|
|
|
# ruff: noqa: D103
|
|
|
|
import asyncio
|
|
import logging
|
|
|
|
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
|
from fastapi.responses import JSONResponse
|
|
from starlette.requests import Request
|
|
from starlette.responses import Response
|
|
|
|
from spoolman import env
|
|
from spoolman.database.database import backup_global_db
|
|
from spoolman.exceptions import ItemNotFoundError
|
|
from spoolman.ws import websocket_manager
|
|
|
|
from . import auth, backup as backup_module, export, externaldb, field, filament, location as location_module, models, other, print_job, setting, spool, vendor
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
app = FastAPI(
|
|
title="Spoolman REST API v1",
|
|
version="1.0.0",
|
|
description="""
|
|
REST API for Spoolman.
|
|
|
|
The API is served on the path `/api/v1/`.
|
|
|
|
Some endpoints also serve a websocket on the same path. The websocket is used to listen for changes to the data
|
|
that the endpoint serves. The websocket messages are JSON objects. Additionally, there is a root-level websocket
|
|
endpoint that listens for changes to any data in the database.
|
|
""",
|
|
)
|
|
|
|
|
|
@app.exception_handler(ItemNotFoundError)
|
|
async def itemnotfounderror_exception_handler(_request: Request, exc: ItemNotFoundError) -> Response:
|
|
logger.debug(exc, exc_info=True)
|
|
return JSONResponse(
|
|
status_code=404,
|
|
content={"message": exc.args[0]},
|
|
)
|
|
|
|
|
|
# Add a general info endpoint
|
|
@app.get("/info")
|
|
async def info() -> models.Info:
|
|
"""Return general info about the API."""
|
|
return models.Info(
|
|
version=env.get_version(),
|
|
debug_mode=env.is_debug_mode(),
|
|
automatic_backups=env.is_automatic_backup_enabled(),
|
|
data_dir=str(env.get_data_dir().resolve()),
|
|
logs_dir=str(env.get_logs_dir().resolve()),
|
|
backups_dir=str(env.get_backups_dir().resolve()),
|
|
db_type=str(env.get_database_type() or "sqlite"),
|
|
git_commit=env.get_commit_hash(),
|
|
build_date=env.get_build_date(),
|
|
auth_enabled=auth.is_auth_enabled(),
|
|
)
|
|
|
|
|
|
# Add health check endpoint
|
|
@app.get("/health")
|
|
async def health() -> models.HealthCheck:
|
|
"""Return a health check."""
|
|
return models.HealthCheck(status="healthy")
|
|
|
|
|
|
# Add endpoint for triggering a db backup
|
|
@app.post(
|
|
"/backup",
|
|
description="Trigger a database backup. Only applicable for SQLite databases.",
|
|
response_model=models.BackupResponse,
|
|
responses={500: {"model": models.Message}},
|
|
)
|
|
async def backup(): # noqa: ANN201
|
|
"""Trigger a database backup."""
|
|
path = await backup_global_db()
|
|
if path is None:
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"message": "Backup failed. See server logs for more information."},
|
|
)
|
|
return models.BackupResponse(path=str(path))
|
|
|
|
|
|
@app.websocket(
|
|
"/",
|
|
name="Listen to any changes",
|
|
)
|
|
async def notify(
|
|
websocket: WebSocket,
|
|
) -> None:
|
|
await websocket.accept()
|
|
websocket_manager.connect((), websocket)
|
|
try:
|
|
while True:
|
|
await asyncio.sleep(0.5)
|
|
if await websocket.receive_text():
|
|
await websocket.send_json({"status": "healthy"})
|
|
except WebSocketDisconnect:
|
|
websocket_manager.disconnect((), websocket)
|
|
|
|
|
|
# Add routers
|
|
app.include_router(filament.router)
|
|
app.include_router(spool.router)
|
|
app.include_router(vendor.router)
|
|
app.include_router(setting.router)
|
|
app.include_router(field.router)
|
|
app.include_router(other.router)
|
|
app.include_router(externaldb.router)
|
|
app.include_router(export.router)
|
|
app.include_router(backup_module.router)
|
|
app.include_router(print_job.router)
|
|
app.include_router(auth.router)
|
|
app.include_router(location_module.router)
|