From 66772cb659d1e507163ddcaf35221f67b2dd4d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E6=98=BE=E4=BC=98?= <237809796@qq.com> Date: Mon, 10 Jul 2023 02:23:38 +0800 Subject: [PATCH] . --- Dockerfile | 3 +- internal/config/config.go | 3 ++ internal/monitor/monitor.go | 56 ++++++------------------------------- internal/zsub/msg-accept.go | 2 +- internal/zsub/zdb.go | 2 +- internal/zsub/ztimer.go | 8 +++--- 6 files changed, 19 insertions(+), 55 deletions(-) diff --git a/Dockerfile b/Dockerfile index 82c751e..c6eec69 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,7 +29,8 @@ FROM alpine:latest WORKDIR /opt/zhub # 从构建阶段复制可执行文件到当前阶段 -COPY --from=builder /opt/zhub . +COPY --from=builder /opt/zhub/zhub.sh . +COPY --from=builder /opt/zhub/app.ini . # 复制 app.ini 配置文件到容器中 COPY app.ini . diff --git a/internal/config/config.go b/internal/config/config.go index 4457431..15aa893 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -38,6 +38,9 @@ func ReadConfig() Config { viper.SetDefault("log.handlers", "console") viper.SetDefault("log.level", "info") viper.SetDefault("service.auth", true) + defer func() { + os.MkdirAll(conf.Data.Dir, os.ModeDir) + }() /*// 读取指定的配置文件 if !strings.EqualFold("", fileName) { diff --git a/internal/monitor/monitor.go b/internal/monitor/monitor.go index 2b37694..ae48cb2 100644 --- a/internal/monitor/monitor.go +++ b/internal/monitor/monitor.go @@ -39,55 +39,15 @@ func StartWatch() { zsub.Hub.Publish(topic, value) c.JSON(http.StatusOK, "+OK") }) + r.GET("/topic/delay", func(c *gin.Context) { + topic := c.Query("topic") + value := c.Query("value") + delay := c.Query("delay") + + zsub.Hub.Delay([]string{"delay", topic, value, delay}) + 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))) - http.HandleFunc("/info", info) - http.HandleFunc("/cleanup", cleanup) - http.HandleFunc("/retimer", retimer) - http.HandleFunc("/topic/publish", publish) - - watchAddr := zsub.Conf.Service.Watch - log.Println("zhub.watch = ", watchAddr) - http.ListenAndServe(watchAddr, nil)*/ } - -/*func publish(w http.ResponseWriter, r *http.Request) { - topic := r.FormValue("topic") - value := r.FormValue("value") - zsub.Hub.Publish(topic, value) - renderJson(w, "+ok") -} - -// retimer 重载定时调度 -func retimer(w http.ResponseWriter, _ *http.Request) { - zsub.Hub.ReloadTimer() - renderJson(w, "+reload timer ok") -} - -func cleanup(w http.ResponseWriter, _ *http.Request) { - zsub.Hub.Clearup() - renderJson(w, "+OK") -} - -func info(w http.ResponseWriter, _ *http.Request) { - info := zsub.Info() - renderJson(w, info) -} - -func renderJson(w http.ResponseWriter, d interface{}) { - var bytes []byte - - if str, ok := d.(string); ok { - bytes = []byte(str) - } else { - bytes, _ = json.Marshal(d) - w.Header().Set("content-type", "application/json; charset=utf-8;") - } - w.Write(bytes) -}*/ diff --git a/internal/zsub/msg-accept.go b/internal/zsub/msg-accept.go index 763ac9a..0ce4c6b 100644 --- a/internal/zsub/msg-accept.go +++ b/internal/zsub/msg-accept.go @@ -139,7 +139,7 @@ func handleMessage(v Message) { case "broadcast": Hub.broadcast(rcmd[1], rcmd[2]) case "delay": - Hub.delay(rcmd, c) + Hub.Delay(rcmd) case "timer": for _, name := range rcmd[1:] { Hub.timer([]string{"timer", name}, c) // append to timers diff --git a/internal/zsub/zdb.go b/internal/zsub/zdb.go index 1cff46d..002871d 100644 --- a/internal/zsub/zdb.go +++ b/internal/zsub/zdb.go @@ -125,7 +125,7 @@ func (s *ZSub) loadDelay() { if exectime < time.Now().Unix() { continue } - s.delay([]string{"delay", split[0], split[1], strconv.FormatInt((exectime-time.Now().Unix())*1000, 10)}, nil) + s.Delay([]string{"delay", split[0], split[1], strconv.FormatInt((exectime-time.Now().Unix())*1000, 10)}) } } diff --git a/internal/zsub/ztimer.go b/internal/zsub/ztimer.go index 534e36d..09500c0 100644 --- a/internal/zsub/ztimer.go +++ b/internal/zsub/ztimer.go @@ -28,8 +28,8 @@ type ZDelay struct { Timer *time.Timer } -// delay topic value 100 -> publish topic value -func (s *ZSub) delay(rcmd []string, c *ZConn) { +// Delay : delay topic value 100 -> publish topic value +func (s *ZSub) Delay(rcmd []string) { s.Lock() defer func() { s.Unlock() @@ -37,13 +37,13 @@ func (s *ZSub) delay(rcmd []string, c *ZConn) { s.delayup = true }() if len(rcmd) != 4 { - c.send("-Error: subscribe para number!") + // c.send("-Error: subscribe para number!") return } t, err := strconv.ParseInt(rcmd[3], 10, 64) if err != nil { - c.send("-Error: " + strings.Join(rcmd, " ")) + // c.send("-Error: " + strings.Join(rcmd, " ")) return }