From d9d64cb067e044c9ae1d8320b93489c0224fedda Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Sun, 21 Sep 2025 14:30:33 +0100 Subject: [PATCH] Added configuration --- api/api.go | 4 +++- api/authMiddleware.go | 7 +++--- config.yaml | 5 ++++ config/config.go | 56 +++++++++++++++++++++++++++++++++++++++++++ main.go | 2 ++ 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 config.yaml create mode 100644 config/config.go diff --git a/api/api.go b/api/api.go index 88b7375..e2fa361 100644 --- a/api/api.go +++ b/api/api.go @@ -5,6 +5,7 @@ import ( "net/http" "strconv" + "github.com/ESilva15/PTInvCertSWDB/config" sw "github.com/ESilva15/PTInvCertSWDB/sw" "github.com/gin-gonic/gin" @@ -48,6 +49,7 @@ func count(c *gin.Context) { } func RunServer() error { + cfg := config.GetInstance() gin.SetMode(ginMode) router := gin.Default() @@ -64,7 +66,7 @@ func RunServer() error { return err } - err = router.Run(":8086") + err = router.Run(":" + cfg.Port) if err != nil { return err } diff --git a/api/authMiddleware.go b/api/authMiddleware.go index 9602010..c783f14 100644 --- a/api/authMiddleware.go +++ b/api/authMiddleware.go @@ -4,6 +4,7 @@ import ( "net/http" "strings" + "github.com/ESilva15/PTInvCertSWDB/config" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v4" ) @@ -12,11 +13,11 @@ type TokenData struct { Token string `json:"JWTToken"` } -const key = "stronk" - func validateToken(tStr string) (bool, error) { + cfg := config.GetInstance() + token, err := jwt.Parse(tStr, func(token *jwt.Token) (any, error) { - return []byte(key), nil + return []byte(cfg.AuthKey), nil }, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()})) if err != nil { return false, err diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..109a74b --- /dev/null +++ b/config.yaml @@ -0,0 +1,5 @@ +port: 8086 +authKey: "stronk" +dataBackend: "YAML" +yamlBackend: + path: "./swdb.yaml" diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..62daf62 --- /dev/null +++ b/config/config.go @@ -0,0 +1,56 @@ +// Package config defines the configuration of our app +package config + +import ( + "log" + "os" + "sync" + + "gopkg.in/yaml.v2" +) + +type YamlBackend struct { + Path string `yaml:"path"` +} + +// Configuration defines the base configuration of the expenses app +type Configuration struct { + Port string `yaml:"port"` + AuthKey string `yaml:"authKey"` + DataBackend string `yaml:"dataBackend"` + YamlBackend YamlBackend `yaml:"yamlBackend"` +} + +var ( + instance *Configuration + once sync.Once + confPath string +) + +// GetInstance returns the instance of the configuration of the app. +func GetInstance() *Configuration { + once.Do(func() { + instance = &Configuration{} + instance.loadConfiguration() + }) + + return instance +} + +// SetConfig sets the path for the configuration file. +func SetConfig(path string) { + confPath = path +} + +func (c *Configuration) loadConfiguration() { + file, err := os.ReadFile(confPath) + if err != nil { + log.Fatalf("Unable to open configuration file [%s]: %s", confPath, + err.Error()) + } + + err = yaml.Unmarshal(file, &instance) + if err != nil { + log.Fatalf("Error parsing JSON: %s", err.Error()) + } +} diff --git a/main.go b/main.go index aa17f5f..faa6cea 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,10 @@ package main import ( cmd "github.com/ESilva15/PTInvCertSWDB/cmd" + "github.com/ESilva15/PTInvCertSWDB/config" ) func main() { + config.SetConfig("./config.yaml") cmd.Execute() }