diff options
author | Julien Dessaux | 2025-01-02 19:43:53 +0100 |
---|---|---|
committer | Julien Dessaux | 2025-01-02 19:43:53 +0100 |
commit | 24bca7067b02844dc275ed6e22f96f20ab02f82e (patch) | |
tree | 5eab0d3e6d2a0beae78ab4c7be02dd2a60af4af4 /pkg/webui/run.go | |
parent | chore(tfstated): code cleanup (diff) | |
download | tfstated-24bca7067b02844dc275ed6e22f96f20ab02f82e.tar.gz tfstated-24bca7067b02844dc275ed6e22f96f20ab02f82e.tar.bz2 tfstated-24bca7067b02844dc275ed6e22f96f20ab02f82e.zip |
feat(tfstated): bootstrap webui listening on a second port
Diffstat (limited to '')
-rw-r--r-- | pkg/webui/run.go | 45 |
1 files changed, 45 insertions, 0 deletions
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 +} |