48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
A music steaming application.
|
|
"""
|
|
import random
|
|
|
|
import asyncpg
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.templating import Jinja2Templates
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
import config
|
|
#import buckler_fastapi
|
|
import database as db
|
|
|
|
app = FastAPI()
|
|
#app.add_middleware(buckler_fastapi.BucklerSessionMiddleware)
|
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
|
@app.get("/", response_class=HTMLResponse)
|
|
async def index(request: Request):
|
|
artists = await db.get_artists(request)
|
|
context = {'request': request, 'artists': artists}
|
|
return templates.TemplateResponse('index.html', context)
|
|
|
|
|
|
@app.get("/rand_track/")
|
|
async def get_rand_track(request: Request):
|
|
"""Return a random track."""
|
|
track = await db.get_random_track(request)
|
|
return track
|
|
|
|
|
|
@app.get("/albums/")
|
|
async def get_albums(request: Request, albumartist: str):
|
|
"""Return all albums associated with a particular albumartist."""
|
|
albums = await db.get_albums(request, albumartist)
|
|
return albums
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup():
|
|
app.state.db_pool = await asyncpg.create_pool(**config.db)
|