summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2025-01-02 19:43:53 +0100
committerJulien Dessaux2025-01-02 19:43:53 +0100
commit24bca7067b02844dc275ed6e22f96f20ab02f82e (patch)
tree5eab0d3e6d2a0beae78ab4c7be02dd2a60af4af4
parentchore(tfstated): code cleanup (diff)
downloadtfstated-24bca7067b02844dc275ed6e22f96f20ab02f82e.tar.gz
tfstated-24bca7067b02844dc275ed6e22f96f20ab02f82e.tar.bz2
tfstated-24bca7067b02844dc275ed6e22f96f20ab02f82e.zip
feat(tfstated): bootstrap webui listening on a second port
-rw-r--r--cmd/tfstated/main.go9
-rw-r--r--pkg/webui/healthz.go12
-rw-r--r--pkg/webui/routes.go14
-rw-r--r--pkg/webui/run.go45
4 files changed, 78 insertions, 2 deletions
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
+}