feat(tfstated): bootstrap webui listening on a second port
This commit is contained in:
parent
99ff8441da
commit
24bca7067b
4 changed files with 78 additions and 2 deletions
|
@ -26,7 +26,8 @@ func run(
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
backend := backend.Run(ctx, db, getenv, stderr)
|
backend := backend.Run(ctx, db, getenv)
|
||||||
|
webui := webui.Run(ctx, db, getenv)
|
||||||
|
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
shutdownCtx := context.Background()
|
shutdownCtx := context.Background()
|
||||||
|
@ -34,13 +35,17 @@ func run(
|
||||||
defer shutdownCancel()
|
defer shutdownCancel()
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(1)
|
wg.Add(2)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if err := backend.Shutdown(shutdownCtx); err != nil {
|
if err := backend.Shutdown(shutdownCtx); err != nil {
|
||||||
slog.Error("error shutting down backend http server", "error", err)
|
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()
|
wg.Wait()
|
||||||
|
|
12
pkg/webui/healthz.go
Normal file
12
pkg/webui/healthz.go
Normal file
|
@ -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("{}"))
|
||||||
|
})
|
||||||
|
}
|
14
pkg/webui/routes.go
Normal file
14
pkg/webui/routes.go
Normal file
|
@ -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())
|
||||||
|
}
|
45
pkg/webui/run.go
Normal file
45
pkg/webui/run.go
Normal file
|
@ -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
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue