refactor(backend,worker): update environment variable keys for consistency and improve configuration handling across backend and worker modules

This commit is contained in:
keven
2025-10-18 23:54:11 +08:00
parent d294027463
commit b80cb0c2d8
12 changed files with 33 additions and 25 deletions

View File

@@ -25,7 +25,7 @@ func DownloadShare(c echo.Context) error {
}
claims := DownloadShareClaims{}
t, err := jwt.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
return []byte(utils.GetEnv("download_secret")), nil
return []byte(utils.GetEnv("download.secret")), nil
})
if err != nil {
return utils.HTTPErrorHandler(c, err)
@@ -96,7 +96,7 @@ func VaildateShare(c echo.Context) error {
})
// Sign and get the complete encoded token as a string using the secret
downloadToken, err := token.SignedString([]byte(utils.GetEnv("download_secret")))
downloadToken, err := token.SignedString([]byte(utils.GetEnv("download.secret")))
if err != nil {
return utils.HTTPErrorHandler(c, err)
}

View File

@@ -40,7 +40,7 @@ func CreateUploadTask(c echo.Context) error {
"chunk_size": fileInfo.ChunkSize,
})
}
maxStorageSize, err := utils.GetFileSize(utils.GetEnv("MAX_LOCALSTORAGE_SIZE"))
maxStorageSize, err := utils.GetFileSize(utils.GetEnv("upload.maximum"))
if err != nil {
return utils.HTTPErrorHandler(c, err)
}

View File

@@ -70,7 +70,7 @@ func GetStat(c echo.Context) error {
return utils.HTTPErrorHandler(c, err)
}
maxStorageSize, err := utils.GetFileSize(utils.GetEnv("MAX_LOCALSTORAGE_SIZE"))
maxStorageSize, err := utils.GetFileSize(utils.GetEnv("upload.maximum"))
if err != nil {
return utils.HTTPErrorHandler(c, err)
}

View File

@@ -3,17 +3,17 @@ package utils
import "github.com/hibiken/asynq"
func GetQueueClient() *asynq.Client {
opt := RedisURI2AsynqOpt(GetEnv("REDIS_URL"))
opt := RedisURI2AsynqOpt(GetEnv("redis.url"))
return asynq.NewClient(opt)
}
func GetQueueInspector() *asynq.Inspector {
opt := RedisURI2AsynqOpt(GetEnv("REDIS_URL"))
opt := RedisURI2AsynqOpt(GetEnv("redis.url"))
return asynq.NewInspector(opt)
}
func RedisURI2AsynqOpt(uri string) asynq.RedisConnOpt {
opt, err := asynq.ParseRedisURI(GetEnv("REDIS_URL"))
opt, err := asynq.ParseRedisURI(GetEnv("redis.url"))
if err != nil {
panic(err)
}

View File

@@ -1,6 +1,8 @@
package utils
import (
"strings"
"github.com/spf13/viper"
)
@@ -15,12 +17,14 @@ func InitEnv() {
return
}
v = viper.New()
v.SetConfigName(".env")
v.SetConfigType("env")
v.SetConfigName("config.yaml")
v.SetConfigType("yaml")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AddConfigPath(".")
v.AddConfigPath("../")
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
err := v.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// 只有当错误不是"配置文件未找到"时才 panic
panic(err)

View File

@@ -8,7 +8,7 @@ import (
)
func GeneratePasswordHash(password string) (string, error) {
salt := GetEnv("PASSWORD_SALT")
salt := GetEnv("password.salt")
if salt == "" {
return "", errors.New("请配置PASSWORD_SALT")
}

View File

@@ -9,8 +9,8 @@ import (
func TestGeneratePasswordHash(t *testing.T) {
// 保存原始环境变量
originalSalt := os.Getenv("PASSWORD_SALT")
defer os.Setenv("PASSWORD_SALT", originalSalt)
originalSalt := os.Getenv("password.salt")
defer os.Setenv("password.salt", originalSalt)
tests := []struct {
name string
@@ -20,11 +20,11 @@ func TestGeneratePasswordHash(t *testing.T) {
errorMsg string
}{
{
name: "PASSWORD_SALT未配置",
name: "password.salt未配置",
password: "testpassword",
salt: "",
expectError: true,
errorMsg: "请配置PASSWORD_SALT",
errorMsg: "请配置password.salt",
},
{
name: "正常生成哈希",
@@ -38,9 +38,9 @@ func TestGeneratePasswordHash(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
// 设置环境变量
if tt.salt != "" {
os.Setenv("PASSWORD_SALT", tt.salt)
os.Setenv("password.salt", tt.salt)
} else {
os.Unsetenv("PASSWORD_SALT")
os.Unsetenv("password.salt")
}
hash, err := GeneratePasswordHash(tt.password)

View File

@@ -10,7 +10,7 @@ var rdb *redis.Client = InitRedis()
var ctx = context.Background()
func InitRedis() *redis.Client {
opt, err := redis.ParseURL(GetEnv("REDIS_URL"))
opt, err := redis.ParseURL(GetEnv("redis.url"))
if err != nil {
panic(err)
}

View File

@@ -3,7 +3,7 @@ package utils
import "github.com/hibiken/asynq"
func RedisURI2AsynqOpt(uri string) asynq.RedisConnOpt {
opt, err := asynq.ParseRedisURI(GetEnv("REDIS_URL"))
opt, err := asynq.ParseRedisURI(GetEnv("redis.url"))
if err != nil {
panic(err)
}

View File

@@ -1,6 +1,8 @@
package utils
import (
"strings"
"github.com/spf13/viper"
)
@@ -15,12 +17,14 @@ func InitEnv() {
return
}
v = viper.New()
v.SetConfigName(".env")
v.SetConfigType("env")
v.SetConfigName("config.yaml")
v.SetConfigType("yaml")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AddConfigPath(".")
v.AddConfigPath("../")
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
err := v.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// 只有当错误不是"配置文件未找到"时才 panic
panic(err)

View File

@@ -10,7 +10,7 @@ var rdb *redis.Client = InitRedis()
var ctx = context.Background()
func InitRedis() *redis.Client {
opt, err := redis.ParseURL(GetEnv("REDIS_URL"))
opt, err := redis.ParseURL(GetEnv("redis.url"))
if err != nil {
panic(err)
}

View File

@@ -23,8 +23,8 @@ func main() {
zap.ReplaceGlobals(logger)
srv := asynq.NewServer(
utils.RedisURI2AsynqOpt(utils.GetEnv("REDIS_URL")),
asynq.Config{Concurrency: cast.ToInt(utils.GetEnvWithDefault("WORKER_CONCURRENCY", "4"))},
utils.RedisURI2AsynqOpt(utils.GetEnv("redis.url")),
asynq.Config{Concurrency: cast.ToInt(utils.GetEnvWithDefault("worker.concurrency", "4"))},
)
mux := asynq.NewServeMux()