Changeset - 5e4321405f54
[Not reviewed]
default
2 2 2
drewp@bigasterisk.com - 19 months ago 2023-06-08 19:28:27
drewp@bigasterisk.com
redo services url structure and homepage config. (pages aren't updated this layout yet)
4 files changed with 50 insertions and 61 deletions:
0 comments (0 inline, 0 general)
bin/homepage
Show inline comments
 
#!/bin/zsh
 

	
 
CONF=/tmp/light9_nginx.conf
 
ROOT=`dirname $0`/..
 
ROOT=${ROOT:a}
 

	
 
bin/python light9/homepage/write_config.py /tmp/light9_nginx_routes.conf && \
 
cat > $CONF <<EOF
 
worker_processes 1;
 

	
 
daemon off;
 
error_log /tmp/light9_homepage.err;
 
pid /dev/null;
 

	
 
events {
 
  worker_connections 1024;
 
}
 

	
 
http {
 
 include $ROOT/light9/web/mime.types;
 
 
 
 proxy_buffering off;
 

	
 
 server {
 
  access_log off;
 
  autoindex on;
 
bin/python light9/homepage/write_config.py $CONF
 

	
 
  include "/tmp/light9_nginx_routes.conf";
 

	
 
  location / {
 
    proxy_pass http://localhost:8300;
 
  }
 

	
 
 }
 
}
 
EOF
 
pnpm vite -c light9/homepage/vite.config.ts &
 
pnpm vite -c light9/web/vite.config.ts &
 
/usr/sbin/nginx -c $CONF
 
wait
light9/homepage/write_config.py
Show inline comments
 
'''
 

	
 
 goal (everything under localhost:8200):
 
   /                          light9/web/index.html
 
   /effects                   light9/web/effects/index.html
 
   /collector/                light9/web/collector/index.html
 
   /show/dance2023/URI        light9/show/dance2023/URI
 
   /service/collector/        localhost:8302
 
   /service/collector/metrics localhost:8302/metrics
 
'''
 
import sys
 
from pathlib import Path
 
from urllib.parse import urlparse
 

	
 
from light9 import showconfig
 
@@ -9,43 +20,40 @@ from light9.run_local import log
 
def main():
 
    [outPath] = sys.argv[1:]
 

	
 
    log.info('generating config')
 
    log.info(f'writing nginx config to {outPath}')
 
    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)
 

	
 
    mime_types = Path(__file__).parent.parent / 'web/mime.types'
 
    nginx_port = urlparse(str(webServer)).port
 
    with open(outPath, 'wt') as out:
 
        line = "listen %s;" % urlparse(str(webServer)).port
 
        print(line, file=out)
 
        log.info(line)
 
        print(f'''
 
worker_processes 1;
 

	
 
        def location(path, server, viteServer):
 
            print(f"""
 
daemon off;
 
error_log /tmp/light9_homepage.err;
 
pid /dev/null;
 

	
 
        location = /{path}/metrics {{
 
          rewrite ^/{path}(/.*) $1 break;
 
          proxy_pass {server}; 
 
events {{
 
  worker_connections 1024;
 
        }}
 

	
 
        location /{path}/api/ {{
 
          # just the tail part for services
 
          rewrite ^/{path}/api(/.*) $1 break;
 
          proxy_pass {server}; 
 
http {{
 
 include {mime_types};
 

	
 
  proxy_buffering off;
 
  proxy_http_version 1.1;
 

	
 
          # for websocket
 
          proxy_http_version 1.1;
 
          proxy_set_header Upgrade $http_upgrade;
 
          proxy_set_header Connection "upgrade";
 
          proxy_set_header Host $host;
 
        }}
 
        location /{path}/ {{
 
          # vite has 'base' to deal with the /{path}/ part of the request
 
          proxy_pass {viteServer}; 
 
        }}
 

	
 
        """,
 
 server {{
 
  listen {nginx_port};
 
  access_log off;
 
  autoindex on;''',
 
                  file=out)
 

	
 
        for role, server in sorted(graph.predicate_objects(netHome)):
 
@@ -55,15 +63,26 @@ def main():
 
            if not path:
 
                continue
 
            server = str(server).rstrip('/')
 
            viteServer = server.replace('82', '83')  # rewrite this please
 
            location(path, server, viteServer)
 
            print(f'''
 
  location = /{path} {{ rewrite (.*) $1/ permanent; }}
 
  location /service/{path}/ {{
 
    rewrite ^/service/{path}(/.*) $1 break;
 
    proxy_pass {server};
 
  }}''',
 
                  file=out)
 

	
 
        showPath = showconfig.showUri().split('/', 3)[-1]
 
        root = showconfig.root()[:-len(showPath)].decode('ascii')
 
        print(f"""
 
        location /{showPath} {{
 
        print(f'''
 
  location /show {{
 
          root {root};
 
        }}""", file=out)
 
  }}
 

	
 
  location / {{
 
    proxy_pass http://localhost:8300;
 
  }}
 
 }}
 
}}''', file=out)
 

	
 

	
 
if __name__ == '__main__':
light9/web/index.html
Show inline comments
 
file renamed from light9/homepage/index.html to light9/web/index.html
light9/web/vite.config.ts
Show inline comments
 
file renamed from light9/homepage/vite.config.ts to light9/web/vite.config.ts
 
@@ -3,8 +3,8 @@ import { defineConfig } from "vite";
 
const servicePort = 8200; // (not really, for homepage)
 
export default defineConfig({
 
  base: "/",
 
  root: "./light9/homepage",
 
  publicDir: "../web",
 
  root: "./light9/web",
 
  publicDir: ".",
 
  server: {
 
    host: "0.0.0.0",
 
    strictPort: true,
0 comments (0 inline, 0 general)