fix utils

This commit is contained in:
Jay
2018-04-24 17:49:58 +08:00
parent 1429c7fd85
commit 0a471443ce
8 changed files with 174 additions and 26 deletions
+1 -9
View File
@@ -55,13 +55,9 @@ func RedisGet(namespace, key string) (string, error) {
return "", errors.New("key empty")
}
if len(n) > 0 {
n += ":"
}
n += key
val, err := redisStore.Get(key).Result()
val, err := redisStore.Get(n).Result()
return val, err
}
@@ -77,10 +73,6 @@ func RedisDel(namespace, key string) error {
return errors.New("key empty")
}
if len(n) > 0 {
n += ":"
}
n += key
err := redisStore.Del(n).Err()
+34
View File
@@ -0,0 +1,34 @@
package middleware
import (
"encoding/json"
"git.trj.tw/golang/go-gallery/modules/context"
"git.trj.tw/golang/go-gallery/modules/memstore"
)
// VerifyToken -
func VerifyToken(c *context.Context) {
token := c.GetHeader("X-Auth-Token")
if len(token) == 0 {
c.CustomRes("Forbidden", nil)
return
}
str, err := memstore.RedisGet("golang", token)
if err != nil || len(str) == 0 {
c.CustomRes("Forbidden", "token invaild")
return
}
jsonData := make(map[string]interface{})
err = json.Unmarshal([]byte(str), &jsonData)
if err != nil {
c.ServerError(nil)
}
c.Set("token", jsonData)
c.Next()
}
+4 -1
View File
@@ -7,7 +7,10 @@ import (
// ToMap struct to map[string]interface{}
func ToMap(ss interface{}) map[string]interface{} {
t := reflect.ValueOf(ss).Elem()
t := reflect.ValueOf(ss)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
smap := make(map[string]interface{})
mtag := regexp.MustCompile(`cc:\"(.+)\"`)