diff --git a/docat/docat/app.py b/docat/docat/app.py index 555edda1..42cf495a 100644 --- a/docat/docat/app.py +++ b/docat/docat/app.py @@ -17,7 +17,7 @@ from typing import Optional import magic -from fastapi import Depends, FastAPI, File, Header, Response, UploadFile, status +from fastapi import APIRouter, Depends, FastAPI, File, Header, Response, UploadFile, status from fastapi.staticfiles import StaticFiles from starlette.responses import JSONResponse from tinydb import Query, TinyDB @@ -65,34 +65,29 @@ def get_db() -> TinyDB: redoc_url="/api/redoc", lifespan=lifespan, ) +router = APIRouter() -@app.get("/api/stats", response_model=Stats, status_code=status.HTTP_200_OK) +@router.get("/api/stats", response_model=Stats, status_code=status.HTTP_200_OK) def get_stats(): if not DOCAT_UPLOAD_FOLDER.exists(): return Projects(projects=[]) return get_system_stats(DOCAT_UPLOAD_FOLDER) -@app.get("/api/projects", response_model=Projects, status_code=status.HTTP_200_OK) +@router.get("/api/projects", response_model=Projects, status_code=status.HTTP_200_OK) def get_projects(include_hidden: bool = False): if not DOCAT_UPLOAD_FOLDER.exists(): return Projects(projects=[]) return get_all_projects(DOCAT_UPLOAD_FOLDER, include_hidden) -@app.get( +@router.get( "/api/projects/{project}", response_model=ProjectDetail, status_code=status.HTTP_200_OK, responses={status.HTTP_404_NOT_FOUND: {"model": ApiResponse}}, ) -@app.get( - "/api/projects/{project}/", - response_model=ProjectDetail, - status_code=status.HTTP_200_OK, - responses={status.HTTP_404_NOT_FOUND: {"model": ApiResponse}}, -) def get_project(project, include_hidden: bool = False): details = get_project_details(DOCAT_UPLOAD_FOLDER, project, include_hidden) @@ -102,8 +97,7 @@ def get_project(project, include_hidden: bool = False): return details -@app.post("/api/{project}/icon", response_model=ApiResponse, status_code=status.HTTP_200_OK) -@app.post("/api/{project}/icon/", response_model=ApiResponse, status_code=status.HTTP_200_OK) +@router.post("/api/{project}/icon", response_model=ApiResponse, status_code=status.HTTP_200_OK) def upload_icon( project: str, response: Response, @@ -147,8 +141,7 @@ def upload_icon( return ApiResponse(message="Icon successfully uploaded") -@app.post("/api/{project}/{version}/hide", response_model=ApiResponse, status_code=status.HTTP_200_OK) -@app.post("/api/{project}/{version}/hide/", response_model=ApiResponse, status_code=status.HTTP_200_OK) +@router.post("/api/{project}/{version}/hide", response_model=ApiResponse, status_code=status.HTTP_200_OK) def hide_version( project: str, version: str, @@ -183,8 +176,7 @@ def hide_version( return ApiResponse(message=f"Version {version} is now hidden") -@app.post("/api/{project}/{version}/show", response_model=ApiResponse, status_code=status.HTTP_200_OK) -@app.post("/api/{project}/{version}/show/", response_model=ApiResponse, status_code=status.HTTP_200_OK) +@router.post("/api/{project}/{version}/show", response_model=ApiResponse, status_code=status.HTTP_200_OK) def show_version( project: str, version: str, @@ -218,8 +210,7 @@ def show_version( return ApiResponse(message=f"Version {version} is now shown") -@app.post("/api/{project}/{version}", response_model=ApiResponse, status_code=status.HTTP_201_CREATED) -@app.post("/api/{project}/{version}/", response_model=ApiResponse, status_code=status.HTTP_201_CREATED) +@router.post("/api/{project}/{version}", response_model=ApiResponse, status_code=status.HTTP_201_CREATED) def upload( project: str, version: str, @@ -278,8 +269,7 @@ def upload( return ApiResponse(message="Documentation uploaded successfully") -@app.put("/api/{project}/{version}/tags/{new_tag}", response_model=ApiResponse, status_code=status.HTTP_201_CREATED) -@app.put("/api/{project}/{version}/tags/{new_tag}/", response_model=ApiResponse, status_code=status.HTTP_201_CREATED) +@router.put("/api/{project}/{version}/tags/{new_tag}", response_model=ApiResponse, status_code=status.HTTP_201_CREATED) def tag(project: str, version: str, new_tag: str, response: Response): destination = DOCAT_UPLOAD_FOLDER / project / new_tag source = DOCAT_UPLOAD_FOLDER / project / version @@ -295,18 +285,12 @@ def tag(project: str, version: str, new_tag: str, response: Response): return ApiResponse(message=f"Tag {new_tag} -> {version} successfully created") -@app.get( +@router.get( "/api/{project}/claim", response_model=ClaimResponse, status_code=status.HTTP_201_CREATED, responses={status.HTTP_409_CONFLICT: {"model": ApiResponse}}, ) -@app.get( - "/api/{project}/claim/", - response_model=ClaimResponse, - status_code=status.HTTP_201_CREATED, - responses={status.HTTP_409_CONFLICT: {"model": ApiResponse}}, -) def claim(project: str, db: TinyDB = Depends(get_db)): Project = Query() table = db.table("claims") @@ -322,8 +306,7 @@ def claim(project: str, db: TinyDB = Depends(get_db)): return ClaimResponse(message=f"Project {project} successfully claimed", token=token) -@app.put("/api/{project}/rename/{new_project_name}", response_model=ApiResponse, status_code=status.HTTP_200_OK) -@app.put("/api/{project}/rename/{new_project_name}/", response_model=ApiResponse, status_code=status.HTTP_200_OK) +@router.put("/api/{project}/rename/{new_project_name}", response_model=ApiResponse, status_code=status.HTTP_200_OK) def rename( project: str, new_project_name: str, @@ -362,8 +345,7 @@ def rename( return ApiResponse(message=f"Successfully renamed project {project} to {new_project_name}") -@app.delete("/api/{project}/{version}", response_model=ApiResponse, status_code=status.HTTP_200_OK) -@app.delete("/api/{project}/{version}/", response_model=ApiResponse, status_code=status.HTTP_200_OK) +@router.delete("/api/{project}/{version}", response_model=ApiResponse, status_code=status.HTTP_200_OK) def delete( project: str, version: str, @@ -407,3 +389,5 @@ def check_token_for_project(db, token, project) -> TokenStatus: if os.environ.get("DOCAT_SERVE_FILES"): DOCAT_UPLOAD_FOLDER.mkdir(parents=True, exist_ok=True) app.mount("/doc", StaticFiles(directory=DOCAT_UPLOAD_FOLDER, html=True), name="docs") + +app.include_router(router)