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
+34 -17
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: on:
push: push:
branches: 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: jobs:
build: build-and-push:
runs-on: ubuntu-latest runs-on: ubuntu-latest
container:
image: docker:24-git
steps: steps:
- name: Checkout codice - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Login al registry Gitea # Questi due step preparano l'ambiente per build multi-architettura
uses: docker/login-action@v3 - name: Set up QEMU
with: uses: docker/setup-qemu-action@v3
registry: gitea.enrilinux.ovh
username: ${{ secrets.REGISTRY_USER }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 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 uses: docker/build-push-action@v5
with: with:
context: . context: .
push: true push: true
# IMPORTANTE: Sostituisci "gitea.example.com" con il dominio del tuo Gitea
# e "username" con il tuo nome utente o organizzazione
tags: | tags: |
gitea.enrilinux.ovh/enrilinux/webdlp:latest gitea.enrilinux.ovh/${{ github.repository_owner }}/${{ steps.meta.outputs.REPO_NAME }}:latest
gitea.enrilinux.ovh/enrilinux/webdlp:${{ github.sha }} 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 import FastAPI, Request, Form
from fastapi.responses import HTMLResponse, FileResponse, JSONResponse from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
from fastapi.templating import Jinja2Templates
import os import os
import json import json
from datetime import datetime from datetime import datetime
@@ -9,10 +8,9 @@ from app.downloader import download_video
app = FastAPI() app = FastAPI()
templates = Jinja2Templates(directory="app/templates")
DOWNLOAD_DIR = "downloads" DOWNLOAD_DIR = "downloads"
HISTORY_FILE = "history.json" HISTORY_FILE = "history.json"
LOG_FILE = "data/app.log"
os.makedirs(DOWNLOAD_DIR, exist_ok=True) os.makedirs(DOWNLOAD_DIR, exist_ok=True)
os.makedirs("data", 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: with open(HISTORY_FILE, "w") as f:
json.dump([], f) json.dump([], f)
if not os.path.exists(LOG_FILE):
with open(LOG_FILE, "w") as f:
f.write("")
def write_log(message): def write_log(message):
with open(LOG_FILE, "a") as f: with open(LOG_FILE, "a") as f:
f.write(f"{datetime.now().strftime('%H:%M:%S')} - {message}\n") f.write(f"{datetime.now().strftime('%H:%M:%S')} - {message}\n")
@@ -28,14 +30,26 @@ def write_log(message):
@app.get("/", response_class=HTMLResponse) @app.get("/", response_class=HTMLResponse)
async def home(request: Request): 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") @app.post("/download")
async def download(url: str = Form(...), mode: str = Form(...)): async def download(url: str = Form(...), mode: str = Form(...)):
try:
path = download_video(url, mode) path = download_video(url, mode)
write_log(f"Downloaded: {url} -> {path}")
# salva nella cronologia
with open(HISTORY_FILE, "r") as f: with open(HISTORY_FILE, "r") as f:
history = json.load(f) history = json.load(f)
@@ -50,14 +64,26 @@ async def download(url: str = Form(...), mode: str = Form(...)):
json.dump(history, f, indent=2) json.dump(history, f, indent=2)
return {"path": path} 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") @app.get("/history")
async def history(): async def history():
try:
with open(HISTORY_FILE) as f: with open(HISTORY_FILE) as f:
return JSONResponse(json.load(f)) return JSONResponse(json.load(f))
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.get("/file") @app.get("/file")
async def file(path: str): 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)})