fix: 🥅 containers api working

This commit is contained in:
Michel Roux 2024-11-03 21:24:49 +01:00
parent 699185ee76
commit cb65233e56
2 changed files with 26 additions and 9 deletions

View File

@ -2,6 +2,7 @@ from os import getenv, path
from typing import Annotated, Any
from docker import errors, from_env
from docker.models.containers import Container
from dotenv import load_dotenv
from fastapi import Depends, FastAPI, HTTPException, Request
from fastapi.security import HTTPBasic, HTTPBasicCredentials
@ -57,15 +58,31 @@ class AuthStaticFiles(StaticFiles):
app = FastAPI(dependencies=[Depends(check_auth)])
def serialize_container(container: Container) -> dict[str, Any]:
return {
"id": container.short_id,
"name": container.name,
"image": container.image.tags[0] if container.image else None,
"labels": container.labels,
"status": container.status,
"health": container.health,
"ports": container.ports,
"owner": container.labels.get("owner"),
"environment": container.attrs["Config"]["Env"],
}
@app.get("/api/containers")
def get_containers(
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
) -> list[str]:
) -> list[dict[str, Any]]:
if credentials.username == "admin":
return [container.name for container in client.containers.list()]
return [
serialize_container(container) for container in client.containers.list()
]
return [
container.name
serialize_container(container)
for container in client.containers.list(
filters={"label": f"owner={credentials.username}"}
)
@ -88,7 +105,7 @@ def get_container(
):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
return container.attrs
return serialize_container(container)
app.mount(
@ -100,5 +117,5 @@ app.mount(
)
def launch():
def launch() -> None:
run(app, host="0.0.0.0")

View File

@ -2,9 +2,9 @@
<Loading class="container" :loading="loading">
<Message v-if="error" type="danger">{{ error }}</Message>
<ul v-if="!loading && !error">
<li v-for="container in containers" :key="container">
<router-link :to="`/container/${container}`">
{{ container }}
<li v-for="container in containers" :key="container.id">
<router-link :to="`/container/${container.name}`">
{{ container.name }}
</router-link>
</li>
</ul>
@ -31,7 +31,7 @@ export default {
},
async mounted() {
try {
const response = await axios.get<string[]>('/api/containers')
const response = await axios.get('/api/containers')
this.containers = response.data
} catch (error) {
console.error(error)