更新:1、依赖升级
2、module 名称地址
This commit is contained in:
@@ -2,13 +2,14 @@ package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"zhub/internal/config"
|
||||
|
||||
"gitea.1216.top/lxy/zhub/internal/config"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
|
||||
@@ -1,40 +1,40 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
type Log struct {
|
||||
Handlers string
|
||||
Level string
|
||||
File string
|
||||
Handlers string `ini:"handlers"`
|
||||
Level string `ini:"level"`
|
||||
File string `ini:"file"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Log Log
|
||||
Log Log `ini:"log"`
|
||||
Service struct {
|
||||
Watch string
|
||||
Addr string
|
||||
Auth bool
|
||||
}
|
||||
Watch string `ini:"watch"`
|
||||
Addr string `ini:"addr"`
|
||||
Auth bool `ini:"auth"`
|
||||
} `ini:"service"`
|
||||
Data struct {
|
||||
Dir string
|
||||
}
|
||||
Dir string `ini:"dir"`
|
||||
} `ini:"data"`
|
||||
Ztimer struct {
|
||||
Db struct {
|
||||
Addr string
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
Schema string
|
||||
Type string
|
||||
}
|
||||
}
|
||||
Auth map[string]string
|
||||
Addr string `ini:"addr"`
|
||||
User string `ini:"user"`
|
||||
Password string `ini:"password"`
|
||||
Database string `ini:"database"`
|
||||
Schema string `ini:"schema"`
|
||||
Type string `ini:"type"`
|
||||
} `ini:"db"`
|
||||
} `ini:"ztimer"`
|
||||
Auth map[string]string `ini:"auth"`
|
||||
}
|
||||
|
||||
func ReadConfig() Config {
|
||||
@@ -61,16 +61,16 @@ func ReadConfig() Config {
|
||||
}*/
|
||||
|
||||
// 尝试从 /etc/ 目录下查找 zhub.ini 配置文件
|
||||
viper.AddConfigPath("/etc/") // 添加 /etc/ 目录作为配置文件搜索路径
|
||||
/*viper.AddConfigPath("/etc/") // 添加 /etc/ 目录作为配置文件搜索路径
|
||||
viper.SetConfigName("zhub") // 指定配置文件名为 zhub
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
if err := viper.Unmarshal(&conf); err != nil {
|
||||
log.Fatalf("Failed to unmarshal config: %s", err.Error())
|
||||
}
|
||||
return conf
|
||||
}
|
||||
}*/
|
||||
// 如果 /etc/ 目录下未找到配置文件,则尝试从当前程序运行目录下查找 app.ini 配置文件
|
||||
dir, err := os.Getwd() // 获取程序运行目录
|
||||
/*dir, err := os.Getwd() // 获取程序运行目录
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get current directory: %s", err.Error())
|
||||
}
|
||||
@@ -90,9 +90,15 @@ func ReadConfig() Config {
|
||||
}
|
||||
|
||||
return conf
|
||||
} else {
|
||||
log.Fatalf("Config file not found: " + err.Error())
|
||||
}*/
|
||||
|
||||
load, err := ini.Load("app.ini")
|
||||
if err != nil {
|
||||
log.Panicf("Failed to load config: %s", err.Error())
|
||||
}
|
||||
// 如果在 /etc/ 目录和当前程序所在目录下均未找到配置文件,则报错
|
||||
log.Fatalf("Config file not found")
|
||||
load.MapTo(&conf)
|
||||
return conf
|
||||
}
|
||||
func InitLog(logConfig Log) {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
"zhub/internal/zbus"
|
||||
|
||||
"gitea.1216.top/lxy/zhub/internal/zbus"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var r = gin.Default()
|
||||
@@ -39,11 +41,20 @@ func StartWatch() {
|
||||
zbus.Bus.ReloadTimer()
|
||||
c.JSON(http.StatusOK, "+reload timer ok")
|
||||
})
|
||||
r.GET("/topic/publish", func(c *gin.Context) {
|
||||
topic := c.Query("topic")
|
||||
value := c.Query("value")
|
||||
r.POST("/message/send", func(c *gin.Context) {
|
||||
topic := c.PostForm("name") // c.Query("topic")
|
||||
value := c.PostForm("value") // c.Query("value")
|
||||
// _type := c.PostForm("type") // publish、broadcast
|
||||
|
||||
zbus.Bus.Publish(topic, value)
|
||||
log.Println(c.PostForm("type"), topic, value)
|
||||
switch c.PostForm("type") {
|
||||
case "broadcast":
|
||||
zbus.Bus.Broadcast(topic, value)
|
||||
case "publish":
|
||||
zbus.Bus.Publish(topic, value)
|
||||
default:
|
||||
|
||||
}
|
||||
c.JSON(http.StatusOK, "+OK")
|
||||
})
|
||||
r.GET("/topic/delay", func(c *gin.Context) {
|
||||
|
||||
@@ -6,7 +6,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"zhub/internal/auth"
|
||||
|
||||
"gitea.1216.top/lxy/zhub/internal/auth"
|
||||
)
|
||||
|
||||
var AuthManager *auth.PermissionManager
|
||||
@@ -156,7 +157,7 @@ func messageHandler(v Message) {
|
||||
}
|
||||
return
|
||||
case "broadcast":
|
||||
Bus.broadcast(rcmd[1], rcmd[2])
|
||||
Bus.Broadcast(rcmd[1], rcmd[2])
|
||||
case "delay":
|
||||
Bus.Delay(rcmd)
|
||||
default:
|
||||
|
||||
@@ -12,7 +12,8 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
"zhub/internal/config"
|
||||
|
||||
"gitea.1216.top/lxy/zhub/internal/config"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -387,9 +388,9 @@ func (s *ZBus) Publish(topic, msg string) {
|
||||
}
|
||||
|
||||
/*
|
||||
send broadcast message
|
||||
send Broadcast message
|
||||
*/
|
||||
func (s *ZBus) broadcast(topic, msg string) {
|
||||
func (s *ZBus) Broadcast(topic, msg string) {
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
if strings.EqualFold(topic, "lock") {
|
||||
@@ -422,7 +423,7 @@ func (s *ZBus) _lock(lock *Lock) {
|
||||
lock.start = time.Now().Unix()
|
||||
locks = append(locks, lock)
|
||||
s.locks[lock.key] = locks
|
||||
s.broadcast("lock", lock.uuid)
|
||||
s.Broadcast("lock", lock.uuid)
|
||||
|
||||
// 设置时间到解锁
|
||||
locks[0].timer = time.NewTimer(time.Duration(locks[0].duration) * time.Second)
|
||||
@@ -435,7 +436,7 @@ func (s *ZBus) _lock(lock *Lock) {
|
||||
} else {
|
||||
switch lock.cmd {
|
||||
case "trylock": // send trylock fail message
|
||||
s.broadcast("trylock", lock.uuid)
|
||||
s.Broadcast("trylock", lock.uuid)
|
||||
case "lock":
|
||||
s.locks[lock.key] = append(locks, lock)
|
||||
}
|
||||
@@ -452,7 +453,7 @@ func (s *ZBus) _unlock(l Lock) {
|
||||
s.locks[l.key] = locks
|
||||
}
|
||||
if len(s.locks[l.key]) > 0 { // next lock
|
||||
s.broadcast("lock", s.locks[l.key][0].uuid)
|
||||
s.Broadcast("lock", s.locks[l.key][0].uuid)
|
||||
s.locks[l.key][0].start = time.Now().Unix()
|
||||
s.locks[l.key][0].timer = time.NewTimer(time.Duration(s.locks[l.key][0].duration) * time.Second)
|
||||
go func() {
|
||||
|
||||
Reference in New Issue
Block a user