diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d95363 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.idea +*.exe +*.iml + +/tmp diff --git a/cmd/client.go b/cmd/client.go index 7eda4c7..4788d4c 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -40,7 +40,7 @@ type Client struct { type Lock struct { Key string // lock Key - Uuid string // lock Uuid + Value string // lock Value flagChan chan int // // starttime uint32 // lock start time // duration int // lock duration @@ -90,10 +90,10 @@ func (c *Client) reconn() (err error) { go c.receive() // 重新订阅 - for topic, _ := range c.subFun { + for topic := range c.subFun { c.Subscribe(topic, nil) } - for topic, _ := range c.timerFun { + for topic := range c.timerFun { c.Timer(topic, nil) } break @@ -129,7 +129,7 @@ func (c *Client) init() { go c.receive() } -// subscribe topic +// Subscribe subscribe topic func (c *Client) Subscribe(topic string, fun func(v string)) { c.send("subscribe " + topic) if fun != nil { @@ -179,7 +179,7 @@ func (c *Client) Timer(topic string, fun func()) { c.send("timer", topic) } -// send cmd +// Cmd send cmd func (c *Client) Cmd(cmd ...string) { if len(cmd) == 1 { c.send("cmd", cmd[0]) @@ -197,31 +197,31 @@ func (c *Client) Close() { // Lock Key func (c *Client) Lock(key string, duration int) Lock { - uuid := uuid.New() - c.send("lock", key, uuid, strconv.Itoa(duration)) + v := uuid.New() + c.send("v", key, v, strconv.Itoa(duration)) lockChan := make(chan int, 2) go func() { c.wlock.Lock() defer c.wlock.Unlock() - c.lockFlag[uuid] = &Lock{ + c.lockFlag[v] = &Lock{ Key: key, - Uuid: uuid, + Value: v, flagChan: lockChan, } }() select { case <-lockChan: - log.Println("lock-ok", time.Now().UnixNano()/1e6, uuid) + log.Println("v-ok", time.Now().UnixNano()/1e6, v) } - return Lock{Key: key, Uuid: uuid} + return Lock{Key: key, Value: v} } func (c *Client) Unlock(l Lock) { - c.send("unlock", l.Key, l.Uuid) - delete(c.lockFlag, l.Uuid) + c.send("unlock", l.Key, l.Value) + delete(c.lockFlag, l.Value) } // -------------------------------------- rpc -------------------------------------- @@ -308,7 +308,7 @@ func (c Client) Rpc(topic string, message string, back func(res RpcResult)) { back(rpc.RpcResult) } -// rpc subscribe +// RpcSubscribe rpc subscribe func (c Client) RpcSubscribe(topic string, fun func(Rpc Rpc) RpcResult) { c.Subscribe(topic, func(v string) { rpc := Rpc{} @@ -368,27 +368,34 @@ func (c *Client) receive() { if len(v) == 0 { continue } - switch string(v[0]) { - case "+": + switch v[0] { + case '+': if string(v) == "+ping" { c.send("+pong") } - case "-": + case '-': log.Println("error:", string(v)) - case "*": + case '*': + if len(v) < 2 { + continue + } n, err := strconv.Atoi(string(v[1:])) - if err != nil { - log.Println(err) + if err != nil || n <= 0 { continue } var vs []string for i := 0; i < n; i++ { line, _, err := r.ReadLine() - if err != nil { - log.Println(err) + if err != nil || line == nil { + continue + } + if len(line) < 2 { + continue + } + clen, err := strconv.Atoi(string(line[1:])) + if err != nil || clen <= 0 { continue } - clen, _ := strconv.Atoi(string(line[1:])) buf := make([]byte, clen) _, err = io.ReadFull(r, buf) if err != nil { @@ -417,33 +424,13 @@ func (c *Client) receive() { } } -// -------------------------------------- k-v -------------------------------------- -/* -*3 -$3 -set -$n -x -$m -xx -*/ -func (c *Client) set(key string, value interface{}) error { - - return nil -} - -func (c *Client) get(key string) string { - - return "" -} - // -------------------------------------- hm -------------------------------------- // ============================================================================== var reconnect = 0 -// client 命令行程序 +// ClientRun client 命令行程序 func ClientRun(addr string) { conn, err := net.Dial("tcp", fmt.Sprintf("%s", addr)) diff --git a/go.mod b/go.mod index ae7832e..930a18b 100644 --- a/go.mod +++ b/go.mod @@ -3,26 +3,46 @@ module zhub go 1.18 require ( + github.com/gin-gonic/gin v1.9.1 github.com/go-basic/uuid v1.0.0 github.com/go-sql-driver/mysql v1.5.0 - github.com/mitchellh/go-homedir v1.1.0 github.com/robfig/cron v1.2.0 github.com/spf13/viper v1.15.0 ) require ( + github.com/bytedance/sonic v1.9.1 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.14.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/leodido/go-urn v1.2.4 // indirect github.com/magiconair/properties v1.8.7 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect - github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/spf13/afero v1.9.3 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.4.2 // indirect - golang.org/x/sys v0.3.0 // indirect - golang.org/x/text v0.5.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/arch v0.3.0 // indirect + golang.org/x/crypto v0.9.0 // indirect + golang.org/x/net v0.10.0 // indirect + golang.org/x/sys v0.8.0 // indirect + golang.org/x/text v0.9.0 // indirect + google.golang.org/protobuf v1.30.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/internal/config/config.go b/internal/config/config.go index 185d99b..4457431 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "github.com/spf13/viper" "log" "os" @@ -34,11 +33,6 @@ type Config struct { Auth map[string]string } -func main() { - config := ReadConfig() - - fmt.Printf("%+v", config) -} func ReadConfig() Config { conf := Config{} viper.SetDefault("log.handlers", "console") diff --git a/public/index.html b/public/index.html index fb7820a..b131d15 100644 --- a/public/index.html +++ b/public/index.html @@ -3,9 +3,8 @@