From 29588e0829b79367e576302ced0770dd96ac048e Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 4 Oct 2024 17:50:03 -0400 Subject: [PATCH] main file for config package --- config/main.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 config/main.go diff --git a/config/main.go b/config/main.go new file mode 100644 index 0000000..8711cc0 --- /dev/null +++ b/config/main.go @@ -0,0 +1,36 @@ +package config + +import ( + "encoding/json" + "os" +) + +type Config struct { + RedisURL string `json:"redisURL"` + ServiceAddress string `json:"serviceAddress"` + WebServerPort int `json:"webServerPort"` +} + +func LoadConfig(file string) (*Config, error) { + // Open the config file + configFile, err := os.Open(file) + if err != nil { + return nil, err + } + defer func(configFile *os.File) { + err := configFile.Close() + if err != nil { + return + } + }(configFile) + + // Parse the JSON file into a Config struct + var config Config + decoder := json.NewDecoder(configFile) + err = decoder.Decode(&config) + if err != nil { + return nil, err + } + + return &config, nil +}