package site import ( "encoding/json" "errors" "log" "git.tetele.net/tgo/redisrpc" ) /** * 从redis获取分站数据库及ID信息 * 2020/06/08 * gz */ func GetSiteFromRedis(host string) (map[string]string, error) { var site map[string]string = make(map[string]string) info, err := redisrpc.HGetString(SITE_KEY, host) if err != nil { return site, err } type siteStruct struct { Database string `json:"database"` SiteId string `json:"site_id"` } var siteInfo siteStruct err = json.Unmarshal([]byte(info), &siteInfo) if err != nil { return site, err } if siteInfo.Database != "" { site["database"] = siteInfo.Database } if siteInfo.SiteId != "" { site["site_id"] = siteInfo.SiteId } return site, nil } /** * 分站数据库及ID信息保存及redis * 2020/06/08 * gz */ func SetToRedis(host string, site map[string]string) error { if host == "" { return errors.New("没有域名") } if len(site) < 1 { return errors.New("没有站点信息") } siteStr, err := json.Marshal(site) if err != nil { return err } _, err = redisrpc.HSet(SITE_KEY, host, string(siteStr)) return err } /**/ func GetAll() ([]map[string]string, error) { list, err := redisrpc.HGetAll(SITE_KEY) if err != nil { return nil, err } var siteInfo map[string]string var all []map[string]string if len(list) > 0 { for _, item := range list { siteInfo = make(map[string]string) for key, value := range item { err = json.Unmarshal([]byte(value), &siteInfo) if err != nil { log.Println("json unmarshal error", err, value) break } siteInfo["domainname"] = key } if len(siteInfo) < 1 { continue } all = append(all, siteInfo) } } return all, err }