first working server
This commit is contained in:
@@ -1,38 +1,69 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"GoMembership/internal/utils"
|
||||
"GoMembership/pkg/logger"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type DatabaseConfig struct {
|
||||
DBPath string `json:"DBPath"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
DB DatabaseConfig `json:"db"`
|
||||
type AuthenticationConfig struct {
|
||||
JWTSecret string
|
||||
CSRFSecret string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
DB DatabaseConfig `json:"db"`
|
||||
Auth AuthenticationConfig
|
||||
}
|
||||
|
||||
var (
|
||||
pConfig Config
|
||||
once sync.Once
|
||||
loaded bool
|
||||
)
|
||||
|
||||
func LoadConfig() *Config {
|
||||
path, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatalf("could not get working directory: %v", err)
|
||||
logger.Error.Fatalf("could not get working directory: %v", err)
|
||||
}
|
||||
|
||||
configFile, err := os.Open(filepath.Join(path, "configs", "config.json"))
|
||||
if err != nil {
|
||||
log.Fatalf("could not open config file: %v", err)
|
||||
logger.Error.Fatalf("could not open config file: %v", err)
|
||||
}
|
||||
defer configFile.Close()
|
||||
|
||||
decoder := json.NewDecoder(configFile)
|
||||
config := &Config{}
|
||||
err = decoder.Decode(config)
|
||||
// pConfig = &Config{}
|
||||
err = decoder.Decode(&pConfig)
|
||||
if err != nil {
|
||||
log.Fatalf("could not decode config file: %v", err)
|
||||
logger.Error.Fatalf("could not decode config file: %v", err)
|
||||
}
|
||||
if !loaded {
|
||||
once.Do(
|
||||
func() {
|
||||
csrfSecret, err := utils.GenerateRandomString(32)
|
||||
if err != nil {
|
||||
logger.Error.Fatalf("could not generate CSRF secret: %v", err)
|
||||
}
|
||||
|
||||
jwtSecret, err := utils.GenerateRandomString(32)
|
||||
if err != nil {
|
||||
logger.Error.Fatalf("could not generate JWT secret: %v", err)
|
||||
}
|
||||
pConfig.Auth.JWTSecret = jwtSecret
|
||||
pConfig.Auth.CSRFSecret = csrfSecret
|
||||
loaded = true
|
||||
})
|
||||
}
|
||||
|
||||
return config
|
||||
return &pConfig
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user