From a12d6d4b9e133a9f3b4e3e7cd83d035b4064fccd Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 11 Feb 2024 11:28:49 +0800 Subject: [PATCH] 1 --- app.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ types.go | 3 +++ 2 files changed, 49 insertions(+) diff --git a/app.go b/app.go index 2155839..7958a85 100644 --- a/app.go +++ b/app.go @@ -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 { diff --git a/types.go b/types.go index 2a2ff31..83cedbc 100644 --- a/types.go +++ b/types.go @@ -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)