diff options
author | Julien Dessaux | 2024-12-25 23:06:13 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-12-25 23:06:13 +0100 |
commit | 6d00e1209736a93ef27547c5b5190ca89bde3660 (patch) | |
tree | df8d6c5fe031b67bee3cacae80ae8202001fcdc8 /cmd | |
parent | chore(deps): update dependencies (diff) | |
download | tfstated-6d00e1209736a93ef27547c5b5190ca89bde3660.tar.gz tfstated-6d00e1209736a93ef27547c5b5190ca89bde3660.tar.bz2 tfstated-6d00e1209736a93ef27547c5b5190ca89bde3660.zip |
feat(tfstated): use environment variables to customize listening hostname and port
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/tfstated/main.go | 35 | ||||
-rw-r--r-- | cmd/tfstated/main_test.go | 9 |
2 files changed, 22 insertions, 22 deletions
diff --git a/cmd/tfstated/main.go b/cmd/tfstated/main.go index a1721e1..79b64f7 100644 --- a/cmd/tfstated/main.go +++ b/cmd/tfstated/main.go @@ -17,14 +17,8 @@ import ( "git.adyxax.org/adyxax/tfstated/pkg/logger" ) -type Config struct { - Host string - Port string -} - func run( ctx context.Context, - config *Config, db *database.DB, //args []string, getenv func(string) string, @@ -45,14 +39,23 @@ func run( db, ) + host := getenv("TFSTATED_HOST") + if host == "" { + host = "127.0.0.1" + } + port := getenv("TFSTATED_PORT") + if port == "" { + port = "8080" + } + httpServer := &http.Server{ - Addr: net.JoinHostPort(config.Host, config.Port), + Addr: net.JoinHostPort(host, port), Handler: logger.Middleware(mux, false), } go func() { log.Printf("listening on %s\n", httpServer.Addr) if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { - fmt.Fprintf(stderr, "error listening and serving: %+v\n", err) + _, _ = fmt.Fprintf(stderr, "error listening and serving: %+v\n", err) } }() var wg sync.WaitGroup @@ -64,7 +67,7 @@ func run( 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) + _, _ = fmt.Fprintf(stderr, "error shutting down http server: %+v\n", err) } }() wg.Wait() @@ -76,7 +79,7 @@ func main() { ctx := context.Background() var opts *slog.HandlerOptions - if os.Getenv("TFSTATE_DEBUG") != "" { + if os.Getenv("TFSTATED_DEBUG") != "" { opts = &slog.HandlerOptions{ AddSource: true, Level: slog.LevelDebug, @@ -85,12 +88,11 @@ func main() { logger := slog.New(slog.NewJSONHandler(os.Stdout, opts)) slog.SetDefault(logger) - config := Config{ - Host: "0.0.0.0", - Port: "8080", - } - - db, err := database.NewDB(ctx, "./tfstate.db?_txlock=immediate", os.Getenv) + db, err := database.NewDB( + ctx, + "./tfstate.db?_txlock=immediate", + os.Getenv, + ) if err != nil { fmt.Fprintf(os.Stderr, "database init error: %+v\n", err) os.Exit(1) @@ -99,7 +101,6 @@ func main() { if err := run( ctx, - &config, db, //os.Args, os.Getenv, diff --git a/cmd/tfstated/main_test.go b/cmd/tfstated/main_test.go index dc19a3f..aa67ae8 100644 --- a/cmd/tfstated/main_test.go +++ b/cmd/tfstated/main_test.go @@ -24,14 +24,14 @@ var adminPassword string var adminPasswordMutex sync.Mutex func TestMain(m *testing.M) { - config := Config{ - Host: "127.0.0.1", - Port: "8081", - } getenv := func(key string) string { switch key { case "DATA_ENCRYPTION_KEY": return "hP3ZSCnY3LMgfTQjwTaGrhKwdA0yXMXIfv67OJnntqM=" + case "TFSTATED_HOST": + return "127.0.0.1" + case "TFSTATED_PORT": + return "8081" case "VERSIONS_HISTORY_LIMIT": return "3" default: @@ -55,7 +55,6 @@ func TestMain(m *testing.M) { } go run( ctx, - &config, db, getenv, os.Stderr, |