fix: 🥅 containers api working
This commit is contained in:
parent
699185ee76
commit
cb65233e56
@ -2,6 +2,7 @@ from os import getenv, path
|
|||||||
from typing import Annotated, Any
|
from typing import Annotated, Any
|
||||||
|
|
||||||
from docker import errors, from_env
|
from docker import errors, from_env
|
||||||
|
from docker.models.containers import Container
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from fastapi import Depends, FastAPI, HTTPException, Request
|
from fastapi import Depends, FastAPI, HTTPException, Request
|
||||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||||
@ -57,15 +58,31 @@ class AuthStaticFiles(StaticFiles):
|
|||||||
app = FastAPI(dependencies=[Depends(check_auth)])
|
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")
|
@app.get("/api/containers")
|
||||||
def get_containers(
|
def get_containers(
|
||||||
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
credentials: Annotated[HTTPBasicCredentials, Depends(security)],
|
||||||
) -> list[str]:
|
) -> list[dict[str, Any]]:
|
||||||
if credentials.username == "admin":
|
if credentials.username == "admin":
|
||||||
return [container.name for container in client.containers.list()]
|
return [
|
||||||
|
serialize_container(container) for container in client.containers.list()
|
||||||
|
]
|
||||||
|
|
||||||
return [
|
return [
|
||||||
container.name
|
serialize_container(container)
|
||||||
for container in client.containers.list(
|
for container in client.containers.list(
|
||||||
filters={"label": f"owner={credentials.username}"}
|
filters={"label": f"owner={credentials.username}"}
|
||||||
)
|
)
|
||||||
@ -88,7 +105,7 @@ def get_container(
|
|||||||
):
|
):
|
||||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
return container.attrs
|
return serialize_container(container)
|
||||||
|
|
||||||
|
|
||||||
app.mount(
|
app.mount(
|
||||||
@ -100,5 +117,5 @@ app.mount(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def launch():
|
def launch() -> None:
|
||||||
run(app, host="0.0.0.0")
|
run(app, host="0.0.0.0")
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
<Loading class="container" :loading="loading">
|
<Loading class="container" :loading="loading">
|
||||||
<Message v-if="error" type="danger">{{ error }}</Message>
|
<Message v-if="error" type="danger">{{ error }}</Message>
|
||||||
<ul v-if="!loading && !error">
|
<ul v-if="!loading && !error">
|
||||||
<li v-for="container in containers" :key="container">
|
<li v-for="container in containers" :key="container.id">
|
||||||
<router-link :to="`/container/${container}`">
|
<router-link :to="`/container/${container.name}`">
|
||||||
{{ container }}
|
{{ container.name }}
|
||||||
</router-link>
|
</router-link>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
@ -31,7 +31,7 @@ export default {
|
|||||||
},
|
},
|
||||||
async mounted() {
|
async mounted() {
|
||||||
try {
|
try {
|
||||||
const response = await axios.get<string[]>('/api/containers')
|
const response = await axios.get('/api/containers')
|
||||||
this.containers = response.data
|
this.containers = response.data
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
Loading…
Reference in New Issue
Block a user