asd
This commit is contained in:
@@ -23,6 +23,7 @@ class ServiceCreate(BaseModel):
|
||||
name: str
|
||||
path: str
|
||||
git_url: Optional[str] = None
|
||||
alias: Optional[str] = None
|
||||
|
||||
class ServiceAction(BaseModel):
|
||||
action: str
|
||||
@@ -215,8 +216,8 @@ async def create_service(service: ServiceCreate):
|
||||
raise HTTPException(status_code=404, detail=f"docker-compose file not found in {service_path}")
|
||||
|
||||
service_id = await conn.fetchval(
|
||||
"INSERT INTO services (name, path, git_url, status) VALUES ($1, $2, $3, $4) RETURNING id",
|
||||
service.name, service_path, service.git_url, "stopped"
|
||||
"INSERT INTO services (name, path, git_url, alias, status) VALUES ($1, $2, $3, $4, $5) RETURNING id",
|
||||
service.name, service_path, service.git_url, service.alias, "stopped"
|
||||
)
|
||||
|
||||
await log_service_action(conn, service_id, "register", "success", "Service registered")
|
||||
@@ -278,6 +279,36 @@ async def get_service(service_id: int):
|
||||
finally:
|
||||
await release_db(conn)
|
||||
|
||||
@app.patch("/services/{service_id}", dependencies=[Depends(verify_token)])
|
||||
async def update_service(service_id: int, updates: dict):
|
||||
"""Update service fields (alias, git_url, etc.)"""
|
||||
conn = await get_db()
|
||||
try:
|
||||
service = await conn.fetchrow("SELECT * FROM services WHERE id = $1", service_id)
|
||||
if not service:
|
||||
raise HTTPException(status_code=404, detail="Service not found")
|
||||
|
||||
# Allowed fields to update
|
||||
allowed_fields = {"alias", "git_url"}
|
||||
update_fields = {k: v for k, v in updates.items() if k in allowed_fields and v is not None}
|
||||
|
||||
if not update_fields:
|
||||
return dict(service)
|
||||
|
||||
# Build dynamic UPDATE query
|
||||
set_clauses = [f"{field} = ${i+1}" for i, field in enumerate(update_fields.keys())]
|
||||
values = list(update_fields.values()) + [service_id]
|
||||
|
||||
await conn.execute(
|
||||
f"UPDATE services SET {', '.join(set_clauses)}, last_updated = ${ len(values)} WHERE id = ${len(values)+1}",
|
||||
*values, datetime.utcnow(), service_id
|
||||
)
|
||||
|
||||
updated_service = await conn.fetchrow("SELECT * FROM services WHERE id = $1", service_id)
|
||||
return dict(updated_service)
|
||||
finally:
|
||||
await release_db(conn)
|
||||
|
||||
@app.post("/services/{service_id}/action", dependencies=[Depends(verify_token)])
|
||||
async def service_action(service_id: int, action: ServiceAction):
|
||||
"""Start, stop, or restart a service"""
|
||||
|
||||
Reference in New Issue
Block a user