This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
102 lines (87 loc) · 3.17 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package config
import (
"fmt"
"os"
"github.com/go-redis/redis/v8"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/kelseyhightower/envconfig"
"github.com/x1unix/sbda-ledger/internal/web"
"gopkg.in/yaml.v2"
)
type ServerConfig struct {
ListenAddress string `envconfig:"LGR_LISTEN_ADDR" default:":8080" yaml:"listen"`
ReadTimeout Duration `envconfig:"LGR_READ_TIMEOUT" yaml:"read_timeout"`
WriteTimeout Duration `envconfig:"LGR_WRITE_TIMEOUT" yaml:"write_timeout"`
TokenBucketTTL Duration `envconfig:"LGR_TOKEN_BUCKET_TTL" yaml:"token_bucket_ttl"`
UserRequestsPerSecondLimit float64 `envconfig:"LGR_USER_RPS_LIMIT" yaml:"user_rps_limit"`
}
func (cfg ServerConfig) ListenParams() web.ListenParams {
return web.ListenParams{
Address: cfg.ListenAddress,
ReadTimeout: cfg.ReadTimeout.Duration,
WriteTimeout: cfg.WriteTimeout.Duration,
LimitExpirationTTL: cfg.TokenBucketTTL.Duration,
ClientRPSQuota: cfg.UserRequestsPerSecondLimit,
}
}
type Database struct {
Address string `envconfig:"LGR_DB_ADDRESS" default:"postgres://localhost:5432/ledger" yaml:"address"`
MigrationsDirectory string `envconfig:"LGR_MIGRATIONS_DIR" default:"db/migrations" yaml:"migrations_dir"`
VersionTable string `envconfig:"LGR_VERSION_TABLE" default:"schema_migrations" yaml:"version_table"`
SchemaVersion uint `envconfig:"LGR_SCHEMA_VERSION" yaml:"schema_version"`
SkipMigration bool `envconfig:"LGR_NO_MIGRATION" yaml:"skip_migration"`
}
func (dbs Database) PoolConfig() (*pgxpool.Config, error) {
cfg, err := pgxpool.ParseConfig(dbs.Address)
if err != nil {
return nil, fmt.Errorf("invalid database DSN string: %w", err)
}
return cfg, nil
}
type Redis struct {
DB int `envconfig:"LGR_REDIS_DB" yaml:"db"`
Address string `envconfig:"LGR_REDIS_ADDRESS" yaml:"address" default:"localhost:6379"`
Username string `envconfig:"LGR_REDIS_USER" yaml:"username"`
Password string `envconfig:"LGR_REDIS_PASSWORD" yaml:"password"`
}
// RedisOptions returns redis connection options
func (r Redis) RedisOptions() *redis.Options {
return &redis.Options{
Network: "tcp",
Addr: r.Address,
DB: r.DB,
Username: r.Username,
Password: r.Password,
}
}
type Config struct {
Production bool `envconfig:"LGR_PRODUCTION" default:"false" yaml:"production"`
Server ServerConfig `yaml:"server"`
DB Database `yaml:"db"`
Redis Redis `yaml:"redis"`
}
func FromFile(cfgPath string) (*Config, error) {
cfg, err := FromEnv()
if err != nil {
return nil, err
}
f, err := os.Open(cfgPath)
if err != nil {
return nil, fmt.Errorf("failed to open config file: %w", err)
}
defer f.Close()
if err = yaml.NewDecoder(f).Decode(cfg); err != nil {
return nil, fmt.Errorf("failed to parse config file %q: %w", cfgPath, err)
}
return cfg, nil
}
func FromEnv() (*Config, error) {
cfg := &Config{}
// envconfig doesn't work correctly with nested structs and
// sets invalid env name for nested fields.
err := envconfig.Process("", cfg)
if err != nil {
return nil, fmt.Errorf("failed to read config from environment variables: %w", err)
}
return cfg, err
}