2024-10-29 10:53:46 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
A music steaming application.
|
|
|
|
"""
|
|
|
|
import random
|
|
|
|
|
2024-12-20 21:49:51 -05:00
|
|
|
import asyncpg
|
2024-10-29 10:53:46 -04:00
|
|
|
from fastapi import FastAPI, Request
|
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
|
2024-12-20 21:49:51 -05:00
|
|
|
import config
|
2024-10-29 10:53:46 -04:00
|
|
|
#import buckler_fastapi
|
2024-12-20 21:49:51 -05:00
|
|
|
import database as db
|
2024-10-29 10:53:46 -04:00
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
#app.add_middleware(buckler_fastapi.BucklerSessionMiddleware)
|
|
|
|
|
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
2024-12-20 21:49:51 -05:00
|
|
|
|
2024-10-29 10:53:46 -04:00
|
|
|
@app.get("/", response_class=HTMLResponse)
|
|
|
|
async def index(request: Request):
|
|
|
|
context = {"request": request}
|
|
|
|
return templates.TemplateResponse('index.html', context)
|
|
|
|
|
2024-11-12 13:49:35 -05:00
|
|
|
|
2024-10-29 10:53:46 -04:00
|
|
|
@app.get("/rand_track/")
|
2024-12-20 21:49:51 -05:00
|
|
|
async def get_rand_track(request: Request):
|
2024-10-29 10:53:46 -04:00
|
|
|
"""Return a random track."""
|
2024-12-20 21:49:51 -05:00
|
|
|
track = await db.get_random_track(request)
|
|
|
|
return track
|
|
|
|
|
|
|
|
|
|
|
|
@app.on_event("startup")
|
|
|
|
async def startup():
|
|
|
|
app.state.db_pool = await asyncpg.create_pool(**config.db)
|