From 0d3910829d3b153e0e5ffee66957afb6382473aa Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Fri, 12 Apr 2024 10:00:13 -0600 Subject: [PATCH] if $POSTGRES_CONN set then use it --- config.go | 18 +++++++++++++++--- config_test.go | 7 +++++-- main.go | 2 +- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index b667f61..adb518b 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "fmt" "os" @@ -8,6 +9,7 @@ import ( "slices" "strconv" "strings" + "time" ) type Config struct { @@ -16,16 +18,17 @@ type Config struct { InitializeSlack bool SlackToken string SlackChannels string + PostgresConn string storage Storage queue Queue driver Driver } -func newConfig() (Config, error) { - return newConfigFromEnv(os.Getenv) +func newConfig(ctx context.Context) (Config, error) { + return newConfigFromEnv(ctx, os.Getenv) } -func newConfigFromEnv(getEnv func(string) string) (Config, error) { +func newConfigFromEnv(ctx context.Context, getEnv func(string) string) (Config, error) { def := Config{ Port: 8080, } @@ -78,6 +81,15 @@ func newConfigFromEnv(getEnv func(string) string) (Config, error) { } result.driver = NewRAM() + if result.PostgresConn != "" { + ctx, can := context.WithTimeout(ctx, time.Second*10) + defer can() + pg, err := NewPostgres(ctx, result.PostgresConn) + if err != nil { + return Config{}, err + } + result.driver = pg + } result.storage = NewStorage(result.driver) result.queue = NewQueue(result.driver) diff --git a/config_test.go b/config_test.go index aab228a..f4a5a99 100644 --- a/config_test.go +++ b/config_test.go @@ -1,9 +1,12 @@ package main -import "testing" +import ( + "context" + "testing" +) func TestNewConfig(t *testing.T) { - if got, err := newConfigFromEnv(func(k string) string { + if got, err := newConfigFromEnv(context.Background(), func(k string) string { t.Logf("getenv(%s)", k) switch k { case "PORT": diff --git a/main.go b/main.go index cc3ec33..48bb7aa 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ func main() { ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT) defer can() - cfg, err := newConfig() + cfg, err := newConfig(ctx) if err != nil { panic(err) }