diff --git a/core/store.go b/core/store.go index 235633c..ed578f4 100644 --- a/core/store.go +++ b/core/store.go @@ -1,6 +1,9 @@ package core import ( + "fmt" + "strconv" + "github.com/boltdb/bolt" ) @@ -25,13 +28,13 @@ func init() { } } -func (bucket Bucket) Set(key, value string) { +func (bucket Bucket) Set(key string, value interface{}) { db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(bucket)) if b == nil { b, _ = tx.CreateBucket([]byte(bucket)) } - b.Put([]byte(key), []byte(value)) + b.Put([]byte(key), []byte(fmt.Sprint(value))) return nil }) } @@ -56,6 +59,22 @@ func (bucket Bucket) Get(kv ...string) string { return value } +func (bucket Bucket) GetInt(key string, vs ...int) int { + var value int + if len(vs) != 0 { + value = vs[0] + } + db.View(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte(bucket)) + if b == nil { + return nil + } + value = Int(string(b.Get([]byte(key)))) + return nil + }) + return value +} + func (bucket Bucket) Foreach(f func(k, v []byte) error) { db.View(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(bucket)) @@ -63,3 +82,8 @@ func (bucket Bucket) Foreach(f func(k, v []byte) error) { return nil }) } + +var Int = func(s string) int { + i, _ := strconv.Atoi(s) + return i +} diff --git a/core/test.go b/core/test.go index 8c206ac..dd0c9bc 100644 --- a/core/test.go +++ b/core/test.go @@ -9,6 +9,7 @@ import ( func init() { AddCommand("", []Function{ { + Admin: true, Rules: []string{"set ? ? ?"}, Handle: func(s im.Sender) interface{} { b := Bucket(s.Get(0)) @@ -20,6 +21,7 @@ func init() { }, }, { + Admin: true, Rules: []string{"delete ? ?"}, Handle: func(s im.Sender) interface{} { b := Bucket(s.Get(0)) @@ -31,6 +33,7 @@ func init() { }, }, { + Admin: true, Rules: []string{"get ? ?"}, Handle: func(s im.Sender) interface{} { b := Bucket(s.Get(0)) diff --git a/develop/qinglong/.gitignore b/develop/qinglong/.gitignore deleted file mode 100644 index a539470..0000000 --- a/develop/qinglong/.gitignore +++ /dev/null @@ -1 +0,0 @@ -config.yaml \ No newline at end of file diff --git a/develop/qinglong/conf/demo_config.yaml b/develop/qinglong/conf/demo_config.yaml deleted file mode 100644 index 7d49627..0000000 --- a/develop/qinglong/conf/demo_config.yaml +++ /dev/null @@ -1,3 +0,0 @@ -host: http://127.0.0.1:5700 -client_id: -client_secret: \ No newline at end of file diff --git a/develop/qinglong/init.go b/develop/qinglong/init.go index dd37d88..a99773c 100644 --- a/develop/qinglong/init.go +++ b/develop/qinglong/init.go @@ -22,6 +22,7 @@ type Yaml struct { } var Config Yaml +var qinglong = core.NewBucket("qinglong") var token string var expiration int64 @@ -39,7 +40,9 @@ type Carrier struct { } func init() { - core.ReadYaml(core.ExecPath+"/develop/qinglong/conf/", &Config, "https://raw.githubusercontent.com/cdle/sillyGirl/main/develop/qinglong/conf/demo_config.yaml") + Config.Host = qinglong.Get("host") + Config.ClientID = qinglong.Get("client_id") + Config.ClientSecret = qinglong.Get("client_secret") if v := regexp.MustCompile(`^(https?://[\.\w]+:?\d*)`).FindStringSubmatch(Config.Host); len(v) == 2 { Config.Host = v[1] }