changeset 2174:891c380afcc1

move py code under light9, add import test
author drewp@bigasterisk.com
date Fri, 19 May 2023 13:59:07 -0700
parents f239dedb025a
children 28bcd763303c
files bin/homepage bin/homepageConfig light9/homepage/write_config.py light9/homepage/write_config_test.py
diffstat 4 files changed, 77 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/bin/homepage	Fri May 19 13:46:08 2023 -0700
+++ b/bin/homepage	Fri May 19 13:59:07 2023 -0700
@@ -4,7 +4,7 @@
 ROOT=`dirname $0`/..
 ROOT=${ROOT:a}
 
-bin/homepageConfig > /tmp/light9_nginx_routes.conf && \
+bin/python light9/homepage/write_config.py /tmp/light9_nginx_routes.conf && \
 cat > $CONF <<EOF
 worker_processes 1;
 
@@ -32,7 +32,6 @@
  }
 }
 EOF
-head -1 /tmp/light9_nginx_routes.conf
 pnpm vite -c light9/homepage/vite.config.ts &
 /usr/sbin/nginx -c $CONF
 wait
--- a/bin/homepageConfig	Fri May 19 13:46:08 2023 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,62 +0,0 @@
-#!bin/python
-from run_local import log
-from light9 import showconfig
-from light9.namespaces import L9
-from urllib.parse import urlparse
-
-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;" % urlparse(webServer).port)
-
-
-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}; 
-
-      # 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;
-
-    }}
-    location /{path}/ {{
-      # vite has 'base' to deal with the /{path}/ part of the request
-      proxy_pass {viteServer}; 
-    }}
-
-    """)
-
-
-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('82', '83') # 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};
-    }}""")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/light9/homepage/write_config.py	Fri May 19 13:59:07 2023 -0700
@@ -0,0 +1,73 @@
+import sys
+from urllib.parse import urlparse
+
+from light9 import showconfig
+from light9.namespaces import L9
+from light9.run_local import log
+
+
+def main():
+    [outPath] = sys.argv[1:]
+
+    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)
+
+    with open(outPath, 'wt') as out:
+        line = "listen %s;" % urlparse(str(webServer)).port
+        print(line, file=out)
+        log.info(line)
+
+        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}; 
+
+          # 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;
+
+        }}
+        location /{path}/ {{
+          # vite has 'base' to deal with the /{path}/ part of the request
+          proxy_pass {viteServer}; 
+        }}
+
+        """,
+                  file=out)
+
+        for role, server in sorted(graph.predicate_objects(netHome)):
+            if not str(server).startswith('http') or role == L9['webServer']:
+                continue
+            path = graph.value(role, L9['urlPath'])
+            if not path:
+                continue
+            server = str(server).rstrip('/')
+            viteServer = server.replace('82', '83')  # 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};
+        }}""", file=out)
+
+
+if __name__ == '__main__':
+    main()
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/light9/homepage/write_config_test.py	Fri May 19 13:59:07 2023 -0700
@@ -0,0 +1,3 @@
+def test_import():
+    import write_config
+    # no crash