changeset 2371:5e4321405f54

redo services url structure and homepage config. (pages aren't updated this layout yet)
author drewp@bigasterisk.com
date Thu, 08 Jun 2023 12:28:27 -0700
parents 18245a3a0f04
children 06bf6dae8e64
files bin/homepage light9/homepage/index.html light9/homepage/vite.config.ts light9/homepage/write_config.py light9/web/index.html light9/web/vite.config.ts
diffstat 6 files changed, 95 insertions(+), 106 deletions(-) [+]
line wrap: on
line diff
--- a/bin/homepage	Mon Jun 05 17:36:45 2023 -0700
+++ b/bin/homepage	Thu Jun 08 12:28:27 2023 -0700
@@ -1,39 +1,9 @@
 #!/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
--- a/light9/homepage/index.html	Mon Jun 05 17:36:45 2023 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-<!DOCTYPE html>
-<html>
-  <head>
-    <title>light9 home</title>
-    <meta charset="utf-8" />
-    <link rel="stylesheet" href="style.css" />
-    <script type="module" src="./ServiceButtonRow.ts"></script>
-  </head>
-  <body>
-    <h1>light9 home page</h1>
-
-    <div style="display: grid">
-      <service-button-row name="ascoltami" metrics="1"></service-button-row>
-      <service-button-row name="fade"></service-button-row>
-      <service-button-row name="effectListing"></service-button-row>
-      <service-button-row name="effectSequencer" metrics="1"></service-button-row>
-      <service-button-row name="collector" metrics="1"></service-button-row>
-      <service-button-row name="rdfdb" metrics="1"></service-button-row>
-    </div>
-  </body>
-</html>
--- a/light9/homepage/vite.config.ts	Mon Jun 05 17:36:45 2023 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-import { defineConfig } from "vite";
-
-const servicePort = 8200; // (not really, for homepage)
-export default defineConfig({
-  base: "/",
-  root: "./light9/homepage",
-  publicDir: "../web",
-  server: {
-    host: "0.0.0.0",
-    strictPort: true,
-    port: servicePort + 100,
-    hmr: {
-      port: servicePort + 200,
-    },
-  },
-  clearScreen: false,
-  define: {
-    global: {},
-  },
-});
--- a/light9/homepage/write_config.py	Mon Jun 05 17:36:45 2023 -0700
+++ b/light9/homepage/write_config.py	Thu Jun 08 12:28:27 2023 -0700
@@ -1,4 +1,15 @@
+'''
+
+ 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,44 +20,41 @@
 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}; 
-        }}
+  # for websocket
+  proxy_set_header Upgrade $http_upgrade;
+  proxy_set_header Connection "upgrade";
 
-        """,
-                  file=out)
+ server {{
+  listen {nginx_port};
+  access_log off;
+  autoindex on;''',
+              file=out)
 
         for role, server in sorted(graph.predicate_objects(netHome)):
             if not str(server).startswith('http') or role == L9['webServer']:
@@ -55,15 +63,26 @@
             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} {{
-          root {root};
-        }}""", file=out)
+        print(f'''
+  location /show {{
+    root {root};
+  }}
+
+  location / {{
+    proxy_pass http://localhost:8300;
+  }}
+ }}
+}}''', file=out)
 
 
 if __name__ == '__main__':
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/light9/web/index.html	Thu Jun 08 12:28:27 2023 -0700
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>light9 home</title>
+    <meta charset="utf-8" />
+    <link rel="stylesheet" href="style.css" />
+    <script type="module" src="./ServiceButtonRow.ts"></script>
+  </head>
+  <body>
+    <h1>light9 home page</h1>
+
+    <div style="display: grid">
+      <service-button-row name="ascoltami" metrics="1"></service-button-row>
+      <service-button-row name="fade"></service-button-row>
+      <service-button-row name="effectListing"></service-button-row>
+      <service-button-row name="effectSequencer" metrics="1"></service-button-row>
+      <service-button-row name="collector" metrics="1"></service-button-row>
+      <service-button-row name="rdfdb" metrics="1"></service-button-row>
+    </div>
+  </body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/light9/web/vite.config.ts	Thu Jun 08 12:28:27 2023 -0700
@@ -0,0 +1,20 @@
+import { defineConfig } from "vite";
+
+const servicePort = 8200; // (not really, for homepage)
+export default defineConfig({
+  base: "/",
+  root: "./light9/web",
+  publicDir: ".",
+  server: {
+    host: "0.0.0.0",
+    strictPort: true,
+    port: servicePort + 100,
+    hmr: {
+      port: servicePort + 200,
+    },
+  },
+  clearScreen: false,
+  define: {
+    global: {},
+  },
+});