1
This commit is contained in:
53
internal/config/config.go
Normal file
53
internal/config/config.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/ilyakaznacheev/cleanenv"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInternal = errors.New("can not load config")
|
||||
ErrMarshaling = errors.New("can not marshal config path")
|
||||
)
|
||||
|
||||
func MustLoad(version, commit, date string) *Config {
|
||||
cfg, err := loadConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cfg.Build.Version = version
|
||||
cfg.Build.Commit = commit
|
||||
cfg.Build.Date = date
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func loadConfig() (*Config, error) {
|
||||
if _, err := os.Stat(ConfigBasePath); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("no config file found")
|
||||
}
|
||||
|
||||
var localConfig LocalDeploy
|
||||
|
||||
if err := cleanenv.ReadConfig(ConfigBasePath, &localConfig); err != nil {
|
||||
return nil, fmt.Errorf("error reading local config: %w", err)
|
||||
}
|
||||
|
||||
for _, env := range localConfig.Deploy.Env {
|
||||
if err := os.Setenv(env.Name, env.Value); err != nil {
|
||||
return nil, fmt.Errorf("error setting env %s: %w", env.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
|
||||
if err := cleanenv.ReadConfig(ConfigBasePath, &cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
5
internal/config/constants.go
Normal file
5
internal/config/constants.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
const (
|
||||
ConfigBasePath = "/home/alexorel/work/Molva/molva-backend/.build/config/local.example.yaml"
|
||||
)
|
99
internal/config/dictionary.go
Normal file
99
internal/config/dictionary.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type (
|
||||
Config struct {
|
||||
Env string `yaml:"env" env-default:"production"`
|
||||
Host string `yaml:"host" env-default:"localhost"`
|
||||
Port int `yaml:"port" env-default:"8000"`
|
||||
Keycloak Keycloak
|
||||
Broker Broker `yaml:"broker"`
|
||||
Secret SecretConfig
|
||||
EmailVerificationService EmailVerificationService `yaml:"emailVerificationService"`
|
||||
ValkeyCache ValkeyCacheConfig `yaml:"cache"`
|
||||
S3Storage S3StorageConfig `yaml:"s3Storage"`
|
||||
Database Database `yaml:"database"`
|
||||
Integration Integration
|
||||
Build BuildInfo
|
||||
}
|
||||
|
||||
BuildInfo struct {
|
||||
Version string
|
||||
Commit string
|
||||
Date string
|
||||
}
|
||||
|
||||
ValkeyCacheConfig struct {
|
||||
Addrs string `yaml:"-" env:"CACHE_ADDRS" env-default:"localhost:6379"`
|
||||
Password string `yaml:"-" env:"CACHE_PASSWORD" env-default:""`
|
||||
ReadOnly bool `yaml:"readOnly" env-default:"false"`
|
||||
DialTimeout time.Duration `yaml:"dialTimeout" env-default:"5s"`
|
||||
PoolSize int `yaml:"poolSize" env-default:"10"`
|
||||
DefaultTTL time.Duration `yaml:"defaultTtl" env-default:"1h"`
|
||||
RootCaFilePath string `yaml:"rootCaFilePath" env-default:""`
|
||||
}
|
||||
|
||||
S3StorageConfig struct {
|
||||
Bucket string `yaml:"bucket" env-default:""`
|
||||
DefaultLinkTTL time.Duration `yaml:"defaultLinkTtl" env-default:"1h"`
|
||||
}
|
||||
|
||||
SecretConfig struct {
|
||||
Key string `env:"SHORTENER_SECRET_KEY"` // 32-byte key
|
||||
}
|
||||
|
||||
Keycloak struct {
|
||||
BaseURL string `env:"KEYCLOAK_BASE_URL" env-default:"http://localhost"`
|
||||
Realm string `env:"KEYCLOAK_REALM" env-default:""`
|
||||
ClientId string `env:"KEYCLOAK_CLIENT_ID" env-default:""`
|
||||
ClientSecret string `env:"KEYCLOAK_CLIENT_SECRET" env-default:""`
|
||||
}
|
||||
|
||||
Broker struct {
|
||||
Host string `yaml:"-" env:"BROKER_HOST" env-default:"amqp://user:password@localhost"`
|
||||
Port int `yaml:"-" env:"BROKER_PORT" env-default:"5672"`
|
||||
UserName string `yaml:"-" env:"BROKER_USERNAME" env-default:"user"`
|
||||
Password string `yaml:"-" env:"BROKER_PASSWORD" env-default:"password"`
|
||||
NotificationsQueueName string `yaml:"notificationsQueueName" env-default:"notifications"`
|
||||
}
|
||||
|
||||
// TODO: че с этим делать? Его нигде нет (ни на дев, ни на прод)
|
||||
EmailVerificationService struct {
|
||||
APIKey string `yaml:"-" env-default:""`
|
||||
}
|
||||
|
||||
Database struct {
|
||||
Host string `yaml:"-" env:"DB_HOSTS"`
|
||||
Port uint16 `yaml:"-" env:"DB_PORT"`
|
||||
Username string `yaml:"-" env:"DB_USERNAME"`
|
||||
Password string `yaml:"-" env:"DB_PASSWORD"`
|
||||
DBName string `yaml:"-" env:"DB_DBNAME"`
|
||||
Schema string `yaml:"schema"`
|
||||
SSLMode string `yaml:"sslmode" env-default:"verify-full"`
|
||||
RootCaFilePath string `yaml:"rootCaFilePath" env-default:"/root/YandexInternalRootCA.crt"`
|
||||
}
|
||||
|
||||
Integration struct {
|
||||
Vkusvill VkusvillIntegration `yaml:"vkusvill"`
|
||||
}
|
||||
|
||||
VkusvillIntegration struct {
|
||||
ApiToken string `yaml:"-" env:"INTEGRATION_VKUSVILL_API_TOKEN"`
|
||||
}
|
||||
)
|
||||
|
||||
type (
|
||||
LocalDeploy struct {
|
||||
Deploy LocalDeployConfig `yaml:"deploy"`
|
||||
}
|
||||
|
||||
LocalDeployConfig struct {
|
||||
Env []LocalDeployConfigElement `yaml:"env"`
|
||||
}
|
||||
|
||||
LocalDeployConfigElement struct {
|
||||
Name string `yaml:"name"`
|
||||
Value string `yaml:"value"`
|
||||
}
|
||||
)
|
Reference in New Issue
Block a user