This commit is contained in:
2023-07-10 02:23:38 +08:00
parent 01b6451155
commit 66772cb659
6 changed files with 19 additions and 55 deletions

View File

@@ -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) {

View File

@@ -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)
}*/

View File

@@ -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

View File

@@ -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)})
}
}

View File

@@ -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
}