修改:代码风格
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
.idea
|
||||||
|
*.exe
|
||||||
|
*.iml
|
||||||
|
|
||||||
|
/tmp
|
||||||
@@ -40,7 +40,7 @@ type Client struct {
|
|||||||
|
|
||||||
type Lock struct {
|
type Lock struct {
|
||||||
Key string // lock Key
|
Key string // lock Key
|
||||||
Uuid string // lock Uuid
|
Value string // lock Value
|
||||||
flagChan chan int //
|
flagChan chan int //
|
||||||
// starttime uint32 // lock start time
|
// starttime uint32 // lock start time
|
||||||
// duration int // lock duration
|
// duration int // lock duration
|
||||||
@@ -90,10 +90,10 @@ func (c *Client) reconn() (err error) {
|
|||||||
go c.receive()
|
go c.receive()
|
||||||
|
|
||||||
// 重新订阅
|
// 重新订阅
|
||||||
for topic, _ := range c.subFun {
|
for topic := range c.subFun {
|
||||||
c.Subscribe(topic, nil)
|
c.Subscribe(topic, nil)
|
||||||
}
|
}
|
||||||
for topic, _ := range c.timerFun {
|
for topic := range c.timerFun {
|
||||||
c.Timer(topic, nil)
|
c.Timer(topic, nil)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@@ -129,7 +129,7 @@ func (c *Client) init() {
|
|||||||
go c.receive()
|
go c.receive()
|
||||||
}
|
}
|
||||||
|
|
||||||
// subscribe topic
|
// Subscribe subscribe topic
|
||||||
func (c *Client) Subscribe(topic string, fun func(v string)) {
|
func (c *Client) Subscribe(topic string, fun func(v string)) {
|
||||||
c.send("subscribe " + topic)
|
c.send("subscribe " + topic)
|
||||||
if fun != nil {
|
if fun != nil {
|
||||||
@@ -179,7 +179,7 @@ func (c *Client) Timer(topic string, fun func()) {
|
|||||||
c.send("timer", topic)
|
c.send("timer", topic)
|
||||||
}
|
}
|
||||||
|
|
||||||
// send cmd
|
// Cmd send cmd
|
||||||
func (c *Client) Cmd(cmd ...string) {
|
func (c *Client) Cmd(cmd ...string) {
|
||||||
if len(cmd) == 1 {
|
if len(cmd) == 1 {
|
||||||
c.send("cmd", cmd[0])
|
c.send("cmd", cmd[0])
|
||||||
@@ -197,31 +197,31 @@ func (c *Client) Close() {
|
|||||||
|
|
||||||
// Lock Key
|
// Lock Key
|
||||||
func (c *Client) Lock(key string, duration int) Lock {
|
func (c *Client) Lock(key string, duration int) Lock {
|
||||||
uuid := uuid.New()
|
v := uuid.New()
|
||||||
c.send("lock", key, uuid, strconv.Itoa(duration))
|
c.send("v", key, v, strconv.Itoa(duration))
|
||||||
|
|
||||||
lockChan := make(chan int, 2)
|
lockChan := make(chan int, 2)
|
||||||
go func() {
|
go func() {
|
||||||
c.wlock.Lock()
|
c.wlock.Lock()
|
||||||
defer c.wlock.Unlock()
|
defer c.wlock.Unlock()
|
||||||
c.lockFlag[uuid] = &Lock{
|
c.lockFlag[v] = &Lock{
|
||||||
Key: key,
|
Key: key,
|
||||||
Uuid: uuid,
|
Value: v,
|
||||||
flagChan: lockChan,
|
flagChan: lockChan,
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-lockChan:
|
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) {
|
func (c *Client) Unlock(l Lock) {
|
||||||
c.send("unlock", l.Key, l.Uuid)
|
c.send("unlock", l.Key, l.Value)
|
||||||
delete(c.lockFlag, l.Uuid)
|
delete(c.lockFlag, l.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------- rpc --------------------------------------
|
// -------------------------------------- rpc --------------------------------------
|
||||||
@@ -308,7 +308,7 @@ func (c Client) Rpc(topic string, message string, back func(res RpcResult)) {
|
|||||||
back(rpc.RpcResult)
|
back(rpc.RpcResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
// rpc subscribe
|
// RpcSubscribe rpc subscribe
|
||||||
func (c Client) RpcSubscribe(topic string, fun func(Rpc Rpc) RpcResult) {
|
func (c Client) RpcSubscribe(topic string, fun func(Rpc Rpc) RpcResult) {
|
||||||
c.Subscribe(topic, func(v string) {
|
c.Subscribe(topic, func(v string) {
|
||||||
rpc := Rpc{}
|
rpc := Rpc{}
|
||||||
@@ -368,27 +368,34 @@ func (c *Client) receive() {
|
|||||||
if len(v) == 0 {
|
if len(v) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch string(v[0]) {
|
switch v[0] {
|
||||||
case "+":
|
case '+':
|
||||||
if string(v) == "+ping" {
|
if string(v) == "+ping" {
|
||||||
c.send("+pong")
|
c.send("+pong")
|
||||||
}
|
}
|
||||||
case "-":
|
case '-':
|
||||||
log.Println("error:", string(v))
|
log.Println("error:", string(v))
|
||||||
case "*":
|
case '*':
|
||||||
|
if len(v) < 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
n, err := strconv.Atoi(string(v[1:]))
|
n, err := strconv.Atoi(string(v[1:]))
|
||||||
if err != nil {
|
if err != nil || n <= 0 {
|
||||||
log.Println(err)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var vs []string
|
var vs []string
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
line, _, err := r.ReadLine()
|
line, _, err := r.ReadLine()
|
||||||
if err != nil {
|
if err != nil || line == nil {
|
||||||
log.Println(err)
|
continue
|
||||||
|
}
|
||||||
|
if len(line) < 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
clen, err := strconv.Atoi(string(line[1:]))
|
||||||
|
if err != nil || clen <= 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
clen, _ := strconv.Atoi(string(line[1:]))
|
|
||||||
buf := make([]byte, clen)
|
buf := make([]byte, clen)
|
||||||
_, err = io.ReadFull(r, buf)
|
_, err = io.ReadFull(r, buf)
|
||||||
if err != nil {
|
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 --------------------------------------
|
// -------------------------------------- hm --------------------------------------
|
||||||
|
|
||||||
// ==============================================================================
|
// ==============================================================================
|
||||||
|
|
||||||
var reconnect = 0
|
var reconnect = 0
|
||||||
|
|
||||||
// client 命令行程序
|
// ClientRun client 命令行程序
|
||||||
func ClientRun(addr string) {
|
func ClientRun(addr string) {
|
||||||
conn, err := net.Dial("tcp", fmt.Sprintf("%s", addr))
|
conn, err := net.Dial("tcp", fmt.Sprintf("%s", addr))
|
||||||
|
|
||||||
|
|||||||
28
go.mod
28
go.mod
@@ -3,26 +3,46 @@ module zhub
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/gin-gonic/gin v1.9.1
|
||||||
github.com/go-basic/uuid v1.0.0
|
github.com/go-basic/uuid v1.0.0
|
||||||
github.com/go-sql-driver/mysql v1.5.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/robfig/cron v1.2.0
|
||||||
github.com/spf13/viper v1.15.0
|
github.com/spf13/viper v1.15.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
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/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/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/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/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/afero v1.9.3 // indirect
|
||||||
github.com/spf13/cast v1.5.0 // indirect
|
github.com/spf13/cast v1.5.0 // indirect
|
||||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
github.com/subosito/gotenv v1.4.2 // indirect
|
github.com/subosito/gotenv v1.4.2 // indirect
|
||||||
golang.org/x/sys v0.3.0 // indirect
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
golang.org/x/text v0.5.0 // 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/ini.v1 v1.67.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@@ -34,11 +33,6 @@ type Config struct {
|
|||||||
Auth map[string]string
|
Auth map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
config := ReadConfig()
|
|
||||||
|
|
||||||
fmt.Printf("%+v", config)
|
|
||||||
}
|
|
||||||
func ReadConfig() Config {
|
func ReadConfig() Config {
|
||||||
conf := Config{}
|
conf := Config{}
|
||||||
viper.SetDefault("log.handlers", "console")
|
viper.SetDefault("log.handlers", "console")
|
||||||
|
|||||||
@@ -3,9 +3,8 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>ZHub</title>
|
<title>ZHub</title>
|
||||||
<link rel="stylesheet" href="http://rep.1216.top/repo/zui-dist/css/zui.css">
|
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/zui/1.10.0/css/zui.min.css">
|
||||||
<link href="http://meta.1216.top/res/css/zui-theme.css" rel="stylesheet">
|
<style>
|
||||||
<style type="text/css">
|
|
||||||
body {
|
body {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
@@ -99,7 +98,7 @@
|
|||||||
|
|
||||||
<H6 class="center-block">
|
<H6 class="center-block">
|
||||||
|
|
||||||
<div>
|
<span>
|
||||||
<a href="#" title="ZHub">ZHub</a>
|
<a href="#" title="ZHub">ZHub</a>
|
||||||
<br>
|
<br>
|
||||||
<a href="index.html" title="开发者工具">Dev-Kit</a> |
|
<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://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://www.1216.top" target="_blank" title="redkale 实现论坛博客系统">Redbbs</a> |
|
||||||
<a href="https://gitee.com/tc608/jfly" target="_blank" title="jfinal 实现论坛博客系统">JFly</a>
|
<a href="https://gitee.com/tc608/jfly" target="_blank" title="jfinal 实现论坛博客系统">JFly</a>
|
||||||
</div>
|
</span>
|
||||||
<br>
|
<br>
|
||||||
© TCCN <span href="">京ICP备16065761号</span>
|
© ZHub <span>京ICP备16065761号</span>
|
||||||
<br>
|
<br>
|
||||||
<br>
|
<br>
|
||||||
</H6>
|
</H6>
|
||||||
|
|
||||||
<script src="http://meta.1216.top/res/zui/lib/jquery/jquery.js" defer></script>
|
<script src="//cdnjs.cloudflare.com/ajax/libs/zui/1.10.0/lib/jquery/jquery.js"></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/js/zui.min.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user