mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
refactor(backend,worker): update environment variable keys for consistency and improve configuration handling across backend and worker modules
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user