From 1387664f0b9d07bc0fb0e3e44ae5a1060ca43d93 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sun, 3 Nov 2024 17:04:28 +0100 Subject: [PATCH] feat: :sparkles: add list containers --- backend/__main__.py | 1 - backend/docker.py | 5 ++ backend/http.py | 31 +++++-- frontend/App.vue | 2 +- frontend/main.ts | 2 +- poetry.lock | 203 +++++++++++++++++++++++++++++++++++++++++++- pyproject.toml | 4 +- 7 files changed, 237 insertions(+), 11 deletions(-) create mode 100644 backend/docker.py diff --git a/backend/__main__.py b/backend/__main__.py index 00fe97a..5775d03 100644 --- a/backend/__main__.py +++ b/backend/__main__.py @@ -4,5 +4,4 @@ from uvicorn import run from .http import app load_dotenv() - run(app, host="0.0.0.0") diff --git a/backend/docker.py b/backend/docker.py new file mode 100644 index 0000000..6aac51d --- /dev/null +++ b/backend/docker.py @@ -0,0 +1,5 @@ +from docker import from_env +from dotenv import load_dotenv + +load_dotenv() +client = from_env() diff --git a/backend/http.py b/backend/http.py index 04e4d57..747f597 100644 --- a/backend/http.py +++ b/backend/http.py @@ -1,12 +1,14 @@ +from os import path from typing import Annotated from fastapi import Depends, FastAPI, HTTPException from fastapi.security import HTTPBasic, HTTPBasicCredentials from fastapi.staticfiles import StaticFiles from sqlmodel import Session, and_, select -from starlette.status import HTTP_403_FORBIDDEN +from starlette import status from .db import User, create_db_and_tables, engine, get_session +from .docker import client security = HTTPBasic() @@ -23,7 +25,7 @@ def check_auth(credentials: Annotated[HTTPBasicCredentials, Depends(security)]) if not user: raise HTTPException( - status_code=HTTP_403_FORBIDDEN, + status_code=status.HTTP_403_FORBIDDEN, detail="Invalid authentication credentials", ) @@ -31,7 +33,6 @@ def check_auth(credentials: Annotated[HTTPBasicCredentials, Depends(security)]) app = FastAPI(dependencies=[Depends(get_session), Depends(check_auth)]) -app.mount("/", StaticFiles(directory="dist", html=True), name="static") @app.on_event("startup") @@ -39,6 +40,24 @@ def on_startup() -> None: create_db_and_tables() -@app.get("/") -def hello_world() -> str: - return "

Hello, World!

" +@app.get("/api/containers") +def get_containers( + session: Annotated[Session, Depends(get_session)], + credentials: Annotated[HTTPBasicCredentials, Depends(security)], +): + if credentials.username == "admin": + return [container.name for container in client.containers.list()] + + return [ + container.name + for container in client.containers.list( + filters={"label": f"owner={credentials.username}"} + ) + ] + + +app.mount( + "/", + StaticFiles(directory=f"{path.dirname(path.realpath(__file__))}/dist", html=True), + name="static", +) diff --git a/frontend/App.vue b/frontend/App.vue index 6e4ca9e..871e859 100644 --- a/frontend/App.vue +++ b/frontend/App.vue @@ -12,7 +12,7 @@