if $POSTGRES_CONN set then use it
parent
8f288cf12e
commit
0d3910829d
18
config.go
18
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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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":
|
||||
|
|
|
|||
Loading…
Reference in New Issue