package mainimport ( "encoding/base64" "encoding/json" "log" "net/http" "strconv" "strings" "github.com/go-redis/redis" "github.com/satori/go.uuid")type objMessage struct { UID int64 `json:"uid"` UserName string `json:"user_name"`}type token struct { Token string `json:"token"`}type tokenResponse struct { Data token `json:"data"`}var client *redis.Clientfunc genToken(appid int, username string) string { u2, _ := uuid.NewV4() return u2.String()}func grantHandler(w http.ResponseWriter, r *http.Request) { encodeBasic := strings.TrimPrefix(r.Header["Authorization"][0], "Basic ") decodeBasic, _ := base64.StdEncoding.DecodeString(encodeBasic) auth := string(decodeBasic) appidStr := strings.Split(auth, ":")[0] appid, _ := strconv.Atoi(appidStr) var om objMessage err := json.NewDecoder(r.Body).Decode(&om) if err != nil { http.Error(w, err.Error(), 400) return } var tr tokenResponse tr.Data.Token = genToken(appid, om.UserName) fmap := make(map[string]interface{}) fmap["user_id"] = int64(om.UID) fmap["app_id"] = int64(appid) fmap["notification_on"] = int8(1) fmap["forbidden"] = int(1) client.HMSet("access_token_"+tr.Data.Token, fmap) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(tr)}func main() { http.HandleFunc("/auth/grant", grantHandler) client = redis.NewClient(&redis.Options{ Addr: "172.16.0.201:6379", Password: "", // no password set DB: 1, // use default DB }) log.Fatal(http.ListenAndServe(":8090", nil))}