started working on the backend
This commit is contained in:
95
backend/pkg/config/config.go
Normal file
95
backend/pkg/config/config.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/johannesbuehl/golunteer/backend/pkg/lib"
|
||||
"github.com/rs/zerolog"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type ReservationConfig struct {
|
||||
Expiration time.Duration
|
||||
}
|
||||
|
||||
type ConfigStruct struct {
|
||||
ConfigYaml
|
||||
LogLevel zerolog.Level
|
||||
SessionExpire time.Duration
|
||||
}
|
||||
|
||||
var Config ConfigStruct
|
||||
|
||||
type Payload struct {
|
||||
jwt.RegisteredClaims
|
||||
CustomClaims map[string]any
|
||||
}
|
||||
|
||||
func (config ConfigStruct) SignJWT(val any) (string, error) {
|
||||
valMap, err := lib.StrucToMap(val)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
payload := Payload{
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(config.SessionExpire)),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
},
|
||||
CustomClaims: valMap,
|
||||
}
|
||||
|
||||
t := jwt.NewWithClaims(jwt.SigningMethodHS256, payload)
|
||||
|
||||
return t.SignedString([]byte(config.ClientSession.JwtSignature))
|
||||
}
|
||||
|
||||
func loadConfig() ConfigStruct {
|
||||
Config := ConfigYaml{}
|
||||
|
||||
yamlFile, err := os.ReadFile("config.yaml")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Error opening config-file: %q", err))
|
||||
}
|
||||
|
||||
reader := bytes.NewReader(yamlFile)
|
||||
|
||||
dec := yaml.NewDecoder(reader)
|
||||
dec.KnownFields(true)
|
||||
err = dec.Decode(&Config)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error parsing config-file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if logLevel, err := zerolog.ParseLevel(Config.LogLevel); err != nil {
|
||||
panic(fmt.Errorf("can't parse log-level: %v", err))
|
||||
} else {
|
||||
var configStruct ConfigStruct
|
||||
|
||||
// parse the durations
|
||||
if session_expire, err := time.ParseDuration(Config.ClientSession.Expire); err != nil {
|
||||
log.Fatalf(`Error parsing "client_session.expire": %v`, err)
|
||||
|
||||
// parse the templates
|
||||
} else {
|
||||
configStruct = ConfigStruct{
|
||||
ConfigYaml: Config,
|
||||
LogLevel: logLevel,
|
||||
SessionExpire: session_expire,
|
||||
}
|
||||
}
|
||||
|
||||
return configStruct
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
Config = loadConfig()
|
||||
}
|
||||
75
backend/pkg/config/configYAML.go
Normal file
75
backend/pkg/config/configYAML.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var CONFIG_PATH = "config.yaml"
|
||||
|
||||
type ConfigYaml struct {
|
||||
LogLevel string `yaml:"log_level"`
|
||||
Database struct {
|
||||
Host string `yaml:"host"`
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
Database string `yaml:"database"`
|
||||
} `yaml:"database"`
|
||||
Server struct {
|
||||
Port int `yaml:"port"`
|
||||
} `yaml:"server"`
|
||||
ClientSession struct {
|
||||
JwtSignature string `yaml:"jwt_signature"`
|
||||
Expire string `yaml:"expire"`
|
||||
} `yaml:"client_session"`
|
||||
}
|
||||
|
||||
type CacheConfig struct {
|
||||
Expiration time.Duration
|
||||
Purge time.Duration
|
||||
}
|
||||
|
||||
var YamlConfig ConfigYaml
|
||||
|
||||
func _loadConfig() ConfigYaml {
|
||||
config := ConfigYaml{}
|
||||
|
||||
yamlFile, err := os.ReadFile(CONFIG_PATH)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Error opening config-file: %v", err))
|
||||
}
|
||||
|
||||
reader := bytes.NewReader(yamlFile)
|
||||
|
||||
dec := yaml.NewDecoder(reader)
|
||||
dec.KnownFields(true)
|
||||
err = dec.Decode(&config)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error parsing config-file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func WriteConfig() {
|
||||
buf := bytes.Buffer{}
|
||||
enc := yaml.NewEncoder(&buf)
|
||||
enc.SetIndent(2)
|
||||
// Can set default indent here on the encoder
|
||||
if err := enc.Encode(&YamlConfig); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
if err := os.WriteFile(CONFIG_PATH, buf.Bytes(), 0644); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
YamlConfig = _loadConfig()
|
||||
}
|
||||
Reference in New Issue
Block a user