2022-06-24 00:41:58 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
2022-06-27 15:47:07 +00:00
|
|
|
"strconv"
|
2022-06-24 00:41:58 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
r Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewService(r Repository) Service {
|
|
|
|
return &service{r: r}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) LoadConfig(fileName string) (*Config, error) {
|
|
|
|
if s.r == nil || fileName == "" {
|
|
|
|
logrus.Infoln("config: LoadConfig")
|
|
|
|
cfToken, exists := os.LookupEnv("CF_TOKEN")
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.New("cannot find env variable CF_TOKEN")
|
|
|
|
}
|
2022-06-27 15:47:07 +00:00
|
|
|
domainaName, exists := os.LookupEnv("DOMAIN_NAME")
|
2022-06-24 00:41:58 +00:00
|
|
|
if !exists {
|
2022-06-27 15:47:07 +00:00
|
|
|
return nil, errors.New("cannot find env variable DOMAIN_NAME")
|
2022-06-24 00:41:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 15:47:07 +00:00
|
|
|
hostTimeout, exists := os.LookupEnv("HOST_LIVENESS_TIMEOUT")
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.New("cannot find env variable HOST_LIVENESS_TIMEOUT")
|
|
|
|
}
|
|
|
|
hostTimeoutInt, err := strconv.ParseInt(hostTimeout, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("incorrect HOST_LIVENESS_TIMEOUT value")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
logLevel, exists := os.LookupEnv(("LOG_LEVEL"))
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.New("cannot find env variable LOG_LEVEL")
|
2022-06-24 00:41:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 15:47:07 +00:00
|
|
|
return &Config{CloudflareToken: cfToken, DomainName: domainaName, HostTimeout: int(hostTimeoutInt), LogLevel: logLevel}, nil
|
2022-06-24 00:41:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.r.LoadFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.r.GetConfig(), nil
|
|
|
|
}
|