From 24bca7067b02844dc275ed6e22f96f20ab02f82e Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 2 Jan 2025 19:43:53 +0100 Subject: feat(tfstated): bootstrap webui listening on a second port --- cmd/tfstated/main.go | 9 +++++++-- pkg/webui/healthz.go | 12 ++++++++++++ pkg/webui/routes.go | 14 ++++++++++++++ pkg/webui/run.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 pkg/webui/healthz.go create mode 100644 pkg/webui/routes.go create mode 100644 pkg/webui/run.go diff --git a/cmd/tfstated/main.go b/cmd/tfstated/main.go index 8b462f7..a1c74ca 100644 --- a/cmd/tfstated/main.go +++ b/cmd/tfstated/main.go @@ -26,7 +26,8 @@ func run( return err } - backend := backend.Run(ctx, db, getenv, stderr) + backend := backend.Run(ctx, db, getenv) + webui := webui.Run(ctx, db, getenv) <-ctx.Done() shutdownCtx := context.Background() @@ -34,13 +35,17 @@ func run( defer shutdownCancel() var wg sync.WaitGroup - wg.Add(1) + wg.Add(2) go func() { defer wg.Done() if err := backend.Shutdown(shutdownCtx); err != nil { slog.Error("error shutting down backend http server", "error", err) } }() + go func() { + defer wg.Done() + if err := webui.Shutdown(shutdownCtx); err != nil { + slog.Error("error shutting down webui http server", "error", err) } }() wg.Wait() diff --git a/pkg/webui/healthz.go b/pkg/webui/healthz.go new file mode 100644 index 0000000..dee51d0 --- /dev/null +++ b/pkg/webui/healthz.go @@ -0,0 +1,12 @@ +package webui + +import "net/http" + +func handleHealthz() http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "no-store, no-cache") + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("{}")) + }) +} diff --git a/pkg/webui/routes.go b/pkg/webui/routes.go new file mode 100644 index 0000000..ef16040 --- /dev/null +++ b/pkg/webui/routes.go @@ -0,0 +1,14 @@ +package webui + +import ( + "net/http" + + "git.adyxax.org/adyxax/tfstated/pkg/database" +) + +func addRoutes( + mux *http.ServeMux, + db *database.DB, +) { + mux.Handle("GET /healthz", handleHealthz()) +} diff --git a/pkg/webui/run.go b/pkg/webui/run.go new file mode 100644 index 0000000..3aaec55 --- /dev/null +++ b/pkg/webui/run.go @@ -0,0 +1,45 @@ +package webui + +import ( + "context" + "log/slog" + "net" + "net/http" + + "git.adyxax.org/adyxax/tfstated/pkg/database" + "git.adyxax.org/adyxax/tfstated/pkg/logger" +) + +func Run( + ctx context.Context, + db *database.DB, + getenv func(string) string, +) *http.Server { + mux := http.NewServeMux() + addRoutes( + mux, + db, + ) + + host := getenv("TFSTATED_WEBUI_HOST") + if host == "" { + host = "127.0.0.1" + } + port := getenv("TFSTATED_WEBUI_PORT") + if port == "" { + port = "8081" + } + + httpServer := &http.Server{ + Addr: net.JoinHostPort(host, port), + Handler: logger.Middleware(mux, false), + } + go func() { + slog.Info("webui http server listening", "address", httpServer.Addr) + if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { + slog.Error("error listening and serving webui http server", "address", httpServer.Addr, "error", err) + } + }() + + return httpServer +} -- cgit v1.2.3