site
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package site
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "git.tetele.net/tgo/redisrpc"
  6. )
  7. /**
  8. * 从redis获取分站数据库及ID信息
  9. * 2020/06/08
  10. * gz
  11. */
  12. func GetSiteFromRedis(host string) (map[string]string, error) {
  13. var site map[string]string = make(map[string]string)
  14. info, err := redisrpc.HGetString(SITE_KEY, host)
  15. if err != nil {
  16. return site, err
  17. }
  18. type siteStruct struct {
  19. Database string `json:"database"`
  20. SiteId string `json:"site_id"`
  21. }
  22. var siteInfo siteStruct
  23. err = json.Unmarshal([]byte(info), &siteInfo)
  24. if err != nil {
  25. return site, err
  26. }
  27. if siteInfo.Database != "" {
  28. site["database"] = siteInfo.Database
  29. }
  30. if siteInfo.SiteId != "" {
  31. site["site_id"] = siteInfo.SiteId
  32. }
  33. return site, nil
  34. }
  35. /**
  36. * 分站数据库及ID信息保存及redis
  37. * 2020/06/08
  38. * gz
  39. */
  40. func SetToRedis(host string, site map[string]string) error {
  41. if host == "" {
  42. return errors.New("没有域名")
  43. }
  44. if len(site) < 1 {
  45. return errors.New("没有站点信息")
  46. }
  47. siteStr, err := json.Marshal(site)
  48. if err != nil {
  49. return err
  50. }
  51. _, err = redisrpc.HSet(SITE_KEY, host, string(siteStr))
  52. return err
  53. }
  54. /*
  55. func GetAll() ([]map[string]string, error) {
  56. list, err := redisrpc.HGetAll(SITE_KEY)
  57. if err != nil {
  58. return nil, err
  59. }
  60. var siteInfo map[string]string
  61. var all []map[string]string
  62. if len(list) > 0 {
  63. for key, item := range list {
  64. if (key+1)%2 == 0 { //只处理偶数位
  65. siteInfo = make(map[string]string)
  66. err = json.Unmarshal(item, &siteInfo)
  67. if err != nil {
  68. continue
  69. }
  70. siteInfo["domainname"] = string(list[key-1])
  71. all = append(all, siteInfo)
  72. }
  73. }
  74. }
  75. return all, err
  76. }
  77. */