mirror of
https://github.com/keven1024/015.git
synced 2026-06-08 13:24:33 +00:00
feat(backend): add password hashing error handling and implement HTTP utility tests
This commit is contained in:
106
pkg/utils/env.go
106
pkg/utils/env.go
@@ -1,53 +1,99 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var v *viper.Viper
|
||||
var (
|
||||
v *viper.Viper
|
||||
envOnce sync.Once
|
||||
)
|
||||
|
||||
func init() {
|
||||
InitEnv()
|
||||
}
|
||||
|
||||
func InitEnv() {
|
||||
func InitEnv(props EnvOption) {
|
||||
if v != nil {
|
||||
return
|
||||
}
|
||||
v = viper.New()
|
||||
v.SetConfigName("config.yaml")
|
||||
v.SetConfigType("yaml")
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
v.AddConfigPath(".")
|
||||
v.AddConfigPath("../")
|
||||
v.AutomaticEnv()
|
||||
v.WatchConfig()
|
||||
err := v.ReadInConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
// if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||
// // 只有当错误不是"配置文件未找到"时才 panic
|
||||
// panic(err)
|
||||
// }
|
||||
envOnce.Do(func() {
|
||||
v = viper.New()
|
||||
if props.ConfigData != nil {
|
||||
v.ReadConfig(props.ConfigData)
|
||||
return
|
||||
}
|
||||
for _, name := range props.ConfigName {
|
||||
v.SetConfigName(name)
|
||||
}
|
||||
for _, viperConfigType := range props.ConfigType {
|
||||
v.SetConfigType(viperConfigType)
|
||||
}
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
for _, path := range props.ConfigPath {
|
||||
v.AddConfigPath(path)
|
||||
}
|
||||
v.AutomaticEnv()
|
||||
v.WatchConfig()
|
||||
err := v.ReadInConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type Option interface {
|
||||
applyTo(*EnvOption)
|
||||
}
|
||||
|
||||
type EnvOption struct {
|
||||
DefaultValue string
|
||||
ConfigPath []string
|
||||
ConfigName []string
|
||||
ConfigType []string
|
||||
ConfigData io.Reader // 测试环境使用
|
||||
}
|
||||
|
||||
type WithDefaultValue string
|
||||
|
||||
func (o WithDefaultValue) applyTo(props *EnvOption) {
|
||||
props.DefaultValue = string(o)
|
||||
}
|
||||
|
||||
func getEnvOptions(options ...Option) EnvOption {
|
||||
props := EnvOption{
|
||||
DefaultValue: "",
|
||||
ConfigPath: []string{".", "../"},
|
||||
ConfigName: []string{"config"},
|
||||
ConfigType: []string{"yaml"},
|
||||
}
|
||||
for _, option := range options {
|
||||
option.applyTo(&props)
|
||||
}
|
||||
return props
|
||||
}
|
||||
|
||||
func GetEnv(key string) string {
|
||||
InitEnv()
|
||||
return v.GetString(key)
|
||||
}
|
||||
|
||||
func GetEnvWithDefault(key string, defaultValue string) string {
|
||||
func GetEnv(key string, options ...Option) string {
|
||||
props := getEnvOptions(options...)
|
||||
InitEnv(props)
|
||||
value := v.GetString(key)
|
||||
if value == "" {
|
||||
return defaultValue
|
||||
|
||||
if value == "" && props.DefaultValue != "" {
|
||||
return props.DefaultValue
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func GetEnvWithDefault(key string, defaultValue string) string {
|
||||
return GetEnv(key, WithDefaultValue(defaultValue))
|
||||
}
|
||||
|
||||
func GetEnvMapString(key string) map[string]string {
|
||||
InitEnv()
|
||||
props := getEnvOptions()
|
||||
InitEnv(props)
|
||||
return v.GetStringMapString(key)
|
||||
}
|
||||
|
||||
func SetEnv(key string, value string) {
|
||||
v.Set(key, value)
|
||||
}
|
||||
|
||||
@@ -2,12 +2,16 @@ package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var rdb *redis.Client = InitRedis()
|
||||
var ctx = context.Background()
|
||||
var (
|
||||
rdb *redis.Client
|
||||
ctx = context.Background()
|
||||
onceRedis sync.Once
|
||||
)
|
||||
|
||||
func InitRedis() *redis.Client {
|
||||
opt, err := redis.ParseURL(GetEnv("redis.url"))
|
||||
@@ -18,5 +22,10 @@ func InitRedis() *redis.Client {
|
||||
}
|
||||
|
||||
func GetRedisClient() (*redis.Client, context.Context) {
|
||||
onceRedis.Do(func() {
|
||||
if rdb == nil {
|
||||
rdb = InitRedis()
|
||||
}
|
||||
})
|
||||
return rdb, ctx
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user