1、完善上下文类型,增加serverid和channel到上下文中。

This commit is contained in:
redhat 2025-05-23 19:39:46 +08:00
parent 7a74103aba
commit 38fb2a6df8
6 changed files with 25 additions and 60 deletions

View File

@ -1,68 +1,27 @@
package unis package unis
import ( import (
"bytes"
"dashboard/dao/sqldb"
"dashboard/models" "dashboard/models"
"dashboard/services/unis" "dashboard/services/unis"
"encoding/json"
"fmt"
"io"
"net/http" "net/http"
"reflect"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func StationConfig(c *gin.Context) error { func StationConfig(c *gin.Context) error {
bodyBytes, err := io.ReadAll(c.Request.Body)
if err != nil {
return err
}
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
var config models.StationConfigParams var config models.StationConfigParams
if err := c.ShouldBindJSON(&config); err != nil { if err := c.ShouldBindJSON(&config); err != nil {
return err return err
} }
// serverId := c.GetHeader("serverId") if err := unis.StationConfig(&config); err != nil {
channelStr := c.GetHeader("channel")
channel, err := strconv.Atoi(channelStr)
if err != nil {
return err return err
} }
fmt.Println("url", c.Request.URL.String()) c.JSON(http.StatusOK, gin.H{
"code": 0,
oldConf, err := sqldb.ResFulDataGet(c.Request.Method, c.Request.URL.String(), channel) "msg": "Server station config ok",
if err != nil { })
return err
}
var oldconfig models.StationConfigParams
if oldConf != "" {
if err := json.Unmarshal([]byte(oldConf), &oldconfig); err != nil {
return err
}
}
oldconfig.StationConfig.ConfigType = config.StationConfig.ConfigType
if !reflect.DeepEqual(config, oldconfig) {
fmt.Println("config is not equal")
if err := sqldb.ResFulDataStore(c.Request.Method, c.Request.URL.RawPath, string(bodyBytes), channel); err != nil {
return err
}
}
if err:=unis.StationConfig(&config);err!=nil{
return err
}
c.JSON(http.StatusOK, "ok")
return nil return nil
} }

View File

@ -6,7 +6,9 @@ import (
) )
const ( const (
GinContxtLog = "log" GinContextLog = "log"
GinContextChannel = "channel"
GinContextServerId = "serverid"
) )
const ( const (

View File

@ -6,7 +6,7 @@ const rpcNamePrefix = "UnisRpcService"
type UnisRpcMethod string type UnisRpcMethod string
func (m UnisRpcMethod)String()string{ func (m UnisRpcMethod)Methord()string{
return fmt.Sprintf("%s.%s",rpcNamePrefix,string(m)) return fmt.Sprintf("%s.%s",rpcNamePrefix,string(m))
} }

View File

@ -96,7 +96,7 @@ func GinRecovery(log *logger.Logger, stack bool) gin.HandlerFunc {
func GinLog(log *logger.Logger) gin.HandlerFunc { func GinLog(log *logger.Logger) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
c.Set(models.GinContxtLog, log) c.Set(models.GinContextLog, log)
c.Next() c.Next()
} }
} }
@ -172,6 +172,18 @@ func GinCheckServerId(c *gin.Context) error {
} }
} }
channel, err := strconv.Atoi(c.GetHeader("channel"))
if err != nil {
c.Abort()
return &models.BaseError{
Code: http.StatusServiceUnavailable,
Msg: "Channel Id error",
}
}
c.Set(models.GinContextServerId, serverId)
c.Set(models.GinContextChannel, channel)
c.Next() c.Next()
return nil return nil
@ -188,15 +200,7 @@ func GinStoreRequest(c *gin.Context) (err error) {
}() }()
log, _ := utils.GetLogFromContext(c) log, _ := utils.GetLogFromContext(c)
channel := c.GetInt(models.GinContextChannel)
channel, _err := strconv.Atoi(c.GetHeader("channel"))
if _err != nil {
err = &models.BaseError{
Code: http.StatusServiceUnavailable,
Msg: "Channel Id error",
}
return
}
oldConf, _err := sqldb.ResFulDataGet(c.Request.Method, c.Request.URL.String(), channel) oldConf, _err := sqldb.ResFulDataGet(c.Request.Method, c.Request.URL.String(), channel)
if _err != nil { if _err != nil {

View File

@ -10,7 +10,7 @@ func GetConfig(req models.UnisRpcRequest) (*models.UnisRpcResponse, error) {
for key, value := range rpcMapClients { for key, value := range rpcMapClients {
fmt.Println(key) fmt.Println(key)
value.Call(models.UnisStationConfig.String(), req, out) value.Call(models.UnisStationConfig.Methord(), req, out)
} }
return out, nil return out, nil

View File

@ -8,7 +8,7 @@ import (
) )
func GetLogFromContext(c *gin.Context) (*logger.Logger, error) { func GetLogFromContext(c *gin.Context) (*logger.Logger, error) {
res, ok := c.Get(models.GinContxtLog) res, ok := c.Get(models.GinContextLog)
if !ok { if !ok {
return nil, models.ErrorInvalidData return nil, models.ErrorInvalidData
} }