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
import (
"bytes"
"dashboard/dao/sqldb"
"dashboard/models"
"dashboard/services/unis"
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"strconv"
"github.com/gin-gonic/gin"
)
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
if err := c.ShouldBindJSON(&config); err != nil {
return err
}
// serverId := c.GetHeader("serverId")
channelStr := c.GetHeader("channel")
channel, err := strconv.Atoi(channelStr)
if err != nil {
return err
}
fmt.Println("url", c.Request.URL.String())
oldConf, err := sqldb.ResFulDataGet(c.Request.Method, c.Request.URL.String(), channel)
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")
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "Server station config ok",
})
return nil
}

View File

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

View File

@ -6,7 +6,7 @@ const rpcNamePrefix = "UnisRpcService"
type UnisRpcMethod string
func (m UnisRpcMethod)String()string{
func (m UnisRpcMethod)Methord()string{
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 {
return func(c *gin.Context) {
c.Set(models.GinContxtLog, log)
c.Set(models.GinContextLog, log)
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()
return nil
@ -188,15 +200,7 @@ func GinStoreRequest(c *gin.Context) (err error) {
}()
log, _ := utils.GetLogFromContext(c)
channel, _err := strconv.Atoi(c.GetHeader("channel"))
if _err != nil {
err = &models.BaseError{
Code: http.StatusServiceUnavailable,
Msg: "Channel Id error",
}
return
}
channel := c.GetInt(models.GinContextChannel)
oldConf, _err := sqldb.ResFulDataGet(c.Request.Method, c.Request.URL.String(), channel)
if _err != nil {

View File

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

View File

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