undate Docker + workflows
Some checks failed
Build and Push Docker Image / build (push) Failing after 48s
Some checks failed
Build and Push Docker Image / build (push) Failing after 48s
This commit is contained in:
22
main.py
22
main.py
@@ -1,17 +1,24 @@
|
||||
import sqlite3
|
||||
import os
|
||||
from fastapi import FastAPI
|
||||
|
||||
# 📁 Dateiname der Datenbank
|
||||
DB_NAME = "hangman.db"
|
||||
# 📁 Konfiguration: Lese Pfad aus ENV oder nutze Standard
|
||||
# Im Docker Container setzen wir dies später auf /app/data/hangman.db
|
||||
DB_PATH = os.getenv("DB_PATH", "hangman.db")
|
||||
|
||||
# 🛠️ Datenbank-Hilfsfunktionen
|
||||
def get_db_connection():
|
||||
"""Erstellt eine Verbindung zur SQLite-Datenbank."""
|
||||
conn = sqlite3.connect(DB_NAME)
|
||||
# Prüfen, ob das Verzeichnis existiert, falls es ein absoluter Pfad ist
|
||||
directory = os.path.dirname(DB_PATH)
|
||||
if directory and not os.path.exists(directory):
|
||||
os.makedirs(directory, exist_ok=True)
|
||||
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
# 💡 Initialisiere die FastAPI-Anwendung (ohne Lifespan/Init-Logik)
|
||||
# 💡 Initialisiere die FastAPI-Anwendung
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/random_word")
|
||||
@@ -31,8 +38,7 @@ def get_random_word():
|
||||
if result:
|
||||
return {"word": result["word"]}
|
||||
else:
|
||||
return {"error": "Keine Wörter in der Datenbank gefunden (Bitte init.py ausführen)."}
|
||||
return {"error": f"Keine Wörter in {DB_PATH} gefunden."}
|
||||
|
||||
except sqlite3.OperationalError:
|
||||
# Fängt den Fall ab, dass die Tabelle noch nicht existiert
|
||||
return {"error": "Datenbankfehler: Wurde init.py ausgeführt?"}
|
||||
except sqlite3.OperationalError as e:
|
||||
return {"error": f"Datenbankfehler bei {DB_PATH}: {str(e)}"}
|
||||
Reference in New Issue
Block a user