Skip to content

Commit

Permalink
feat: make favicon configurable as blueprint
Browse files Browse the repository at this point in the history
... instead of supporting the explicit `favicon` configuration option.
  • Loading branch information
mgoltzsche committed Feb 25, 2024
1 parent 6fc6e60 commit 1b33570
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 18 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include README.md
include beetsplug/webrouter/config_default.yaml
include beetsplug/webrouter/static/favicon.ico
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ webrouter:
routes:
/:
plugin: web
/favicon.ico:
plugin: webrouter.favicon
/subsonic:
plugin: beetstream
config:
Expand Down
35 changes: 19 additions & 16 deletions beetsplug/webrouter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import re
import importlib
from flask import Flask, send_file, abort
from flask import Flask
from beets.plugins import BeetsPlugin
from beets.ui import Subcommand, decargs
from beets import config
Expand Down Expand Up @@ -37,14 +37,14 @@ def _run_cmd(self, lib, opts, args):
for k,v in self.config['routes'].items():
plugin = v['plugin'].get()
if plugin:
mod = importlib.import_module('beetsplug.{}'.format(plugin))
mod = importlib.import_module(f'beetsplug.{plugin}')
has_create_app = hasattr(mod, 'create_app')
has_app = hasattr(mod, 'app')
if 'blueprint' in v and v['blueprint'].get():
blueprint_routes.append({
'prefix': k,
'blueprint': getattr(mod, v['blueprint'].get()),
})
else:
if hasattr(mod, 'create_app'):
blueprint_routes.append(BlueprintRoute(k,
getattr(mod, v['blueprint'].get())))
elif has_app or has_create_app:
if has_create_app:
modapp = mod.create_app()
else:
modapp = mod.app
Expand All @@ -53,15 +53,13 @@ def _run_cmd(self, lib, opts, args):
app = modapp
else:
routes[k] = modapp
elif hasattr(mod, 'bp'):
blueprint_routes.append(BlueprintRoute(k, getattr(mod, 'bp')))
else:
raise Exception(f"webrouter: cannot register route to plugin '{plugin}' since it neither specifies a 'create_app' function, a Flask app 'app' nor a Blueprint 'bp' nor is a Blueprint name configured!")

for e in blueprint_routes:
app.register_blueprint(e['blueprint'], url_prefix=e['prefix'])

icon = self.config['favicon'].get()
if icon:
@app.route('/favicon.ico')
def favicon():
return send_file(icon, mimetype='image/x-icon')
for r in blueprint_routes:
app.register_blueprint(r.blueprint, url_prefix=r.url_prefix)

app.wsgi_app = DispatcherMiddleware(app.wsgi_app, routes)

Expand Down Expand Up @@ -96,3 +94,8 @@ def _configure_app(self, app, cfg, lib):

if self.config['reverse_proxy']:
app.wsgi_app = ReverseProxied(app.wsgi_app)

class BlueprintRoute:
def __init__(self, url_prefix, blueprint):
self.url_prefix = url_prefix
self.blueprint = blueprint
1 change: 0 additions & 1 deletion beetsplug/webrouter/config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ cors_supports_credentials: false
reverse_proxy: false
include_paths: false
readonly: true
favicon: ""
routes:
/:
plugin: web
8 changes: 8 additions & 0 deletions beetsplug/webrouter/favicon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask import Blueprint

bp = Blueprint('webrouterfavicon_bp', __name__, static_folder='static',
static_url_path='/static')

@bp.route('')
def favicon():
return bp.send_static_file('favicon.ico')
Binary file added beetsplug/webrouter/static/favicon.ico
Binary file not shown.
3 changes: 2 additions & 1 deletion example_beets_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ plugins:
- smartplaylist

webrouter:
favicon: /favicon.ico
routes:
/:
plugin: web
/favicon.ico:
plugin: webrouter.favicon
/subsonic:
plugin: beetstream
config:
Expand Down

0 comments on commit 1b33570

Please sign in to comment.