init
This commit is contained in:
5
backend/config.py
Normal file
5
backend/config.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from pathlib import Path
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
FRONTEND_DIST = PROJECT_ROOT / "frontend" / "dist"
|
||||
DB_PATH = PROJECT_ROOT / "creator.db"
|
||||
30
backend/database.py
Normal file
30
backend/database.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import aiosqlite
|
||||
|
||||
from config import DB_PATH
|
||||
|
||||
_db: aiosqlite.Connection | None = None
|
||||
|
||||
|
||||
async def get_db() -> aiosqlite.Connection:
|
||||
global _db
|
||||
if _db is None:
|
||||
_db = await aiosqlite.connect(DB_PATH)
|
||||
return _db
|
||||
|
||||
|
||||
async def init_db():
|
||||
db = await get_db()
|
||||
# Tabellen folgen, sobald die ersten Features stehen.
|
||||
await db.commit()
|
||||
|
||||
|
||||
async def close_db():
|
||||
global _db
|
||||
if _db is not None:
|
||||
await _db.close()
|
||||
_db = None
|
||||
|
||||
|
||||
def _row_to_dict(row, cursor):
|
||||
columns = [d[0] for d in cursor.description]
|
||||
return dict(zip(columns, row))
|
||||
23
backend/main.py
Normal file
23
backend/main.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from config import FRONTEND_DIST
|
||||
from database import init_db, close_db
|
||||
from routes import router
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
await init_db()
|
||||
yield
|
||||
await close_db()
|
||||
|
||||
|
||||
app = FastAPI(title="Creator", lifespan=lifespan)
|
||||
|
||||
app.include_router(router)
|
||||
|
||||
if FRONTEND_DIST.exists():
|
||||
app.mount("/", StaticFiles(directory=FRONTEND_DIST, html=True), name="frontend")
|
||||
3
backend/requirements.txt
Normal file
3
backend/requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
fastapi
|
||||
uvicorn[standard]
|
||||
aiosqlite
|
||||
8
backend/routes.py
Normal file
8
backend/routes.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/api")
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
async def health():
|
||||
return {"ok": True}
|
||||
Reference in New Issue
Block a user