diff options
author | Julien Dessaux | 2024-12-31 11:29:58 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-12-31 11:29:58 +0100 |
commit | 65e66b5e389f63eafe5d5902946b6fec974abe85 (patch) | |
tree | ca3062dc4513dd77f719042227d52831733b7d4c | |
parent | chore(tfstated): refactor backend code to a dedicated package (diff) | |
download | tfstated-65e66b5e389f63eafe5d5902946b6fec974abe85.tar.gz tfstated-65e66b5e389f63eafe5d5902946b6fec974abe85.tar.bz2 tfstated-65e66b5e389f63eafe5d5902946b6fec974abe85.zip |
chore(tfstated): prepare for listening on a second port
-rw-r--r-- | cmd/tfstated/main.go | 18 | ||||
-rw-r--r-- | cmd/tfstated/main_test.go | 6 | ||||
-rw-r--r-- | pkg/backend/run.go | 7 |
3 files changed, 17 insertions, 14 deletions
diff --git a/cmd/tfstated/main.go b/cmd/tfstated/main.go index e6e9a38..02ddad3 100644 --- a/cmd/tfstated/main.go +++ b/cmd/tfstated/main.go @@ -30,17 +30,21 @@ func run( return err } - httpServer := backend.Run(ctx, db, getenv, stderr) + backend := backend.Run(ctx, db, getenv, stderr) + + <-ctx.Done() + shutdownCtx := context.Background() + shutdownCtx, shutdownCancel := context.WithTimeout(shutdownCtx, 10*time.Second) + defer shutdownCancel() + var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() - <-ctx.Done() - shutdownCtx := context.Background() - shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second) - defer cancel() - if err := httpServer.Shutdown(shutdownCtx); err != nil { - _, _ = fmt.Fprintf(stderr, "error shutting down http server: %+v\n", err) + if err := backend.Shutdown(shutdownCtx); err != nil { + slog.Error("error shutting down backend http server", "error", err) + } + }() } }() wg.Wait() diff --git a/cmd/tfstated/main_test.go b/cmd/tfstated/main_test.go index aa67ae8..ae9f2d7 100644 --- a/cmd/tfstated/main_test.go +++ b/cmd/tfstated/main_test.go @@ -15,7 +15,7 @@ import ( ) var baseURI = url.URL{ - Host: "127.0.0.1:8081", + Host: "127.0.0.1:8082", Path: "/", Scheme: "http", } @@ -31,7 +31,7 @@ func TestMain(m *testing.M) { case "TFSTATED_HOST": return "127.0.0.1" case "TFSTATED_PORT": - return "8081" + return "8082" case "VERSIONS_HISTORY_LIMIT": return "3" default: @@ -59,7 +59,7 @@ func TestMain(m *testing.M) { getenv, os.Stderr, ) - err = waitForReady(ctx, 5*time.Second, "http://127.0.0.1:8081/healthz") + err = waitForReady(ctx, 5*time.Second, "http://127.0.0.1:8082/healthz") if err != nil { fmt.Fprintf(os.Stderr, "%+v\n", err) os.Exit(1) diff --git a/pkg/backend/run.go b/pkg/backend/run.go index dd7f3bf..a6d2f03 100644 --- a/pkg/backend/run.go +++ b/pkg/backend/run.go @@ -2,9 +2,8 @@ package backend import ( "context" - "fmt" "io" - "log" + "log/slog" "net" "net/http" @@ -41,9 +40,9 @@ func Run( Handler: logger.Middleware(mux, false), } go func() { - log.Printf("listening on %s\n", httpServer.Addr) + slog.Info("backend http server listening", "address", httpServer.Addr) if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { - _, _ = fmt.Fprintf(stderr, "error listening and serving: %+v\n", err) + slog.Error("error listening and serving backend http server", "address", httpServer.Addr, "error", err) } }() |