From 9456665a6f6ef321364be26e98de3c2967a6554d Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sat, 3 Aug 2024 22:40:26 +0200 Subject: [PATCH] Preparation for alternative context path. The contextpath parameter in converge.go is temporary and should be removed later. What is needed is autodetectio of the context path for the usage page and passing on the context for rendering. --- cmd/agent/agent.go | 4 ---- cmd/converge/converge.go | 29 ++++++++++++++++++++--------- pkg/server/templates/basepage.templ | 4 +++- pkg/server/templates/sessions.templ | 2 +- pkg/server/templates/usage.templ | 2 +- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cmd/agent/agent.go b/cmd/agent/agent.go index fe64882..897a5b1 100755 --- a/cmd/agent/agent.go +++ b/cmd/agent/agent.go @@ -286,10 +286,6 @@ func main() { if err != nil { printHelp(fmt.Sprintf("Invalid URL %s", wsURL)) } - if url.Path != "" && url.Path != "/" { - printHelp(fmt.Sprintf("Only a base URL without path may be specified: %s", wsURL)) - } - wsURL += "/agent/" + id dialer := websocket.Dialer{ diff --git a/cmd/converge/converge.go b/cmd/converge/converge.go index 7076b5a..a057524 100644 --- a/cmd/converge/converge.go +++ b/cmd/converge/converge.go @@ -19,7 +19,7 @@ import ( ) func parsePublicId(path string) (publicId string, _ error) { - pattern := regexp.MustCompile("^/[^/]+/([^/]+)$") + pattern := regexp.MustCompile("/([^/]+)$") matches := pattern.FindStringSubmatch(path) if len(matches) != 2 { return "", fmt.Errorf("Invalid URL path '%s'", path) @@ -159,20 +159,31 @@ func main() { } // websocket endpoints - http.HandleFunc("/agent/", registrationService.Handle) - http.HandleFunc("/client/", clientService.Handle) - http.HandleFunc("/ws/sessions", sessionService.Handle) + + // TODO remove, simulate contextpath + + contextpath := "" + http.HandleFunc(contextpath+"/agent/", registrationService.Handle) + http.HandleFunc(contextpath+"/client/", clientService.Handle) + http.HandleFunc(contextpath+"/ws/sessions", sessionService.Handle) // create filehandler with templating for html files. - http.Handle("/docs/", http.StripPrefix("/docs/", http.HandlerFunc(pageHandler))) - http.Handle("/static/", http.StripPrefix("/static/", + http.Handle(contextpath+"/docs/", http.StripPrefix(contextpath+"/docs/", http.HandlerFunc(pageHandler))) + http.Handle(contextpath+"/static/", http.StripPrefix(contextpath+"/static/", http.FileServer(http.Dir(staticdir)))) - http.Handle("/downloads/", http.StripPrefix("/downloads/", + http.Handle(contextpath+"/downloads/", http.StripPrefix(contextpath+"/downloads/", http.FileServer(http.Dir(downloaddir)))) - http.HandleFunc("/", catchAllHandler) + + // TODO remove for testing contextpath + + catchAllHandler2 := func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, contextpath+"/docs", http.StatusFound) + return + } + http.HandleFunc(contextpath+"/", catchAllHandler2) // create usage generator - http.HandleFunc("/usage", generateCLIExammple) + http.HandleFunc(contextpath+"/usage", generateCLIExammple) // Start HTTP server fmt.Println("Rendez-vous server listening on :8000") diff --git a/pkg/server/templates/basepage.templ b/pkg/server/templates/basepage.templ index a60f62a..d0cb04d 100644 --- a/pkg/server/templates/basepage.templ +++ b/pkg/server/templates/basepage.templ @@ -35,6 +35,8 @@ templ BasePage(tab int) { console.log("timezone override for websockets") window.originalWebSocket = htmx.createWebSocket htmx.createWebSocket = function(url) { + url = new URL(url, window.location.href).href + url = url.replace(/^http/, 'ws'); let modifiedUrl = url + "?timezone=" + getTimezone() return window.originalWebSocket(modifiedUrl) } @@ -43,7 +45,7 @@ templ BasePage(tab int) { document.body.addEventListener( "htmx:configRequest", function(evt) { - console.log("Adding timezone to htmx request headers"); + console.log("Adding timezone to htmx request headers and making URL absolute"); evt.detail.headers["X-Timezone"] = getTimezone(); } ); diff --git a/pkg/server/templates/sessions.templ b/pkg/server/templates/sessions.templ index 8a1eb96..6886cc9 100644 --- a/pkg/server/templates/sessions.templ +++ b/pkg/server/templates/sessions.templ @@ -9,7 +9,7 @@ import ( templ Sessions(state *models.State, loc *time.Location) { -
+

sessions

diff --git a/pkg/server/templates/usage.templ b/pkg/server/templates/usage.templ index 5ca7cc5..5a5e65d 100644 --- a/pkg/server/templates/usage.templ +++ b/pkg/server/templates/usage.templ @@ -146,7 +146,7 @@ templ Usage(access models.ConvergeAccess) {