Extend configuration

This commit is contained in:
2023-03-28 21:07:42 +03:00
parent 6839a264db
commit 8d2af4ad65
7 changed files with 63 additions and 24 deletions

View File

@@ -18,11 +18,12 @@ import (
"github.com/derfenix/webarchive/adapters/processors"
badgerRepo "github.com/derfenix/webarchive/adapters/repository/badger"
"github.com/derfenix/webarchive/api/openapi"
"github.com/derfenix/webarchive/config"
"github.com/derfenix/webarchive/entity"
"github.com/derfenix/webarchive/ports/rest"
)
func NewApplication(cfg Config) (Application, error) {
func NewApplication(cfg config.Config) (Application, error) {
log, err := newLogger(cfg.Logging)
if err != nil {
return Application{}, fmt.Errorf("new logger: %w", err)
@@ -38,7 +39,7 @@ func NewApplication(cfg Config) (Application, error) {
return Application{}, fmt.Errorf("new page repo: %w", err)
}
processor, err := processors.NewProcessors()
processor, err := processors.NewProcessors(cfg)
if err != nil {
return Application{}, fmt.Errorf("new processors: %w", err)
}
@@ -73,7 +74,7 @@ func NewApplication(cfg Config) (Application, error) {
}
httpServer := http.Server{
Addr: "0.0.0.0:5001",
Addr: cfg.API.Address,
Handler: server,
ReadTimeout: time.Second * 15,
ReadHeaderTimeout: time.Second * 5,
@@ -94,7 +95,7 @@ func NewApplication(cfg Config) (Application, error) {
}
type Application struct {
cfg Config
cfg config.Config
log *zap.Logger
db *badger.DB
processor entity.Processor
@@ -165,7 +166,7 @@ func (a *Application) Stop() error {
return errs
}
func newLogger(cfg Logging) (*zap.Logger, error) {
func newLogger(cfg config.Logging) (*zap.Logger, error) {
logCfg := zap.NewProductionConfig()
logCfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
logCfg.EncoderConfig.EncodeDuration = zapcore.NanosDurationEncoder

View File

@@ -1,38 +0,0 @@
package application
import (
"context"
"fmt"
"github.com/sethvargo/go-envconfig"
)
const envPrefix = "WEBARCHIVE_"
func NewConfig(ctx context.Context) (Config, error) {
cfg := Config{}
lookuper := envconfig.MultiLookuper(
envconfig.PrefixLookuper(envPrefix, envconfig.OsLookuper()),
envconfig.OsLookuper(),
)
if err := envconfig.ProcessWith(ctx, &cfg, lookuper); err != nil {
return Config{}, fmt.Errorf("process env: %w", err)
}
return cfg, nil
}
type Config struct {
DB DB `env:",prefix=DB_"`
Logging Logging `env:",prefix=LOGGING_"`
}
type DB struct {
Path string `env:"PATH,default=./db"`
}
type Logging struct {
Debug bool `env:"DEBUG"`
}

View File

@@ -1,42 +0,0 @@
package application
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewConfig(t *testing.T) {
t.Parallel()
ctx := context.Background()
t.Run("no envs", func(t *testing.T) {
config, err := NewConfig(ctx)
require.NoError(t, err)
assert.Equal(t, "./db", config.DB.Path)
})
t.Run("env without prefix", func(t *testing.T) {
require.NoError(t, os.Setenv("DB_PATH", "./old_db"))
config, err := NewConfig(ctx)
require.NoError(t, err)
assert.Equal(t, "./old_db", config.DB.Path)
})
t.Run("prefix env override", func(t *testing.T) {
require.NoError(t, os.Setenv("WEBARCHIVE_DB_PATH", "./new_db"))
config, err := NewConfig(ctx)
require.NoError(t, err)
assert.Equal(t, "./new_db", config.DB.Path)
})
}