Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

69 lignes
1.6KB

  1. package main
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "io/ioutil"
  7. "log"
  8. )
  9. //WechatAPIConfig all secret related API Config
  10. type WechatAPIConfig struct {
  11. //Token is the wechat API shared secrete
  12. Token string `json:"Token"`
  13. //EncodingAESKey is the Key for encrypt messages
  14. EncodingAESKey string `'json:"EncodingAESKey"`
  15. //Appid is wechat public account appid
  16. Appid string `json:"Appid"`
  17. //AppSecret is how we identify ourselves.
  18. AppSecret string `json:"AppSecret"`
  19. //AuthTokenSaveTo
  20. AuthTokenSaveTo string `json:"AuthTokenSaveTo"`
  21. //PublicAccountID
  22. PublicAccountID string `json:"PublicAccountID"`
  23. }
  24. //APIConfig contains secrets that cannot store in source file
  25. var APIConfig WechatAPIConfig
  26. func readConfig() error {
  27. log.Printf("read config from %s\r\n", "server_config.json")
  28. body, err := ioutil.ReadFile("server_config.json")
  29. if err != nil {
  30. log.Fatal("Cannot read config from server_config.json")
  31. return err
  32. }
  33. err = json.Unmarshal(body, &APIConfig)
  34. if APIConfig.AppSecret == "" {
  35. return errors.New("AppSecret not available")
  36. }
  37. if APIConfig.Appid == "" {
  38. return errors.New("AppId not available")
  39. }
  40. if APIConfig.AuthTokenSaveTo == "" {
  41. return errors.New("where to save accesstoken is unknown")
  42. }
  43. if APIConfig.EncodingAESKey == "" {
  44. return errors.New("unknown AesEncryptionKey")
  45. }
  46. if APIConfig.Token == "" {
  47. return errors.New("hidden shared secret token is empty")
  48. }
  49. return err
  50. }
  51. //convert AesEncryptKey into Binary 32bytes
  52. func getAesEncryptKey() [32]byte {
  53. key, _ := base64.StdEncoding.DecodeString(APIConfig.EncodingAESKey + "=")
  54. var k [32]byte
  55. copy(k[:], key)
  56. return k
  57. }