wechat.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package wechat
  2. import (
  3. "net/http"
  4. "sync"
  5. "github.com/silenceper/wechat/cache"
  6. "github.com/silenceper/wechat/context"
  7. "github.com/silenceper/wechat/server"
  8. )
  9. //Wechat struct
  10. type Wechat struct {
  11. Context *context.Context
  12. }
  13. //Config for user
  14. type Config struct {
  15. AppID string
  16. AppSecret string
  17. Token string
  18. EncodingAESKey string
  19. Cache cache.Cache
  20. }
  21. //NewWechat init
  22. func NewWechat(cfg *Config) *Wechat {
  23. context := new(context.Context)
  24. copyConfigToContext(cfg, context)
  25. return &Wechat{context}
  26. }
  27. func copyConfigToContext(cfg *Config, context *context.Context) {
  28. context.AppID = cfg.AppID
  29. context.AppSecret = cfg.AppSecret
  30. context.Token = cfg.Token
  31. context.EncodingAESKey = cfg.EncodingAESKey
  32. context.Cache = cfg.Cache
  33. context.SetAccessTokenLock(new(sync.RWMutex))
  34. }
  35. //GetServer init
  36. func (wc *Wechat) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
  37. wc.Context.Request = req
  38. wc.Context.Writer = writer
  39. return server.NewServer(wc.Context)
  40. }