-
Notifications
You must be signed in to change notification settings - Fork 14
/
server_frontend.go
47 lines (39 loc) · 1.28 KB
/
server_frontend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// @description wechat2 是腾讯微信公众平台 api 的 golang 语言封装
// @link https://github.com/philsong/wechat2 for the canonical source repository
// @license https://github.com/philsong/wechat2/blob/master/LICENSE
// @authors chanxuehong(chanxuehong@gmail.com)
package mp
import (
"fmt"
"net/http"
"net/url"
)
// 实现了 http.Handler, 处理一个公众号的消息(事件)请求.
type WechatServerFrontend struct {
wechatServer WechatServer
invalidRequestHandler InvalidRequestHandler
}
func NewWechatServerFrontend(server WechatServer, handler InvalidRequestHandler) *WechatServerFrontend {
if server == nil {
panic("mp: nil WechatServer")
}
if handler == nil {
handler = DefaultInvalidRequestHandler
}
return &WechatServerFrontend{
wechatServer: server,
invalidRequestHandler: handler,
}
}
// 实现 http.Handler.
func (frontend *WechatServerFrontend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
wechatServer := frontend.wechatServer
invalidRequestHandler := frontend.invalidRequestHandler
urlValues, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
fmt.Println("err", err)
invalidRequestHandler.ServeInvalidRequest(w, r, err)
return
}
ServeHTTP(w, r, urlValues, wechatServer, invalidRequestHandler)
}