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.
This commit is contained in:
Erik Brakkee 2024-08-03 22:40:26 +02:00
parent a6bbafe593
commit b875540d6b
5 changed files with 25 additions and 16 deletions

View File

@ -286,10 +286,6 @@ func main() {
if err != nil { if err != nil {
printHelp(fmt.Sprintf("Invalid URL %s", wsURL)) 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 wsURL += "/agent/" + id
dialer := websocket.Dialer{ dialer := websocket.Dialer{

View File

@ -19,7 +19,7 @@ import (
) )
func parsePublicId(path string) (publicId string, _ error) { func parsePublicId(path string) (publicId string, _ error) {
pattern := regexp.MustCompile("^/[^/]+/([^/]+)$") pattern := regexp.MustCompile("/([^/]+)$")
matches := pattern.FindStringSubmatch(path) matches := pattern.FindStringSubmatch(path)
if len(matches) != 2 { if len(matches) != 2 {
return "", fmt.Errorf("Invalid URL path '%s'", path) return "", fmt.Errorf("Invalid URL path '%s'", path)
@ -159,20 +159,31 @@ func main() {
} }
// websocket endpoints // websocket endpoints
http.HandleFunc("/agent/", registrationService.Handle)
http.HandleFunc("/client/", clientService.Handle) // TODO remove, simulate contextpath
http.HandleFunc("/ws/sessions", sessionService.Handle)
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. // create filehandler with templating for html files.
http.Handle("/docs/", http.StripPrefix("/docs/", http.HandlerFunc(pageHandler))) http.Handle(contextpath+"/docs/", http.StripPrefix(contextpath+"/docs/", http.HandlerFunc(pageHandler)))
http.Handle("/static/", http.StripPrefix("/static/", http.Handle(contextpath+"/static/", http.StripPrefix(contextpath+"/static/",
http.FileServer(http.Dir(staticdir)))) 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.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 // create usage generator
http.HandleFunc("/usage", generateCLIExammple) http.HandleFunc(contextpath+"/usage", generateCLIExammple)
// Start HTTP server // Start HTTP server
fmt.Println("Rendez-vous server listening on :8000") fmt.Println("Rendez-vous server listening on :8000")

View File

@ -35,6 +35,8 @@ templ BasePage(tab int) {
console.log("timezone override for websockets") console.log("timezone override for websockets")
window.originalWebSocket = htmx.createWebSocket window.originalWebSocket = htmx.createWebSocket
htmx.createWebSocket = function(url) { htmx.createWebSocket = function(url) {
url = new URL(url, window.location.href).href
url = url.replace(/^http/, 'ws');
let modifiedUrl = url + "?timezone=" + getTimezone() let modifiedUrl = url + "?timezone=" + getTimezone()
return window.originalWebSocket(modifiedUrl) return window.originalWebSocket(modifiedUrl)
} }
@ -43,7 +45,7 @@ templ BasePage(tab int) {
document.body.addEventListener( document.body.addEventListener(
"htmx:configRequest", "htmx:configRequest",
function(evt) { 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(); evt.detail.headers["X-Timezone"] = getTimezone();
} }
); );

View File

@ -9,7 +9,7 @@ import (
templ Sessions(state *models.State, loc *time.Location) { templ Sessions(state *models.State, loc *time.Location) {
<div hx-ext="ws" ws-connect="/ws/sessions"> <div hx-ext="ws" ws-connect="../ws/sessions">
<h1>sessions</h1> <h1>sessions</h1>
<div id="status"> <div id="status">

View File

@ -146,7 +146,7 @@ templ Usage(access models.ConvergeAccess) {
</style> </style>
<form id="inputs" novalidate <form id="inputs" novalidate
hx-post="/usage" hx-post="../usage"
method="post" method="post"
hx-trigger="load,input delay:500ms,change,formdataloaded" hx-trigger="load,input delay:500ms,change,formdataloaded"
hx-target="#example-cli" hx-target="#example-cli"