Skip to content

Commit

Permalink
feat: Config Live Reload (#12)
Browse files Browse the repository at this point in the history
* refactor: move common to internal/

* feat: Config Live Reload
  • Loading branch information
Tohrusky authored Jul 14, 2024
1 parent cad38b7 commit 6ddf47f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 43 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/service/user/login.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package user

import (
"github.com/TensoRaws/NuxBT-Backend/internal/common/dao"
"github.com/TensoRaws/NuxBT-Backend/internal/middleware/jwt"
"github.com/TensoRaws/NuxBT-Backend/internal/service/common/dao"
"github.com/TensoRaws/NuxBT-Backend/module/util"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
Expand Down
6 changes: 3 additions & 3 deletions internal/service/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package user
import (
"strconv"

"github.com/TensoRaws/NuxBT-Backend/internal/service/common/dao"
"github.com/TensoRaws/NuxBT-Backend/internal/common/dao"
"github.com/TensoRaws/NuxBT-Backend/module/log"
"github.com/TensoRaws/NuxBT-Backend/module/util"
"github.com/gin-gonic/gin"
Expand All @@ -25,7 +25,7 @@ type ProfileResponse struct {
}

type ProfileOthersRequest struct {
UserId int `form:"user_id" binding:"required"`
UserId int32 `form:"user_id" binding:"required"`
}

// ProfileMe 获取用户自己的信息 (GET /profile/me)
Expand Down Expand Up @@ -87,7 +87,7 @@ func ProfileOthers(c *gin.Context) {
return
}
// 获取信息
user, err := dao.GetUserByID(int32(req.UserId))
user, err := dao.GetUserByID(req.UserId)
if err != nil {
util.AbortWithMsg(c, "User not found")
return
Expand Down
2 changes: 1 addition & 1 deletion internal/service/user/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"

"github.com/TensoRaws/NuxBT-Backend/dal/model"
"github.com/TensoRaws/NuxBT-Backend/internal/service/common/dao"
"github.com/TensoRaws/NuxBT-Backend/internal/common/dao"
"github.com/TensoRaws/NuxBT-Backend/module/config"
"github.com/TensoRaws/NuxBT-Backend/module/log"
"github.com/TensoRaws/NuxBT-Backend/module/util"
Expand Down
2 changes: 1 addition & 1 deletion internal/service/user/reset.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package user

import (
"github.com/TensoRaws/NuxBT-Backend/internal/service/common/dao"
"github.com/TensoRaws/NuxBT-Backend/internal/common/dao"
"github.com/TensoRaws/NuxBT-Backend/module/log"
"github.com/TensoRaws/NuxBT-Backend/module/util"
"github.com/gin-gonic/gin"
Expand Down
41 changes: 4 additions & 37 deletions module/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"errors"
"fmt"
"log"
"strings"
"sync"

Expand All @@ -16,16 +15,6 @@ var (
once sync.Once
)

var (
ServerConfig Server
JwtConfig Jwt
LogConfig Log
DBConfig DB
RedisConfig Redis
OSSConfig OSS
OSS_PREFIX string
)

func Init() {
once.Do(func() {
initialize()
Expand All @@ -49,7 +38,8 @@ func initialize() {

config.WatchConfig()
config.OnConfigChange(func(e fsnotify.Event) {
// 配置文件发生变更之后会调用的回调函数
// 配置文件发生变更之后,重新初始化配置
setConfig()
fmt.Println("Config file changed:", e.Name)
})

Expand Down Expand Up @@ -95,32 +85,9 @@ func initialize() {
}
}

err := config.UnmarshalKey("server", &ServerConfig)
if err != nil {
log.Fatalf("unable to decode into server struct, %v", err)
}
err = config.UnmarshalKey("jwt", &JwtConfig)
if err != nil {
log.Fatalf("unable to decode into jwt struct, %v", err)
}
err = config.UnmarshalKey("log", &LogConfig)
if err != nil {
log.Fatalf("unable to decode into log struct, %v", err)
}
err = config.UnmarshalKey("db", &DBConfig)
if err != nil {
log.Fatalf("unable to decode into db struct, %v", err)
}
err = config.UnmarshalKey("redis", &RedisConfig)
if err != nil {
log.Fatalf("unable to decode into redis struct, %v", err)
}
err = config.UnmarshalKey("oss", &OSSConfig)
if err != nil {
log.Fatalf("unable to decode into oss struct, %v", err)
}
// 初始化配置
setConfig()

OSS_PREFIX = GenerateOSSPrefix()
fmt.Printf("OSS TYPE: %v", config.GetString("oss.type"))
fmt.Printf(" OSS PREFIX: %v\n", OSS_PREFIX)
}
Expand Down
43 changes: 43 additions & 0 deletions module/config/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package config

import (
"log"
)

var (
ServerConfig Server
JwtConfig Jwt
LogConfig Log
DBConfig DB
RedisConfig Redis
OSSConfig OSS
OSS_PREFIX string
)

func setConfig() {
err := config.UnmarshalKey("server", &ServerConfig)
if err != nil {
log.Fatalf("unable to decode into server struct, %v", err)
}
err = config.UnmarshalKey("jwt", &JwtConfig)
if err != nil {
log.Fatalf("unable to decode into jwt struct, %v", err)
}
err = config.UnmarshalKey("log", &LogConfig)
if err != nil {
log.Fatalf("unable to decode into log struct, %v", err)
}
err = config.UnmarshalKey("db", &DBConfig)
if err != nil {
log.Fatalf("unable to decode into db struct, %v", err)
}
err = config.UnmarshalKey("redis", &RedisConfig)
if err != nil {
log.Fatalf("unable to decode into redis struct, %v", err)
}
err = config.UnmarshalKey("oss", &OSSConfig)
if err != nil {
log.Fatalf("unable to decode into oss struct, %v", err)
}
OSS_PREFIX = GenerateOSSPrefix()
}

0 comments on commit 6ddf47f

Please sign in to comment.