| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package wechat
- import (
- "net/http"
- "sync"
- "github.com/silenceper/wechat/cache"
- "github.com/silenceper/wechat/context"
- "github.com/silenceper/wechat/server"
- )
- //Wechat struct
- type Wechat struct {
- Context *context.Context
- }
- //Config for user
- type Config struct {
- AppID string
- AppSecret string
- Token string
- EncodingAESKey string
- Cache cache.Cache
- }
- //NewWechat init
- func NewWechat(cfg *Config) *Wechat {
- context := new(context.Context)
- copyConfigToContext(cfg, context)
- return &Wechat{context}
- }
- func copyConfigToContext(cfg *Config, context *context.Context) {
- context.AppID = cfg.AppID
- context.AppSecret = cfg.AppSecret
- context.Token = cfg.Token
- context.EncodingAESKey = cfg.EncodingAESKey
- context.Cache = cfg.Cache
- context.SetAccessTokenLock(new(sync.RWMutex))
- }
- //GetServer init
- func (wc *Wechat) GetServer(req *http.Request, writer http.ResponseWriter) *server.Server {
- wc.Context.Request = req
- wc.Context.Writer = writer
- return server.NewServer(wc.Context)
- }
|