|
1 | 1 | package config
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 | 4 | "os"
|
6 | 5 | "strings"
|
7 |
| - "time" |
| 6 | + "sync" |
8 | 7 |
|
9 |
| - "github.com/BurntSushi/toml" |
| 8 | + "github.com/kelseyhightower/envconfig" |
10 | 9 | "gorm.io/gorm"
|
11 | 10 | )
|
12 | 11 |
|
13 | 12 | type Config struct {
|
14 |
| - DB *gorm.DB |
| 13 | + DB *gorm.DB `ignored:"true"` |
15 | 14 |
|
16 |
| - ProxyPath string `toml:"proxy_path"` // used in webhook-sender |
17 |
| - ProxyList []string `toml:"-"` // reads proxies from ProxyPath and fills it with |
| 15 | + // TODO: remove |
| 16 | + // ProxyPath string `toml:"proxy_path"` // used in webhook-sender |
| 17 | + // ProxyList []string `toml:"-"` // reads proxies from ProxyPath and fills it with |
18 | 18 |
|
19 |
| - Prod_env bool |
| 19 | + Prod_env bool `envconfig:"PROD_ENV" required:"true"` |
20 | 20 |
|
21 |
| - Telegram struct { |
22 |
| - Token string `toml:"token"` |
| 21 | + // Telegram struct { |
| 22 | + // Token string `toml:"token"` |
| 23 | + // } |
| 24 | + |
| 25 | + Testing struct { |
| 26 | + Enabled bool `envconfig:"TESTING" required:"true"` |
| 27 | + TxConfirmDelay int `envconfig:"TESTING_TX_CONFIRM_DELAY" required:"true"` |
| 28 | + TxFinProcessingDelay int `envconfig:"TX_FIN_PROCESSING_DELAY" required:"true"` |
| 29 | + TxFinDelay int `envconfig:"TX_FIN_DELAY" required:"true"` |
| 30 | + } |
| 31 | + |
| 32 | + Postgres struct { |
| 33 | + Host string `envconfig:"DB_HOST" required:"true"` |
| 34 | + User string `envconfig:"DB_USER" required:"true"` |
| 35 | + Password string `envconfig:"DB_PASSWORD" required:"true"` |
| 36 | + Db_name string `envconfig:"DB_NAME" required:"true"` |
| 37 | + Port uint16 `envconfig:"DB_PORT" required:"true"` |
| 38 | + Ssl_mode string `envconfig:"DB_SSL_MODE" required:"true"` |
| 39 | + } |
| 40 | + Nats struct { |
| 41 | + Servers string `envconfig:"NATS_SERVERS" required:"true"` |
| 42 | + // TomlServers []string `toml:"servers"` |
| 43 | + } |
| 44 | + |
| 45 | + PrivateKey string `envconfig:"API_ADMIN_KEY" required:"true"` |
| 46 | + Api struct { |
| 47 | + Ipv4 string `envconfig:"API_IPV4" required:"true"` |
| 48 | + Proto string `envconfig:"API_PROTO" required:"true"` |
23 | 49 | }
|
| 50 | +} |
| 51 | + |
| 52 | +type ConfigS struct { |
| 53 | + DB *gorm.DB `ignored:"true"` |
| 54 | + |
| 55 | + // TODO: remove |
| 56 | + // ProxyPath string `toml:"proxy_path"` // used in webhook-sender |
| 57 | + // ProxyList []string `toml:"-"` // reads proxies from ProxyPath and fills it with |
| 58 | + |
| 59 | + Prod_env bool `envconfig:"PROD_ENV" required:"true"` |
| 60 | + |
| 61 | + // Telegram struct { |
| 62 | + // Token string `toml:"token"` |
| 63 | + // } |
24 | 64 |
|
25 | 65 | Testing struct {
|
26 |
| - Enabled bool |
27 |
| - TxConfirmDelay time.Duration `toml:"tx_confirm_delay"` |
28 |
| - TxFinProcessingDelay time.Duration `toml:"tx_fin_processing_delay"` |
29 |
| - TxFinDelay time.Duration `toml:"tx_fin_delay"` |
30 |
| - } `toml:"testing"` |
31 |
| - |
32 |
| - PrivateKey string `toml:"private_key"` |
33 |
| - Postgres struct { |
34 |
| - Host string |
35 |
| - User string |
36 |
| - Password string |
37 |
| - Db_name string |
38 |
| - Port uint16 |
39 |
| - Ssl_mode string |
| 66 | + Enabled bool `envconfig:"TESTING" required:"true"` |
| 67 | + TxConfirmDelay int `envconfig:"TESTING_TX_CONFIRM_DELAY" required:"true"` |
| 68 | + TxFinProcessingDelay int `envconfig:"TX_FIN_PROCESSING_DELAY" required:"true"` |
| 69 | + TxFinDelay int `envconfig:"TX_FIN_DELAY" required:"true"` |
| 70 | + } |
| 71 | + |
| 72 | + // PrivateKey string `toml:"private_key"` |
| 73 | + Postgres struct { |
| 74 | + Host string `envconfig:"DB_HOST" required:"true"` |
| 75 | + User string `envconfig:"DB_USER" required:"true"` |
| 76 | + Password string `envconfig:"DB_PASSWORD" required:"true"` |
| 77 | + Db_name string `envconfig:"DB_NAME" required:"true"` |
| 78 | + Port uint16 `envconfig:"DB_PORT" required:"true"` |
| 79 | + Ssl_mode string `envconfig:"DB_SSL_MODE" required:"true"` |
40 | 80 | }
|
41 | 81 | Nats struct {
|
42 |
| - Servers string |
43 |
| - TomlServers []string `toml:"servers"` |
| 82 | + Servers string `envconfig:"NATS_SERVERS" required:"true"` |
| 83 | + // TomlServers []string `toml:"servers"` |
44 | 84 | }
|
45 | 85 | Api struct {
|
46 |
| - Ipv4 string |
47 |
| - Proto string |
48 |
| - } `toml:"rack_web"` |
| 86 | + Ipv4 string `envconfig:"API_IPV4" required:"true"` |
| 87 | + Proto string `envconfig:"API_PROTO" required:"true"` |
| 88 | + } |
49 | 89 | }
|
50 | 90 |
|
51 |
| -func ReadConfig() *Config { |
52 |
| - byte_config, err := os.ReadFile(os.Getenv("CONFIG")) |
53 |
| - if err != nil { |
54 |
| - panic(err) |
55 |
| - } |
| 91 | +var once sync.Once |
56 | 92 |
|
| 93 | +func ReadConfig() *Config { |
57 | 94 | var config Config
|
58 |
| - _, err = toml.Decode(string(byte_config), &config) |
59 |
| - if err != nil { |
60 |
| - panic(err) |
61 |
| - } |
62 | 95 |
|
63 |
| - fmt.Println("PATH", config.ProxyPath) |
| 96 | + once.Do(func() { |
| 97 | + if err := envconfig.Process("", &config); err != nil { |
| 98 | + panic(err) |
| 99 | + } |
| 100 | + }) |
64 | 101 |
|
65 |
| - user, err := os.ReadFile(os.Getenv("SECRETS") + "/nats-user.txt") |
66 |
| - if err != nil { |
67 |
| - panic(err) |
68 |
| - } |
| 102 | + // user, err := os.ReadFile(os.Getenv("SECRETS") + "/nats-user.txt") |
| 103 | + // if err != nil { |
| 104 | + // panic(err) |
| 105 | + // } |
69 | 106 |
|
70 |
| - pass, err := os.ReadFile(os.Getenv("SECRETS") + "/nats-password.txt") |
71 |
| - if err != nil { |
72 |
| - panic(err) |
73 |
| - } |
| 107 | + // pass, err := os.ReadFile(os.Getenv("SECRETS") + "/nats-password.txt") |
| 108 | + // if err != nil { |
| 109 | + // panic(err) |
| 110 | + // } |
74 | 111 |
|
75 |
| - var formatedServers string |
76 |
| - for _, x := range config.Nats.TomlServers { |
77 |
| - connectUrl := fmt.Sprintf("nats://%s:%s@%s,", user, pass, string(x)) |
78 |
| - formatedServers += connectUrl |
79 |
| - } |
| 112 | + // var formatedServers string |
| 113 | + // for _, x := range config.Nats.TomlServers { |
| 114 | + // connectUrl := fmt.Sprintf("nats://%s:%s@%s,", user, pass, string(x)) |
| 115 | + // formatedServers += connectUrl |
| 116 | + // } |
80 | 117 |
|
81 |
| - config.Nats.Servers = formatedServers |
| 118 | + // config.Nats.Servers = formatedServers |
82 | 119 |
|
83 | 120 | // webhook proxies
|
84 |
| - config.ProxyList = GetProxyList(config.ProxyPath) |
| 121 | + // config.ProxyList = GetProxyList(config.ProxyPath) |
85 | 122 |
|
86 | 123 | if config.Prod_env && config.Testing.Enabled {
|
87 | 124 | panic("cannot use testing in prod")
|
|
0 commit comments