Add jwt crypto module, support unit test for config module

Signed-off-by:  Asai Neko<sugar@sne.moe>
This commit is contained in:
2025-12-23 18:11:31 +08:00
parent 1505783c62
commit d314942c08
13 changed files with 212 additions and 11 deletions

View File

@@ -2,17 +2,29 @@ package config
import (
"log"
"os"
"github.com/spf13/viper"
)
func Init() {
// Set config path by env
confPath := os.Getenv("CONFIG_PATH")
if confPath == "" {
confPath = "config.yaml"
}
// Read global config
viper.SetConfigFile("config.yaml")
viper.SetConfigFile(confPath)
viper.SetDefault("Server", serverDef)
viper.SetDefault("Database", databaseDef)
conf := &config{}
if err := viper.ReadInConfig(); err != nil {
// Dont generate config when using dev mode
if os.Getenv("GO_ENV") == "test" || os.Getenv("CONFIG_PATH") != "" {
log.Fatalf("[Config] failed to read config %s: %v", confPath, err)
}
log.Println("Can't read config, trying to modify!")
if err := viper.WriteConfig(); err != nil {
log.Fatal("[Config] Error writing config: ", err)
@@ -24,17 +36,9 @@ func Init() {
}
func Get(key string) any {
viper.SetConfigFile("config.yaml")
if err := viper.ReadInConfig(); err != nil {
log.Fatal("[Config] Error reading config: ", err)
}
return viper.Get(key)
}
func Set(key string, value any) {
viper.SetConfigFile("config.yaml")
if err := viper.ReadInConfig(); err != nil {
log.Fatal("[Config] Error reading config: ", err)
}
viper.Set(key, value)
}