This commit is contained in:
aozhiwei 2024-02-11 11:28:49 +08:00
parent b6f20e6837
commit a12d6d4b9e
2 changed files with 49 additions and 0 deletions

46
app.go
View File

@ -8,6 +8,7 @@ import (
"sync"
"sync/atomic"
"time"
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
@ -35,6 +36,7 @@ type App interface {
RegisterOrmDb(name string, host string, port int32,
user string, passwd string, dataBase string)
GetOrmDb(string) *gorm.DB
RegisterCaHandle(c string, a string, handle GinHandlerFunc)
}
type UserApp interface {
@ -72,6 +74,8 @@ type app struct {
ginEngine *gin.Engine
ormDbLock sync.Mutex
ormDbHash map[string]*gorm.DB
caHandlersMutex sync.RWMutex
caHandlers map[string]GinHandlerFunc
}
func (this *app) init(userApp UserApp) {
@ -118,8 +122,33 @@ func (this *app) init(userApp UserApp) {
go this.goLoopTimer()
this.ginEngine = gin.New()
this.ginEngine.Use(gin.Recovery())
this.caHandlers = make(map[string]GinHandlerFunc)
this.userApp.Init()
go this.ginEngine.Run(fmt.Sprintf(":%d", this.userApp.GetHttpListenPort()))
this.RegisterCaHandle("Ops", "selfChecking", func (c *gin.Context) {
var msg struct {
ErrCode int32 `json:"errcode"`
ErrMsg string `json:"errmsg"`
}
msg.ErrCode = 0
msg.ErrMsg = ""
c.JSON(http.StatusOK, msg)
})
this.ginEngine.GET("/webapp/index.php", func (c *gin.Context) {
handleName := c.DefaultQuery("c", "") + "$" + c.DefaultQuery("a", "")
handle := this.getCaHandle(handleName)
if handle == nil {
var msg struct {
ErrCode int32 `json:"errcode"`
ErrMsg string `json:"errmsg"`
}
msg.ErrCode = 404
msg.ErrMsg = "not fond"
c.JSON(http.StatusOK, msg)
return
}
handle(c)
})
}
func (this *app) unInit() {
@ -326,6 +355,23 @@ func (this *app) GetOrmDb(name string) *gorm.DB {
}
}
func (this *app) RegisterCaHandle(c string, a string, handle GinHandlerFunc) {
this.caHandlersMutex.Lock()
defer this.caHandlersMutex.Unlock()
handleName := c + "$" + a
this.caHandlers[handleName] = handle
}
func (this *app) getCaHandle(handleName string) GinHandlerFunc {
this.caHandlersMutex.Lock()
defer this.caHandlersMutex.Unlock()
if handle, ok := this.caHandlers[handleName]; ok {
return handle
} else {
return nil
}
}
func parseArgs() (int, int) {
args := os.Args[1:]
if len(args) <= 0 {

View File

@ -2,6 +2,8 @@ package f5
import (
"q5"
"github.com/gin-gonic/gin"
)
const (
@ -28,6 +30,7 @@ type Pagination struct {
}
type HandlerFunc func(*Context)
type GinHandlerFunc func(*gin.Context)
type QueryResultCb func(error, *DataSet)
type QueryOneCb func(error, *DataSet)