From 9276777a948ccfebcdf7cc3fb241756a7a869fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E6=98=BE=E4=BC=98?= <237809796@qq.com> Date: Fri, 7 Jul 2023 14:39:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E4=BD=BF=E7=94=A8Gi?= =?UTF-8?q?n=E6=8F=90=E4=BE=9B=20http=20=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/monitor/monitor.go | 50 +++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/internal/monitor/monitor.go b/internal/monitor/monitor.go index 1b03166..2b37694 100644 --- a/internal/monitor/monitor.go +++ b/internal/monitor/monitor.go @@ -1,11 +1,8 @@ package monitor import ( - "encoding/json" - "log" + "github.com/gin-gonic/gin" "net/http" - "os" - "path" "zhub/internal/zsub" ) @@ -15,7 +12,38 @@ func init() { } func StartWatch() { - dir, _ := os.Getwd() + + r := gin.Default() + + r.Group("/users") + + r.GET("/", func(c *gin.Context) { + c.File("./public/index.html") + }) + + r.GET("/info", func(c *gin.Context) { + c.JSON(http.StatusOK, zsub.Info()) + }) + r.GET("/cleanup", func(c *gin.Context) { + zsub.Hub.Clearup() + c.JSON(http.StatusOK, "+OK") + }) + r.GET("/retimer", func(c *gin.Context) { + zsub.Hub.ReloadTimer() + c.JSON(http.StatusOK, "+reload timer ok") + }) + r.GET("/topic/publish", func(c *gin.Context) { + topic := c.Query("topic") + value := c.Query("value") + + zsub.Hub.Publish(topic, value) + c.JSON(http.StatusOK, "+OK") + }) + + watchAddr := zsub.Conf.Service.Watch + r.Run(watchAddr) + + /*dir, _ := os.Getwd() webDir := path.Join(dir, "/public") http.Handle("/", http.FileServer(http.Dir(webDir))) @@ -26,10 +54,10 @@ func StartWatch() { watchAddr := zsub.Conf.Service.Watch log.Println("zhub.watch = ", watchAddr) - http.ListenAndServe(watchAddr, nil) + http.ListenAndServe(watchAddr, nil)*/ } -func publish(w http.ResponseWriter, r *http.Request) { +/*func publish(w http.ResponseWriter, r *http.Request) { topic := r.FormValue("topic") value := r.FormValue("value") zsub.Hub.Publish(topic, value) @@ -37,17 +65,17 @@ func publish(w http.ResponseWriter, r *http.Request) { } // retimer 重载定时调度 -func retimer(w http.ResponseWriter, r *http.Request) { +func retimer(w http.ResponseWriter, _ *http.Request) { zsub.Hub.ReloadTimer() renderJson(w, "+reload timer ok") } -func cleanup(w http.ResponseWriter, r *http.Request) { +func cleanup(w http.ResponseWriter, _ *http.Request) { zsub.Hub.Clearup() renderJson(w, "+OK") } -func info(w http.ResponseWriter, r *http.Request) { +func info(w http.ResponseWriter, _ *http.Request) { info := zsub.Info() renderJson(w, info) } @@ -62,4 +90,4 @@ func renderJson(w http.ResponseWriter, d interface{}) { w.Header().Set("content-type", "application/json; charset=utf-8;") } w.Write(bytes) -} +}*/