"""Render the nginx config for the current MONTY_UI mode, then exec nginx. Picked up by supervisord's [program:nginx] entry. Reads the MONTY_UI env var (default 'vue'), substitutes the chosen upstream into nginx.conf.template, writes the result to /etc/nginx/nginx.conf, and execs nginx in foreground mode so supervisord keeps tracking the same PID. """ import os import sys TEMPLATE_PATH = '/etc/nginx/nginx.conf.template' TARGET_PATH = '/etc/nginx/nginx.conf' UPSTREAM_PLACEHOLDER = '__MONTY_UPSTREAM__' CACHE_VALID_PLACEHOLDER = '__INGESTION_CACHE_VALID__' INGESTION_CACHE_VALID = '3m' UPSTREAMS = { 'vue': 'flask_backend', 'streamlit': 'streamlit_backend', } def main(): """Render the nginx config and exec nginx in foreground.""" mode = os.environ.get('MONTY_UI', 'vue').strip().lower() upstream = UPSTREAMS.get(mode) if upstream is None: choices = ', '.join(sorted(UPSTREAMS)) sys.stderr.write( f'nginx-entrypoint: MONTY_UI={mode!r} is not supported ' f'(expected one of: {choices})\n', ) sys.exit(1) with open(TEMPLATE_PATH) as src: rendered = ( src.read() .replace(UPSTREAM_PLACEHOLDER, upstream) .replace(CACHE_VALID_PLACEHOLDER, INGESTION_CACHE_VALID) ) with open(TARGET_PATH, 'w') as dst: dst.write(rendered) print( f'nginx-entrypoint: MONTY_UI={mode} -> upstream={upstream}, ' f'ingestion_cache_valid={INGESTION_CACHE_VALID}', flush=True) os.execvp('nginx', [ 'nginx', '-c', TARGET_PATH, '-g', 'daemon off;']) if __name__ == '__main__': main()