From b516c979b3a211e68d9a90ab6ea09cabc1557f4b Mon Sep 17 00:00:00 2001 From: Tine Date: Tue, 13 Feb 2024 12:00:48 +0100 Subject: [PATCH] feat(temporal): expose ui under authentication --- cmd/server/main.go | 3 +++ cmd/temporal/main.go | 3 ++- internal/handlers/settings.go | 1 + internal/handlers/temporal.go | 49 +++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 internal/handlers/temporal.go diff --git a/cmd/server/main.go b/cmd/server/main.go index 490de2e..12ac5fd 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -54,6 +54,9 @@ func main() { r.HandleFunc("/oauth2/callback", h.OAuth2CallbackGET).Methods("GET") r.HandleFunc("/oauth2/logout", h.Authenticated(h.OAuth2LogoutGET)).Methods("GET") + // Temporal UI + r.PathPrefix("/temporal").HandlerFunc(h.Authenticated(h.Temporal)) + // 404 r.PathPrefix("/").HandlerFunc(h.Error404).Methods("GET") diff --git a/cmd/temporal/main.go b/cmd/temporal/main.go index 21f5947..7861d14 100644 --- a/cmd/temporal/main.go +++ b/cmd/temporal/main.go @@ -106,7 +106,7 @@ func frontendServer() { Port: 8223, TemporalGRPCAddress: "localhost:7233", EnableUI: true, - UIAssetPath: "", + PublicPath: "/temporal", Codec: uiconfig.Codec{ Endpoint: "", }, @@ -121,6 +121,7 @@ func frontendServer() { if err := server.Start(); err != nil { panic(err) } + server.Stop() } func main() { diff --git a/internal/handlers/settings.go b/internal/handlers/settings.go index 52a8626..b18a84c 100644 --- a/internal/handlers/settings.go +++ b/internal/handlers/settings.go @@ -19,6 +19,7 @@ var SettingsPages = []*components.Page{ {Path: "/settings", Title: "Overview"}, {Path: "/settings/healthchecks", Title: "Healthchecks"}, {Path: "/settings/workers", Title: "Workers"}, + {Path: "/temporal", Title: "Temporal"}, {Path: "/oauth2/logout", Title: "Logout"}, } diff --git a/internal/handlers/temporal.go b/internal/handlers/temporal.go new file mode 100644 index 0000000..081b800 --- /dev/null +++ b/internal/handlers/temporal.go @@ -0,0 +1,49 @@ +package handlers + +import ( + "io" + "net/http" +) + +var customTransport = http.DefaultTransport + +func (h *BaseHandler) Temporal(w http.ResponseWriter, r *http.Request, user *AuthenticatedUser) { + // Create a new HTTP request with the same method, URL, and body as the original request + targetURL := r.URL + targetURL.Host = "localhost:8223" + targetURL.Scheme = "http" + + proxyReq, err := http.NewRequest(r.Method, targetURL.String(), r.Body) + if err != nil { + http.Error(w, "Error creating proxy request", http.StatusInternalServerError) + return + } + + // Copy the headers from the original request to the proxy request + for name, values := range r.Header { + for _, value := range values { + proxyReq.Header.Add(name, value) + } + } + + // Send the proxy request using the custom transport + resp, err := customTransport.RoundTrip(proxyReq) + if err != nil { + http.Error(w, "Error sending proxy request", http.StatusInternalServerError) + return + } + defer resp.Body.Close() + + // Copy the headers from the proxy response to the original response + for name, values := range resp.Header { + for _, value := range values { + w.Header().Add(name, value) + } + } + + // Set the status code of the original response to the status code of the proxy response + w.WriteHeader(resp.StatusCode) + + // Copy the body of the proxy response to the original response + io.Copy(w, resp.Body) +}