This commit is contained in:
cdle
2021-09-15 19:07:08 +08:00
parent aa3486c7e9
commit 3738ec952b
5 changed files with 33 additions and 7 deletions
+26 -2
View File
@@ -1,6 +1,9 @@
package core package core
import ( import (
"fmt"
"strconv"
"github.com/boltdb/bolt" "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 { db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket)) b := tx.Bucket([]byte(bucket))
if b == nil { if b == nil {
b, _ = tx.CreateBucket([]byte(bucket)) b, _ = tx.CreateBucket([]byte(bucket))
} }
b.Put([]byte(key), []byte(value)) b.Put([]byte(key), []byte(fmt.Sprint(value)))
return nil return nil
}) })
} }
@@ -56,6 +59,22 @@ func (bucket Bucket) Get(kv ...string) string {
return value 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) { func (bucket Bucket) Foreach(f func(k, v []byte) error) {
db.View(func(tx *bolt.Tx) error { db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket)) b := tx.Bucket([]byte(bucket))
@@ -63,3 +82,8 @@ func (bucket Bucket) Foreach(f func(k, v []byte) error) {
return nil return nil
}) })
} }
var Int = func(s string) int {
i, _ := strconv.Atoi(s)
return i
}
+3
View File
@@ -9,6 +9,7 @@ import (
func init() { func init() {
AddCommand("", []Function{ AddCommand("", []Function{
{ {
Admin: true,
Rules: []string{"set ? ? ?"}, Rules: []string{"set ? ? ?"},
Handle: func(s im.Sender) interface{} { Handle: func(s im.Sender) interface{} {
b := Bucket(s.Get(0)) b := Bucket(s.Get(0))
@@ -20,6 +21,7 @@ func init() {
}, },
}, },
{ {
Admin: true,
Rules: []string{"delete ? ?"}, Rules: []string{"delete ? ?"},
Handle: func(s im.Sender) interface{} { Handle: func(s im.Sender) interface{} {
b := Bucket(s.Get(0)) b := Bucket(s.Get(0))
@@ -31,6 +33,7 @@ func init() {
}, },
}, },
{ {
Admin: true,
Rules: []string{"get ? ?"}, Rules: []string{"get ? ?"},
Handle: func(s im.Sender) interface{} { Handle: func(s im.Sender) interface{} {
b := Bucket(s.Get(0)) b := Bucket(s.Get(0))
-1
View File
@@ -1 +0,0 @@
config.yaml
-3
View File
@@ -1,3 +0,0 @@
host: http://127.0.0.1:5700
client_id:
client_secret:
+4 -1
View File
@@ -22,6 +22,7 @@ type Yaml struct {
} }
var Config Yaml var Config Yaml
var qinglong = core.NewBucket("qinglong")
var token string var token string
var expiration int64 var expiration int64
@@ -39,7 +40,9 @@ type Carrier struct {
} }
func init() { 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 { if v := regexp.MustCompile(`^(https?://[\.\w]+:?\d*)`).FindStringSubmatch(Config.Host); len(v) == 2 {
Config.Host = v[1] Config.Host = v[1]
} }