if $POSTGRES_CONN set then use it

main
Bel LaPointe 2024-04-12 10:00:13 -06:00
parent 8f288cf12e
commit 0d3910829d
3 changed files with 21 additions and 6 deletions

View File

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

View File

@ -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":

View File

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