2 Commits
Author SHA1 Message Date
esilva 12ba08c5e5 Simplified the persistence backend a bit for now 2025-09-21 14:36:24 +01:00
esilva d9d64cb067 Added configuration 2025-09-21 14:30:33 +01:00
6 changed files with 80 additions and 13 deletions
+4 -2
View File
@@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"github.com/ESilva15/PTInvCertSWDB/config"
sw "github.com/ESilva15/PTInvCertSWDB/sw" sw "github.com/ESilva15/PTInvCertSWDB/sw"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -48,6 +49,7 @@ func count(c *gin.Context) {
} }
func RunServer() error { func RunServer() error {
cfg := config.GetInstance()
gin.SetMode(ginMode) gin.SetMode(ginMode)
router := gin.Default() router := gin.Default()
@@ -59,12 +61,12 @@ func RunServer() error {
} }
// Star the SW service // Star the SW service
err := sw.StartSWService(sw.FILEDB) err := sw.StartSWService()
if err != nil { if err != nil {
return err return err
} }
err = router.Run(":8086") err = router.Run(":" + cfg.Port)
if err != nil { if err != nil {
return err return err
} }
+4 -3
View File
@@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/ESilva15/PTInvCertSWDB/config"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
) )
@@ -12,11 +13,11 @@ type TokenData struct {
Token string `json:"JWTToken"` Token string `json:"JWTToken"`
} }
const key = "stronk"
func validateToken(tStr string) (bool, error) { func validateToken(tStr string) (bool, error) {
cfg := config.GetInstance()
token, err := jwt.Parse(tStr, func(token *jwt.Token) (any, error) { 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()})) }, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
if err != nil { if err != nil {
return false, err return false, err
+5
View File
@@ -0,0 +1,5 @@
port: 8086
authKey: "stronk"
dataBackend: "YAML"
yamlBackend:
path: "./swdb.yaml"
+56
View File
@@ -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())
}
}
+2
View File
@@ -2,8 +2,10 @@ package main
import ( import (
cmd "github.com/ESilva15/PTInvCertSWDB/cmd" cmd "github.com/ESilva15/PTInvCertSWDB/cmd"
"github.com/ESilva15/PTInvCertSWDB/config"
) )
func main() { func main() {
config.SetConfig("./config.yaml")
cmd.Execute() cmd.Execute()
} }
+9 -8
View File
@@ -4,15 +4,14 @@ package sw
import ( import (
"errors" "errors"
"github.com/ESilva15/PTInvCertSWDB/config"
models "github.com/ESilva15/PTInvCertSWDB/sw/models" models "github.com/ESilva15/PTInvCertSWDB/sw/models"
repo "github.com/ESilva15/PTInvCertSWDB/sw/repo" repo "github.com/ESilva15/PTInvCertSWDB/sw/repo"
) )
type RepoType int
const ( const (
FILEDB RepoType = iota FILEDB = "YAML"
POSTGRES POSTGRES = "PG"
) )
var service *SWService var service *SWService
@@ -21,18 +20,20 @@ type SWService struct {
SWRepo repo.SWRepo SWRepo repo.SWRepo
} }
func StartSWService(rt RepoType) error { func StartSWService() error {
cfg := config.GetInstance()
var err error var err error
var swRepo repo.SWRepo var swRepo repo.SWRepo
switch rt { switch cfg.DataBackend {
case FILEDB: case FILEDB:
swRepo, err = repo.NewYAMLRepo("./swdb.yaml") swRepo, err = repo.NewYAMLRepo(cfg.YamlBackend.Path)
if err != nil { if err != nil {
return errors.New("Failed to create yaml repo: " + err.Error()) return errors.New("Failed to create yaml repo: " + err.Error())
} }
default: default:
return errors.New("repo type not implemented yet") return errors.New("chosen persistence backend not implemented yet")
} }
service = &SWService{ service = &SWService{