Add backend support for script execution
This commit is contained in:
@@ -4,6 +4,7 @@ from fastapi.exceptions import HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel
|
||||
from model import Log, SessionLocal, Script
|
||||
from run_scripts import run_scripts
|
||||
import uvicorn
|
||||
|
||||
app = FastAPI()
|
||||
@@ -28,15 +29,22 @@ class ScriptCreate(ScriptBase):
|
||||
pass
|
||||
|
||||
|
||||
class ScriptUpdate(ScriptBase):
|
||||
enabled: bool
|
||||
|
||||
|
||||
class ScriptResponse(ScriptBase):
|
||||
id: int
|
||||
created_at: datetime
|
||||
enabled: bool
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class ScriptLogCreate(BaseModel):
|
||||
message: str
|
||||
error_code: int
|
||||
error_message: str
|
||||
|
||||
|
||||
@app.get("/")
|
||||
@@ -80,19 +88,25 @@ def delete_script(script_id: int):
|
||||
if not script:
|
||||
raise HTTPException(status_code=404, detail="Script not found")
|
||||
db.delete(script)
|
||||
logs = db.query(Log).filter(Log.script_id == script_id).all()
|
||||
for log in logs:
|
||||
db.delete(log)
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
return {"message": "Script deleted"}
|
||||
|
||||
|
||||
@app.put("/script/{script_id}", response_model=ScriptResponse)
|
||||
def update_script(script_id: int, script: ScriptCreate):
|
||||
def update_script(script_id: int, script: ScriptUpdate):
|
||||
db = SessionLocal()
|
||||
existing_script = db.query(Script).filter(Script.id == script_id).first()
|
||||
if not existing_script:
|
||||
raise HTTPException(status_code=404, detail="Script not found")
|
||||
existing_script.name = script.name
|
||||
existing_script.script_content = script.script_content
|
||||
existing_script.enabled = script.enabled
|
||||
|
||||
db.commit()
|
||||
db.refresh(existing_script)
|
||||
db.close()
|
||||
@@ -110,7 +124,12 @@ def get_script_logs(script_id: int):
|
||||
@app.post("/script/{script_id}/log")
|
||||
def create_script_log(script_id: int, log: ScriptLogCreate):
|
||||
db = SessionLocal()
|
||||
new_log = Log(script_id=script_id, message=log.message)
|
||||
new_log = Log(
|
||||
script_id=script_id,
|
||||
message=log.message,
|
||||
error_code=log.error_code,
|
||||
error_message=log.error_message,
|
||||
)
|
||||
db.add(new_log)
|
||||
db.commit()
|
||||
db.refresh(new_log)
|
||||
@@ -130,6 +149,12 @@ def delete_script_log(script_id: int, log_id: int):
|
||||
return {"message": "Log deleted"}
|
||||
|
||||
|
||||
@app.post("/script/{script_id}/execute")
|
||||
def execute_script(script_id: int):
|
||||
run_scripts([script_id])
|
||||
return {"run_script": True}
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
def health_check():
|
||||
return {"status": "healthy"}
|
||||
|
||||
Reference in New Issue
Block a user