2022-11-24 13:45:09 -08:00
|
|
|
import json
|
2022-11-20 01:00:54 -08:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
|
|
|
import bottle
|
|
|
|
from bottle import HTTPResponse
|
|
|
|
from bottle.ext import sqlite
|
|
|
|
|
2022-11-19 16:06:23 -08:00
|
|
|
from groove.auth import is_authenticated
|
2022-11-24 13:45:09 -08:00
|
|
|
from groove.playlist import Playlist
|
2022-11-20 01:00:54 -08:00
|
|
|
|
|
|
|
server = bottle.Bottle()
|
2022-11-19 15:14:12 -08:00
|
|
|
|
2022-11-20 01:00:54 -08:00
|
|
|
|
2022-11-20 16:44:33 -08:00
|
|
|
def start(host: str, port: int, debug: bool) -> None: # pragma: no cover
|
2022-11-20 01:00:54 -08:00
|
|
|
"""
|
2022-11-20 09:28:00 -08:00
|
|
|
Start the Bottle app.
|
2022-11-20 01:00:54 -08:00
|
|
|
"""
|
2022-11-20 09:28:00 -08:00
|
|
|
logging.debug(f"Configuring sqllite using {os.environ.get('DATABASE_PATH')}")
|
2022-11-20 01:00:54 -08:00
|
|
|
server.install(sqlite.Plugin(dbfile=os.environ.get('DATABASE_PATH')))
|
2022-11-20 09:28:00 -08:00
|
|
|
logging.debug(f"Configuring webserver with host={host}, port={port}, debug={debug}")
|
|
|
|
server.run(
|
|
|
|
host=os.getenv('HOST', host),
|
|
|
|
port=os.getenv('PORT', port),
|
|
|
|
debug=debug,
|
|
|
|
server='paste',
|
|
|
|
quiet=True
|
|
|
|
)
|
2022-11-19 15:14:12 -08:00
|
|
|
|
|
|
|
|
|
|
|
@server.route('/')
|
|
|
|
def index():
|
|
|
|
return "Groovy."
|
2022-11-19 16:06:23 -08:00
|
|
|
|
|
|
|
|
2022-11-20 01:00:54 -08:00
|
|
|
@server.route('/build')
|
|
|
|
@bottle.auth_basic(is_authenticated)
|
|
|
|
def build():
|
2022-11-19 16:06:23 -08:00
|
|
|
return "Authenticated. Groovy."
|
2022-11-20 01:00:54 -08:00
|
|
|
|
|
|
|
|
|
|
|
@server.route('/playlist/<slug>')
|
|
|
|
def get_playlist(slug, db):
|
|
|
|
"""
|
|
|
|
Retrieve a playlist and its entries by a slug.
|
|
|
|
"""
|
|
|
|
logging.debug(f"Looking up playlist: {slug}...")
|
2022-11-24 13:45:09 -08:00
|
|
|
playlist = Playlist(slug=slug, conn=db)
|
|
|
|
if not playlist.exists:
|
2022-11-20 01:00:54 -08:00
|
|
|
return HTTPResponse(status=404, body="Not found")
|
2022-11-24 13:45:09 -08:00
|
|
|
response = json.dumps(playlist.as_dict)
|
|
|
|
logging.debug(response)
|
|
|
|
return HTTPResponse(status=200, content_type='application/json', body=response)
|