#!bin/python
from run_local import log
from light9 import showconfig
from light9.namespaces import L9
from urllib.parse import urlparse
from urllib.parse import splitport
log.info('generating config')
graph = showconfig.getGraph()
netHome = graph.value(showconfig.showUri(), L9['networking'])
webServer = graph.value(netHome, L9['webServer'])
if not webServer:
raise ValueError('no %r :webServer' % netHome)
print("listen %s;" % splitport(urlparse(webServer).netloc)[1])
def location(path, server, viteServer):
print(f"""
location = /{path}/metrics {{
rewrite ^/{path}(/.*) $1 break;
proxy_pass {server};
}}
location /{path}/api/ {{
# just the tail part for services
rewrite ^/{path}/api(/.*) $1 break;
proxy_pass {server};
}}
location /{path}/ {{
# vite has 'base' to deal with the /{path}/ part of the request
proxy_pass {viteServer};
}}
# vite seems to work without this old section-- hopefully other services will too!
# # for websocket
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection "upgrade";
# proxy_set_header Host $host;
# proxy_buffering off;
""")
for role, server in sorted(graph.predicate_objects(netHome)):
if not server.startswith('http') or role == L9['webServer']:
continue
path = graph.value(role, L9['urlPath'])
if not path:
continue
server = server.rstrip('/')
viteServer = server.replace('820', '830') # rewrite this please
location(path, server, viteServer)
showPath = showconfig.showUri().split('/', 3)[-1]
root = showconfig.root()[:-len(showPath)].decode('ascii')
print(f"""
location /{showPath} {{
root {root};
}}""")