From baf5aac08e27d33dc5087bbeec1437678bb2a384 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 28 Sep 2024 14:06:06 +0200 Subject: chore(tfstated): rename tfstate to tfstated --- cmd/tfstate/healthz.go | 12 ------- cmd/tfstate/main.go | 93 ------------------------------------------------- cmd/tfstate/routes.go | 7 ---- cmd/tfstated/healthz.go | 12 +++++++ cmd/tfstated/main.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/tfstated/routes.go | 7 ++++ 6 files changed, 112 insertions(+), 112 deletions(-) delete mode 100644 cmd/tfstate/healthz.go delete mode 100644 cmd/tfstate/main.go delete mode 100644 cmd/tfstate/routes.go create mode 100644 cmd/tfstated/healthz.go create mode 100644 cmd/tfstated/main.go create mode 100644 cmd/tfstated/routes.go (limited to 'cmd') diff --git a/cmd/tfstate/healthz.go b/cmd/tfstate/healthz.go deleted file mode 100644 index 20c72c9..0000000 --- a/cmd/tfstate/healthz.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -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/cmd/tfstate/main.go b/cmd/tfstate/main.go deleted file mode 100644 index e0b33a5..0000000 --- a/cmd/tfstate/main.go +++ /dev/null @@ -1,93 +0,0 @@ -package main - -import ( - "context" - "fmt" - "io" - "log" - "log/slog" - "net" - "net/http" - "os" - "os/signal" - "sync" - "time" -) - -type Config struct { - Host string - Port string -} - -func run( - ctx context.Context, - config *Config, - args []string, - getenv func(string) string, - stdin io.Reader, - stdout, stderr io.Writer, -) error { - ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) - defer cancel() - - mux := http.NewServeMux() - addRoutes( - mux, - ) - httpServer := &http.Server{ - Addr: net.JoinHostPort(config.Host, config.Port), - Handler: mux, - } - go func() { - log.Printf("listening on %s\n", httpServer.Addr) - if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { - fmt.Fprintf(os.Stderr, "error listening and serving: %+v\n", err) - } - }() - 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(os.Stderr, "error shutting down http server: %+v\n", err) - } - }() - wg.Wait() - - return nil -} - -func main() { - ctx := context.Background() - - var opts *slog.HandlerOptions - if os.Getenv("TFSTATE_DEBUG") != "" { - opts = &slog.HandlerOptions{ - AddSource: true, - Level: slog.LevelDebug, - } - } - logger := slog.New(slog.NewJSONHandler(os.Stdout, opts)) - slog.SetDefault(logger) - - config := Config{ - Host: "0.0.0.0", - Port: "8080", - } - - if err := run( - ctx, - &config, - os.Args, - os.Getenv, - os.Stdin, - os.Stdout, os.Stderr, - ); err != nil { - fmt.Fprintf(os.Stderr, "%+v\n", err) - os.Exit(1) - } -} diff --git a/cmd/tfstate/routes.go b/cmd/tfstate/routes.go deleted file mode 100644 index f583fc7..0000000 --- a/cmd/tfstate/routes.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "net/http" - -func addRoutes(mux *http.ServeMux) { - mux.Handle("GET /healthz", handleHealthz()) -} diff --git a/cmd/tfstated/healthz.go b/cmd/tfstated/healthz.go new file mode 100644 index 0000000..20c72c9 --- /dev/null +++ b/cmd/tfstated/healthz.go @@ -0,0 +1,12 @@ +package main + +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/cmd/tfstated/main.go b/cmd/tfstated/main.go new file mode 100644 index 0000000..e0b33a5 --- /dev/null +++ b/cmd/tfstated/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "context" + "fmt" + "io" + "log" + "log/slog" + "net" + "net/http" + "os" + "os/signal" + "sync" + "time" +) + +type Config struct { + Host string + Port string +} + +func run( + ctx context.Context, + config *Config, + args []string, + getenv func(string) string, + stdin io.Reader, + stdout, stderr io.Writer, +) error { + ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) + defer cancel() + + mux := http.NewServeMux() + addRoutes( + mux, + ) + httpServer := &http.Server{ + Addr: net.JoinHostPort(config.Host, config.Port), + Handler: mux, + } + go func() { + log.Printf("listening on %s\n", httpServer.Addr) + if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { + fmt.Fprintf(os.Stderr, "error listening and serving: %+v\n", err) + } + }() + 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(os.Stderr, "error shutting down http server: %+v\n", err) + } + }() + wg.Wait() + + return nil +} + +func main() { + ctx := context.Background() + + var opts *slog.HandlerOptions + if os.Getenv("TFSTATE_DEBUG") != "" { + opts = &slog.HandlerOptions{ + AddSource: true, + Level: slog.LevelDebug, + } + } + logger := slog.New(slog.NewJSONHandler(os.Stdout, opts)) + slog.SetDefault(logger) + + config := Config{ + Host: "0.0.0.0", + Port: "8080", + } + + if err := run( + ctx, + &config, + os.Args, + os.Getenv, + os.Stdin, + os.Stdout, os.Stderr, + ); err != nil { + fmt.Fprintf(os.Stderr, "%+v\n", err) + os.Exit(1) + } +} diff --git a/cmd/tfstated/routes.go b/cmd/tfstated/routes.go new file mode 100644 index 0000000..f583fc7 --- /dev/null +++ b/cmd/tfstated/routes.go @@ -0,0 +1,7 @@ +package main + +import "net/http" + +func addRoutes(mux *http.ServeMux) { + mux.Handle("GET /healthz", handleHealthz()) +} -- cgit v1.2.3