fix utils
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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:\"(.+)\"`)
|
||||
|
||||
Reference in New Issue
Block a user