Change JWT TTL config from duration to string for flexibility

- The mapstructure library is no longer needed for direct duration
  parsing since we now store TTLs as string durations (e.g., "15m",
  "168h") and parse them on demand via helper methods.
- This allows more flexible duration formats in configuration and moves
  the parsing responsibility to the JWT config struct itself.
This commit is contained in:
2026-04-27 17:15:16 +08:00
parent 54f0deadbc
commit c0c34eb914
4 changed files with 39 additions and 20 deletions

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"strings"
"github.com/go-viper/mapstructure/v2"
"github.com/spf13/viper"
)
@@ -24,10 +23,6 @@ func defaults(v *viper.Viper) {
v.SetDefault("jwt.refresh_ttl", "168h")
}
func decodeHook() viper.DecoderConfigOption {
return viper.DecodeHook(mapstructure.StringToTimeDurationHookFunc())
}
func New() *viper.Viper {
v := viper.New()
v.SetEnvPrefix("MYGO")
@@ -54,7 +49,7 @@ func Load(v *viper.Viper, cfgFile string) (*Config, error) {
}
var cfg Config
if err := v.Unmarshal(&cfg, decodeHook()); err != nil {
if err := v.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("unmarshal config: %w", err)
}