Initial commit
This commit is contained in:
46
internal/configuration/config.go
Normal file
46
internal/configuration/config.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/goccy/go-yaml"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Deconnect Deconnect `yaml:"deconnect"`
|
||||
Upstream Upstream `yaml:"upstream"`
|
||||
}
|
||||
|
||||
type Deconnect struct {
|
||||
LogLevel logrus.Level `yaml:"log_level"`
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
}
|
||||
|
||||
type Upstream struct {
|
||||
URL string `yaml:"url"`
|
||||
InsecureTLS bool `yaml:"insecure_tls,omitempty"`
|
||||
}
|
||||
|
||||
func New() (*Config, error) {
|
||||
deconnectCfgPath := "/etc/deconnect.yaml"
|
||||
if customPath, ok := os.LookupEnv("DECONNECT_CONFIG"); ok {
|
||||
deconnectCfgPath = customPath
|
||||
}
|
||||
|
||||
rawConfig, err := os.ReadFile(deconnectCfgPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %w (%w)", ErrConfiguration, ErrCantReadConfigFile, err)
|
||||
}
|
||||
|
||||
config := new(Config)
|
||||
|
||||
err = yaml.Unmarshal(rawConfig, config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %w (%w)", ErrConfiguration, ErrCantParseConfigFile, err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
10
internal/configuration/errors.go
Normal file
10
internal/configuration/errors.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package configuration
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrConfiguration = errors.New("configuration")
|
||||
ErrCantReadConfigFile = errors.New("can't read config file")
|
||||
ErrCantParseConfigFile = errors.New("can't parse config file")
|
||||
ErrSourceDirectoryDoesNotExist = errors.New("source directory does not exist")
|
||||
)
|
||||
Reference in New Issue
Block a user