chore(tfstated): prepare for listening on a second port

This commit is contained in:
Julien Dessaux 2024-12-31 11:29:58 +01:00
parent 36e3d473f2
commit 65e66b5e38
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 17 additions and 14 deletions

View file

@ -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()

View file

@ -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)

View file

@ -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)
}
}()