From 7de898962ddfb7a39d73457b1522e5808960bd5e Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Sat, 9 Nov 2024 05:34:23 +0100 Subject: [PATCH] fix: error in creating default configuration --- internal/config.go | 16 +++++++--------- internal/var.go | 1 + util/helpers.go | 10 ++++++++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/internal/config.go b/internal/config.go index b4c7d8d..50b59c8 100644 --- a/internal/config.go +++ b/internal/config.go @@ -75,6 +75,13 @@ func (GatewayServer) Config(configFile string) (*GatewayServer, error) { } logger.Info("Generating new configuration file...") + //check if config directory does exist + if !util.FolderExists(ConfigDir) { + err := os.MkdirAll(ConfigDir, os.ModePerm) + if err != nil { + return nil, err + } + } initConfig(ConfigFile) logger.Info("Server configuration file is available at %s", ConfigFile) util.SetEnv("GOMA_CONFIG_FILE", ConfigFile) @@ -88,7 +95,6 @@ func (GatewayServer) Config(configFile string) (*GatewayServer, error) { return nil, fmt.Errorf("in file %q: %w", ConfigFile, err) } logger.Info("Generating new configuration file...done") - logger.Info("Starting server with default configuration") return &GatewayServer{ ctx: nil, gateway: c.GatewayConfig, @@ -257,14 +263,6 @@ func initConfig(configFile string) { } logger.Info("Configuration file has been initialized successfully") } -func Get() *Gateway { - if cfg == nil { - c := &Gateway{} - c.Setup(GetConfigPaths()) - cfg = c - } - return cfg -} func (Gateway) Setup(conf string) *Gateway { if util.FileExists(conf) { buf, err := os.ReadFile(conf) diff --git a/internal/var.go b/internal/var.go index 7f9c5b1..204fb80 100644 --- a/internal/var.go +++ b/internal/var.go @@ -1,5 +1,6 @@ package pkg +const ConfigDir = "/etc/goma/" // Default configuration file const ConfigFile = "/etc/goma/goma.yml" // Default configuration file const accessControlAllowOrigin = "Access-Control-Allow-Origin" // Cors const serverName = "Goma" diff --git a/util/helpers.go b/util/helpers.go index 6daf98d..6389439 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -24,6 +24,16 @@ func FileExists(filename string) bool { } return !info.IsDir() } + +// FolderExists checks if the folder does exist +func FolderExists(name string) bool { + info, err := os.Stat(name) + if os.IsNotExist(err) { + return false + } + return info.IsDir() + +} func GetStringEnv(key, defaultValue string) string { val := os.Getenv(key) if val == "" {