gameapi host & apigate host

This commit is contained in:
yangduo 2024-08-29 19:11:59 +08:00
parent 00bed4f1d8
commit 34a400e7ed
6 changed files with 319 additions and 0 deletions

View File

@ -14,4 +14,5 @@ type ApiGroup struct {
BlockPlayerApi
WhiteListApi
ActiveCodeApi
WorkerToolApi
}

View File

@ -0,0 +1,263 @@
package system
import (
"f5"
"fmt"
"main/constant"
"main/model/system"
"net"
"net/http"
"q5"
"time"
"github.com/gin-gonic/gin"
)
type WorkerToolApi struct {
}
func (bpa *WorkerToolApi) ListGameApi(c *gin.Context) {
req := struct {
PageDto system.PageDto `json:"page_dto"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
sql := "SELECT * FROM t_internal_gameapi_host WHERE 1=1"
params := []string{}
result := []*system.GameApiHostItem{}
f5.GetGoStyleDb().PageQuery(
constant.CONF_DB,
q5.ToInt32(req.PageDto.PageSize),
q5.ToInt32(req.PageDto.Page),
sql,
params,
f5.GetDbFilter().Comp([]f5.DbQueryFilter{}...),
"",
func(err error, pg *f5.Pagination) {
if err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
for pg.Rows.Next() {
p := new(system.GameApiHostItem)
f5.UnmarshalModel(pg.Rows, p)
q5.AppendSlice(&result, p)
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "获取成功",
"data": result,
"total": pg.Total,
"total_page": pg.TotalPages,
"cur_page": pg.CurrentPage,
})
})
}
func (bpa *WorkerToolApi) AddGameApi(c *gin.Context) {
req := struct {
Host string `binding:"required" json:"gameapi_host"`
Port int32 `binding:"required" json:"gameapi_port"`
Enable int32 `json:"enable"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
if !bpa.testHostport(c, req.Host, req.Port) {
return
}
info := new(system.GameApiHostItem)
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
var count int64 = 0
db := f5.GetApp().GetOrmDb(constant.CONF_DB).Table(info.TableName()).Where("gameapi_host = ?", req.Host).Where("gameapi_port = ?", req.Port)
if err := db.Count(&count).Error; err == nil && count > 0 {
f5.RspErr2(c, 1, "数据存在")
return
}
info.Host = req.Host
info.Port = req.Port
info.Enable = req.Enable
info.CreateTime = nowDaySeconds
info.ModifyTime = nowDaySeconds
if err := f5.GetApp().GetOrmDb(constant.CONF_DB).Create(info).Error; err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
f5.RspErr2(c, 0, "添加成功")
}
func (bpa *WorkerToolApi) EditGameApi(c *gin.Context) {
req := struct {
Host string `binding:"required" json:"gameapi_host"`
Port int32 `binding:"required" json:"gameapi_port"`
Enable int32 `json:"enable"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
if !bpa.testHostport(c, req.Host, req.Port) {
return
}
item := new(system.GameApiHostItem)
db := f5.GetApp().GetOrmDb(constant.CONF_DB).Table(item.TableName())
if err := db.Take(item, "gameapi_host = ? AND gameapi_port = ?", req.Host, req.Port).Error; err != nil {
if !f5.IsOrmErrRecordNotFound(err) {
f5.RspErr2(c, 500, "sever internal error:"+err.Error())
return
} else {
f5.RspErr2(c, 2, "无法查到记录")
return
}
}
if item.Enable != req.Enable {
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
item.Enable = req.Enable
item.ModifyTime = nowDaySeconds
if err := db.Where("gameapi_host = ?", req.Host).Where("gameapi_port = ?", req.Port).Save(item).Error; err != nil {
f5.RspErr2(c, 500, "sever internal error:"+err.Error())
return
}
}
f5.RspErr2(c, 0, "")
}
func (bpa *WorkerToolApi) ListApiGate(c *gin.Context) {
req := struct {
PageDto system.PageDto `json:"page_dto"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
sql := "SELECT * FROM t_apigate_host WHERE 1=1"
params := []string{}
result := []*system.ApiGateHostItem{}
f5.GetGoStyleDb().PageQuery(
constant.CONF_DB,
q5.ToInt32(req.PageDto.PageSize),
q5.ToInt32(req.PageDto.Page),
sql,
params,
f5.GetDbFilter().Comp([]f5.DbQueryFilter{}...),
"",
func(err error, pg *f5.Pagination) {
if err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
for pg.Rows.Next() {
p := new(system.ApiGateHostItem)
f5.UnmarshalModel(pg.Rows, p)
q5.AppendSlice(&result, p)
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "获取成功",
"data": result,
"total": pg.Total,
"total_page": pg.TotalPages,
"cur_page": pg.CurrentPage,
})
})
}
func (bpa *WorkerToolApi) AddApiGate(c *gin.Context) {
req := struct {
Host string `binding:"required" json:"apigate_host"`
Port int32 `binding:"required" json:"apigate_port"`
Enable int32 `json:"enable"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
if !bpa.testHostport(c, req.Host, req.Port) {
return
}
info := new(system.ApiGateHostItem)
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
var count int64 = 0
db := f5.GetApp().GetOrmDb(constant.CONF_DB).Table(info.TableName()).Where("apigate_host = ?", req.Host).Where("apigate_port = ?", req.Port)
if err := db.Count(&count).Error; err == nil && count > 0 {
f5.RspErr2(c, 1, "数据存在")
return
}
info.Host = req.Host
info.Port = req.Port
info.Enable = req.Enable
info.CreateTime = nowDaySeconds
info.ModifyTime = nowDaySeconds
if err := f5.GetApp().GetOrmDb(constant.CONF_DB).Create(info).Error; err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
f5.RspErr2(c, 0, "添加成功")
}
func (bpa *WorkerToolApi) EditApiGate(c *gin.Context) {
req := struct {
Host string `binding:"required" json:"apigate_host"`
Port int32 `binding:"required" json:"apigate_port"`
Enable int32 `json:"enable"`
}{}
if err := c.ShouldBindJSON(&req); err != nil {
f5.RspErr2(c, 1, err.Error())
return
}
if !bpa.testHostport(c, req.Host, req.Port) {
return
}
item := new(system.ApiGateHostItem)
db := f5.GetApp().GetOrmDb(constant.CONF_DB).Table(item.TableName())
if err := db.Take(item, "apigate_host = ? AND apigate_port = ?", req.Host, req.Port).Error; err != nil {
if !f5.IsOrmErrRecordNotFound(err) {
f5.RspErr2(c, 500, "sever internal error:"+err.Error())
return
} else {
f5.RspErr2(c, 2, "无法查到记录")
return
}
}
if item.Enable != req.Enable {
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
item.Enable = req.Enable
item.ModifyTime = nowDaySeconds
if err := db.Where("apigate_host = ?", req.Host).Where("apigate_port = ?", req.Port).Save(item).Error; err != nil {
f5.RspErr2(c, 500, "sever internal error:"+err.Error())
return
}
}
f5.RspErr2(c, 0, "")
}
func (bpa *WorkerToolApi) testHostport(c *gin.Context, host string, port int32) bool {
addrAndPort := fmt.Sprintf("%s:%d", host, port)
conn, err := net.DialTimeout("tcp", addrAndPort, time.Second)
if err != nil {
f5.RspErr2(c, 2, "host port 连不上")
return false
} else {
conn.Close()
return true
}
}

View File

@ -0,0 +1,25 @@
package system
type GameApiHostItem struct {
Host string `gorm:"column:gameapi_host" json:"gameapi_host"`
Port int32 `gorm:"column:gameapi_port" json:"gameapi_port"`
Enable int32 `gorm:"column:enable" json:"enable"`
CreateTime int32 `gorm:"column:createtime" json:"-"`
ModifyTime int32 `gorm:"column:modifytime" json:"-"`
}
func (GameApiHostItem) TableName() string {
return "t_internal_gameapi_host"
}
type ApiGateHostItem struct {
Host string `gorm:"column:apigate_host" json:"apigate_host"`
Port int32 `gorm:"column:apigate_port" json:"apigate_port"`
Enable int32 `gorm:"column:enable" json:"enable"`
CreateTime int32 `gorm:"column:createtime" json:"-"`
ModifyTime int32 `gorm:"column:modifytime" json:"-"`
}
func (ApiGateHostItem) TableName() string {
return "t_apigate_host"
}

View File

@ -30,6 +30,7 @@ func (this *routerMgr) Init() {
this.system.InitBlockPlayerRouter(priGroup)
this.system.InitWhiteListRouter(priGroup)
this.system.InitActiveCodeRouter(priGroup)
this.system.InitWorkerToolRouter(priGroup)
f5.GetSysLog().Info("routerMgr.init")
}

View File

@ -14,4 +14,5 @@ type RouterGroup struct {
BlockPlayerRoute
WhiteListRoute
ActiveCodeRoute
WorkerToolRoute
}

View File

@ -0,0 +1,28 @@
package system
import (
v1 "main/api/v1"
"main/middleware"
"github.com/gin-gonic/gin"
)
type WorkerToolRoute struct {
}
func (this *WorkerToolRoute) InitWorkerToolRouter(priRouter *gin.RouterGroup) {
group := priRouter.Group("gameapihost")
api := v1.ApiGroupApp.SystemApiGroup.WorkerToolApi
{
group.POST("add", middleware.Permission("api/v1/gameapihost/add", api.AddGameApi))
group.POST("edit", middleware.Permission("api/v1/gameapihost/edit", api.EditGameApi))
group.POST("list", middleware.Permission("api/v1/gameapihost/list", api.ListGameApi))
}
{
supergroup := priRouter.Group("apigate")
supergroup.POST("add", middleware.Permission("api/v1/apigate/add", api.AddApiGate))
supergroup.POST("list", middleware.Permission("api/v1/apigate/list", api.ListApiGate))
supergroup.POST("edit", middleware.Permission("api/v1/apigate/edit", api.EditApiGate))
}
}