From 2b75aac67c2aa8ab21e9798a0c96b9ca25ffe923 Mon Sep 17 00:00:00 2001 From: magmaus3 Date: Sat, 5 Nov 2022 16:29:00 +0100 Subject: [PATCH] Add configuration --- docker-compose.yml | 5 +++++ iwm_browser/main.py | 14 ++++++-------- iwm_browser/utils.py | 13 ++++++++++++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 10f75c1..955218a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,3 +5,8 @@ services: build: . volumes: - .:/app + environment: + - IWM_SERVER="http://make.fangam.es" + - IWM_THUMBNAILS="https://images.make.fangam.es" + - IMGPROXY_URL="http://127.0.0.1:8080" + - IMGPROXY_PARAMS="{\"extension\":\"webp\",\"advanced\":[\"q:50\"]}" diff --git a/iwm_browser/main.py b/iwm_browser/main.py index f42f562..be8c38e 100644 --- a/iwm_browser/main.py +++ b/iwm_browser/main.py @@ -13,7 +13,7 @@ from . import __version__ basedir = os.path.dirname(__file__) -app = FastAPI() +app = FastAPI(openapi_url=None) app.mount("/static", StaticFiles(directory=basedir + "/static"), name="static") template_env = jinja2.Environment( @@ -26,15 +26,13 @@ template_env.globals["convert_times"] = utils.convert_times template_env.globals["generate_thumbnail_url"] = utils.generate_thumbnail_url template_env.globals["__version__"] = __version__ -BASE_URL = "http://make.fangam.es" -THUMB_URL = "https://images.make.fangam.es" -utils.global_imgproxy_url = ( - "http://127.0.0.1:8080" # Set it to None to disable the proxy -) -utils.global_imgproxy_params = { +BASE_URL = utils.config_value("IWM_SERVER", "http://make.fangam.es") +THUMB_URL = utils.config_value("IWM_THUMBNAILS", "https://images.make.fangam.es") +utils.global_imgproxy_url = utils.config_value("IMGPROXY_URL", None) +utils.global_imgproxy_params = json.loads(utils.config_value("IMGPROXY_PARAMS", '''{ "extension": "webp", "advanced": ["q:50"], -} # Set it to None to disable the proxy params +}''')) # Set it to None to disable the proxy params # Matches level code. # \S[A-Z0-9]{4}\-[A-Z0-9]{4} = With dash, no whitespace diff --git a/iwm_browser/utils.py b/iwm_browser/utils.py index 59f1e45..948b8c0 100644 --- a/iwm_browser/utils.py +++ b/iwm_browser/utils.py @@ -1,5 +1,6 @@ from imgproxy import ImgProxy - +import json +import os def convert_times(time): # Not sure if it's the right way to do that, but it seems to work. @@ -15,6 +16,16 @@ def convert_times(time): return (hours, minutes, seconds) +def config_value(key: str, default_value=None): + """Searches for environ value for the specified key, + or returns the default value. + + The value must be in json format!""" + if key in os.environ: + return json.loads(os.environ[key]) + else: + return default_value + # Specify default imgproxy instance. global_imgproxy_url = None