feat: setup config (viper =)), jwt settings && session struct
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
Server Server `mapstructure:"server"`
|
||||
JWT JWT `mapstructure:"jwt"`
|
||||
DB DB `mapstructure:"db"`
|
||||
Redis Redis `mapstructure:"redis"`
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
}
|
||||
|
||||
type DB struct {
|
||||
User string `mapstructure:"user"`
|
||||
Pass string `mapstructure:"pass"`
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
DBName string `mapstructure:"db_name"`
|
||||
}
|
||||
|
||||
type Redis struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
Pass string `mapstructure:"pass"`
|
||||
}
|
||||
|
||||
type JWT struct {
|
||||
TokenExpMinutes uint `mapstructure:"token_exp_minutes"`
|
||||
RefreshTokenExpMinutes uint `mapstructure:"refresh_token_exp_minutes"`
|
||||
TokenSecret string `mapstructure:"token_secret"`
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func ReadGeneric[T any](cfgPath string) (T, error) {
|
||||
var cfg T
|
||||
fullAbsPath, err := absPath(cfgPath)
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
// configPath := filepath.Dir(fullAbsPath)
|
||||
// viper.AddConfigPath(configPath)
|
||||
// configType := strings.TrimPrefix(filepath.Ext(fullAbsPath), ".")
|
||||
// viper.SetConfigType(configType)
|
||||
// configFile := strings.TrimSuffix(filepath.Base(fullAbsPath), filepath.Ext(fullAbsPath))
|
||||
// viper.SetConfigName(configFile)
|
||||
|
||||
viper.SetConfigFile(fullAbsPath)
|
||||
|
||||
viper.AutomaticEnv()
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
return cfg, viper.Unmarshal(&cfg)
|
||||
}
|
||||
|
||||
func ReadStandard(cfgPath string) (Config, error) {
|
||||
return ReadGeneric[Config](cfgPath)
|
||||
}
|
||||
|
||||
func absPath(cfgPath string) (string, error) {
|
||||
if !filepath.IsAbs(cfgPath) {
|
||||
return filepath.Abs(cfgPath)
|
||||
}
|
||||
return cfgPath, nil
|
||||
}
|
||||
|
||||
func MustReadStandard(configPath string) Config {
|
||||
cfg, err := ReadStandard(configPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
Reference in New Issue
Block a user