修改:代码风格

This commit is contained in:
2023-07-07 14:45:19 +08:00
parent 9276777a94
commit 1679f384ee
5 changed files with 68 additions and 63 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
.idea
*.exe
*.iml
/tmp

View File

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

28
go.mod
View File

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

View File

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

View File

@@ -3,9 +3,8 @@
<head>
<meta charset="UTF-8">
<title>ZHub</title>
<link rel="stylesheet" href="http://rep.1216.top/repo/zui-dist/css/zui.css">
<link href="http://meta.1216.top/res/css/zui-theme.css" rel="stylesheet">
<style type="text/css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/zui/1.10.0/css/zui.min.css">
<style>
body {
text-align: center;
}
@@ -99,7 +98,7 @@
<H6 class="center-block">
<div>
<span>
<a href="#" title="ZHub">ZHub</a>
<br>
<a href="index.html" title="开发者工具">Dev-Kit</a> |
@@ -108,14 +107,14 @@
<a href="https://gitee.com/tc608/redtimer" target="_blank" title="徒手编写,精简时间调度">Red-timer</a> |
<a href="https://www.1216.top" target="_blank" title="redkale 实现论坛博客系统">Redbbs</a> |
<a href="https://gitee.com/tc608/jfly" target="_blank" title="jfinal 实现论坛博客系统">JFly</a>
</div>
</span>
<br>
© TCCN <span href="">京ICP备16065761号</span>
© ZHub <span>京ICP备16065761号</span>
<br>
<br>
</H6>
<script src="http://meta.1216.top/res/zui/lib/jquery/jquery.js" defer></script>
<script src="http://meta.1216.top/res/zui/js/zui.js" defer></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/zui/1.10.0/lib/jquery/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/zui/1.10.0/js/zui.min.js"></script>
</body>
</html>