fixed app.py

This commit is contained in:
enrilinux
2026-07-19 21:52:02 +02:00
parent a0b38ad75b
commit 2ca9dacb8b
2 changed files with 82 additions and 39 deletions
+35 -18
View File
@@ -1,36 +1,53 @@
name: build-and-push-docker
# .gitea/workflows/build.yaml
name: Build and Push Docker Image
# Il workflow viene attivato su push
on:
push:
branches:
- main
- 'main'
- 'master'
# Questa sezione è fondamentale per evitare build quando non necessario
# Gitea ignorerà automaticamente i commit che contengono queste stringhe [citation:10]
# Aggiungi "[skip ci]" al tuo messaggio di commit per saltare la build
jobs:
build:
build-and-push:
runs-on: ubuntu-latest
container:
image: docker:24-git
steps:
- name: Checkout codice
- name: Checkout code
uses: actions/checkout@v4
- name: Login al registry Gitea
uses: docker/login-action@v3
with:
registry: gitea.enrilinux.ovh
username: ${{ secrets.REGISTRY_USER }}
password: ${{ secrets.REGISTRY_PASSWORD }}
# Questi due step preparano l'ambiente per build multi-architettura
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Setup Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build e push immagine
- name: Log in to Gitea Container Registry
uses: docker/login-action@v3
with:
# IMPORTANTE: Sostituisci con il dominio del tuo Gitea (es. gitea.example.com)
registry: gitea.enrilinux.ovh
username: ${{ secrets.GITEA_USERNAME }}
password: ${{ secrets.GITEA_TOKEN }}
# Estrae il nome del repository e il tag (commit SHA) per l'immagine
- name: Extract metadata
id: meta
run: |
echo "REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F'/' '{print $2}')" >> $GITHUB_OUTPUT
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
# IMPORTANTE: Sostituisci "gitea.example.com" con il dominio del tuo Gitea
# e "username" con il tuo nome utente o organizzazione
tags: |
gitea.enrilinux.ovh/enrilinux/webdlp:latest
gitea.enrilinux.ovh/enrilinux/webdlp:${{ github.sha }}
gitea.enrilinux.ovh/${{ github.repository_owner }}/${{ steps.meta.outputs.REPO_NAME }}:latest
gitea.enrilinux.ovh/${{ github.repository_owner }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.SHORT_SHA }}
+32 -6
View File
@@ -1,6 +1,5 @@
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
from fastapi.templating import Jinja2Templates
import os
import json
from datetime import datetime
@@ -9,10 +8,9 @@ from app.downloader import download_video
app = FastAPI()
templates = Jinja2Templates(directory="app/templates")
DOWNLOAD_DIR = "downloads"
HISTORY_FILE = "history.json"
LOG_FILE = "data/app.log"
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
os.makedirs("data", exist_ok=True)
@@ -21,6 +19,10 @@ if not os.path.exists(HISTORY_FILE):
with open(HISTORY_FILE, "w") as f:
json.dump([], f)
if not os.path.exists(LOG_FILE):
with open(LOG_FILE, "w") as f:
f.write("")
def write_log(message):
with open(LOG_FILE, "a") as f:
f.write(f"{datetime.now().strftime('%H:%M:%S')} - {message}\n")
@@ -28,14 +30,26 @@ def write_log(message):
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
# Legge il file HTML direttamente
try:
with open("/app/app/templates/index.html", "r") as f:
html_content = f.read()
return HTMLResponse(content=html_content)
except Exception as e:
return HTMLResponse(content=f"<h1>Errore</h1><p>{str(e)}</p>", status_code=500)
@app.get("/test")
async def test():
return {"status": "ok", "message": "Server funzionante"}
@app.post("/download")
async def download(url: str = Form(...), mode: str = Form(...)):
try:
path = download_video(url, mode)
write_log(f"Downloaded: {url} -> {path}")
# salva nella cronologia
with open(HISTORY_FILE, "r") as f:
history = json.load(f)
@@ -50,14 +64,26 @@ async def download(url: str = Form(...), mode: str = Form(...)):
json.dump(history, f, indent=2)
return {"path": path}
except Exception as e:
write_log(f"Error: {str(e)}")
return JSONResponse(status_code=500, content={"error": str(e)})
@app.get("/history")
async def history():
try:
with open(HISTORY_FILE) as f:
return JSONResponse(json.load(f))
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.get("/file")
async def file(path: str):
return FileResponse(path)
try:
safe_path = os.path.join(DOWNLOAD_DIR, os.path.basename(path))
if not os.path.exists(safe_path):
return JSONResponse(status_code=404, content={"error": "File not found"})
return FileResponse(safe_path)
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})