修改:使用Gin提供 http 服务

This commit is contained in:
2023-07-07 14:39:20 +08:00
parent 913a531f30
commit 9276777a94

View File

@@ -1,11 +1,8 @@
package monitor package monitor
import ( import (
"encoding/json" "github.com/gin-gonic/gin"
"log"
"net/http" "net/http"
"os"
"path"
"zhub/internal/zsub" "zhub/internal/zsub"
) )
@@ -15,7 +12,38 @@ func init() {
} }
func StartWatch() { 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") webDir := path.Join(dir, "/public")
http.Handle("/", http.FileServer(http.Dir(webDir))) http.Handle("/", http.FileServer(http.Dir(webDir)))
@@ -26,10 +54,10 @@ func StartWatch() {
watchAddr := zsub.Conf.Service.Watch watchAddr := zsub.Conf.Service.Watch
log.Println("zhub.watch = ", watchAddr) 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") topic := r.FormValue("topic")
value := r.FormValue("value") value := r.FormValue("value")
zsub.Hub.Publish(topic, value) zsub.Hub.Publish(topic, value)
@@ -37,17 +65,17 @@ func publish(w http.ResponseWriter, r *http.Request) {
} }
// retimer 重载定时调度 // retimer 重载定时调度
func retimer(w http.ResponseWriter, r *http.Request) { func retimer(w http.ResponseWriter, _ *http.Request) {
zsub.Hub.ReloadTimer() zsub.Hub.ReloadTimer()
renderJson(w, "+reload timer ok") renderJson(w, "+reload timer ok")
} }
func cleanup(w http.ResponseWriter, r *http.Request) { func cleanup(w http.ResponseWriter, _ *http.Request) {
zsub.Hub.Clearup() zsub.Hub.Clearup()
renderJson(w, "+OK") renderJson(w, "+OK")
} }
func info(w http.ResponseWriter, r *http.Request) { func info(w http.ResponseWriter, _ *http.Request) {
info := zsub.Info() info := zsub.Info()
renderJson(w, 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.Header().Set("content-type", "application/json; charset=utf-8;")
} }
w.Write(bytes) w.Write(bytes)
} }*/