Исправить замечания ревью кода: убрать повтор в AppendOnce за счёт запаса у чистки ключей, вынести MessagePayload и SameContent, принять X-Request-Id от балансировщика, упростить фоновые задачи сервера, сократить комментарии
This commit is contained in:
@@ -15,31 +15,41 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Logging selects the log format, text for dev and JSON otherwise, and the
|
||||
// level.
|
||||
type Logging struct {
|
||||
Env string
|
||||
Level slog.Level
|
||||
}
|
||||
|
||||
func (l Logging) NewLogger() *slog.Logger {
|
||||
opts := &slog.HandlerOptions{Level: l.Level}
|
||||
if l.Env == "dev" {
|
||||
return slog.New(slog.NewTextHandler(os.Stdout, opts))
|
||||
}
|
||||
return slog.New(slog.NewJSONHandler(os.Stdout, opts))
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Logging
|
||||
Host string
|
||||
Port int
|
||||
Env string
|
||||
LogLevel slog.Level
|
||||
ShutdownTimeout time.Duration
|
||||
MaxBodyBytes int64
|
||||
DatabaseURL string
|
||||
SessionStoreURL string
|
||||
SessionTTL time.Duration
|
||||
// DrainDelay is how long a stopping process keeps serving with readiness
|
||||
// already off, so the load balancer can take it out of rotation before
|
||||
// the listener closes. It must exceed the balancer's detection time.
|
||||
// DrainDelay keeps a stopping process serving with readiness off; it must
|
||||
// exceed the balancer's detection time.
|
||||
DrainDelay time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
DBMaxConns int32
|
||||
// DebugAddr enables the internal expvar listener; empty disables it.
|
||||
// DebugAddr enables the internal expvar listener when set.
|
||||
DebugAddr string
|
||||
}
|
||||
|
||||
// Migrate is the configuration of the one-shot migration command, which
|
||||
// needs the database only.
|
||||
type Migrate struct {
|
||||
Env string
|
||||
LogLevel slog.Level
|
||||
Logging
|
||||
DatabaseURL string
|
||||
}
|
||||
|
||||
@@ -51,8 +61,6 @@ func Load(lookup Lookup) (Config, error) {
|
||||
cfg := Config{
|
||||
Host: "0.0.0.0",
|
||||
Port: 8080,
|
||||
Env: "lab",
|
||||
LogLevel: slog.LevelInfo,
|
||||
ShutdownTimeout: 8 * time.Second,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
SessionTTL: time.Hour,
|
||||
@@ -74,7 +82,7 @@ func Load(lookup Lookup) (Config, error) {
|
||||
cfg.Port = port
|
||||
}
|
||||
var err error
|
||||
if cfg.Env, cfg.LogLevel, err = logging(lookup, cfg.Env, cfg.LogLevel); err != nil {
|
||||
if cfg.Logging, err = loadLogging(lookup); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if v, ok := lookup("APP_SHUTDOWN_TIMEOUT"); ok {
|
||||
@@ -135,9 +143,9 @@ func Load(lookup Lookup) (Config, error) {
|
||||
}
|
||||
|
||||
func LoadMigrate(lookup Lookup) (Migrate, error) {
|
||||
cfg := Migrate{Env: "lab", LogLevel: slog.LevelInfo}
|
||||
var cfg Migrate
|
||||
var err error
|
||||
if cfg.Env, cfg.LogLevel, err = logging(lookup, cfg.Env, cfg.LogLevel); err != nil {
|
||||
if cfg.Logging, err = loadLogging(lookup); err != nil {
|
||||
return Migrate{}, err
|
||||
}
|
||||
if cfg.DatabaseURL, err = secret(lookup, "DATABASE_URL_FILE"); err != nil {
|
||||
@@ -146,16 +154,17 @@ func LoadMigrate(lookup Lookup) (Migrate, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func logging(lookup Lookup, env string, level slog.Level) (string, slog.Level, error) {
|
||||
func loadLogging(lookup Lookup) (Logging, error) {
|
||||
l := Logging{Env: "lab", Level: slog.LevelInfo}
|
||||
if v, ok := lookup("APP_ENV"); ok && strings.TrimSpace(v) != "" {
|
||||
env = v
|
||||
l.Env = v
|
||||
}
|
||||
if v, ok := lookup("APP_LOG_LEVEL"); ok {
|
||||
if err := level.UnmarshalText([]byte(v)); err != nil {
|
||||
return "", 0, fmt.Errorf("APP_LOG_LEVEL must be debug, info, warn or error, got %q", v)
|
||||
if err := l.Level.UnmarshalText([]byte(v)); err != nil {
|
||||
return Logging{}, fmt.Errorf("APP_LOG_LEVEL must be debug, info, warn or error, got %q", v)
|
||||
}
|
||||
}
|
||||
return env, level, nil
|
||||
return l, nil
|
||||
}
|
||||
|
||||
// secret reads the file named by the variable; surrounding whitespace,
|
||||
|
||||
@@ -53,7 +53,7 @@ func TestDefaults(t *testing.T) {
|
||||
if cfg.Addr() != "0.0.0.0:8080" {
|
||||
t.Fatalf("Addr = %q", cfg.Addr())
|
||||
}
|
||||
if cfg.Env != "lab" || cfg.LogLevel != slog.LevelInfo {
|
||||
if cfg.Env != "lab" || cfg.Level != slog.LevelInfo {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.ShutdownTimeout != 8*time.Second || cfg.MaxBodyBytes != 65536 {
|
||||
@@ -110,7 +110,7 @@ func TestOverrides(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Load error: %v", err)
|
||||
}
|
||||
if cfg.Addr() != "127.0.0.1:9090" || cfg.Env != "dev" || cfg.LogLevel != slog.LevelDebug {
|
||||
if cfg.Addr() != "127.0.0.1:9090" || cfg.Env != "dev" || cfg.Level != slog.LevelDebug {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.ShutdownTimeout != 3*time.Second || cfg.MaxBodyBytes != 1024 || cfg.SessionTTL != 5*time.Minute {
|
||||
@@ -168,7 +168,7 @@ func TestLoadMigrateNeedsOnlyTheDatabaseURL(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("LoadMigrate error: %v", err)
|
||||
}
|
||||
if cfg.DatabaseURL != databaseURL || cfg.Env != "dev" || cfg.LogLevel != slog.LevelWarn {
|
||||
if cfg.DatabaseURL != databaseURL || cfg.Env != "dev" || cfg.Level != slog.LevelWarn {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if _, err := config.LoadMigrate(env(map[string]string{})); err == nil || !strings.Contains(err.Error(), "DATABASE_URL_FILE") {
|
||||
|
||||
Reference in New Issue
Block a user