-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
71 lines (69 loc) · 1.8 KB
/
http.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
xerr "github.com/goclub/error"
xhttp "github.com/goclub/http"
"net/http"
)
func (dep Service) httpListen() (err error) {
router := xhttp.NewRouter(xhttp.RouterOption{
OnCatchError: func(c *xhttp.Context, err error) error {
if reject, asReject := xerr.AsReject(err); asReject {
if reject.ShouldRecord {
dep.sentryClient.Error(err)
}
return c.WriteJSON(reject.Resp())
}
dep.sentryClient.Error(err)
c.WriteStatusCode(500)
return c.WriteBytes([]byte("system error(error)"))
},
})
router.HandleFunc(xhttp.Route{xhttp.POST, "/wx-api-center-control/cgi-bin/ticket/getticket"}, func(c *xhttp.Context) (err error) {
ctx := c.Request.Context()
var matchApp ConfigApp
if matchApp, err = dep.AuthAndMatch(c); err != nil {
return
}
apiType := c.Request.URL.Query().Get("type")
if apiType == "" {
return xerr.Reject(1, "query 中的 type 不能为空", true)
}
var ticket string
if ticket, err = dep.Ticket(ctx, matchApp, apiType); err != nil {
return
}
return c.WriteJSON(struct {
xerr.Resp
Ticket string `json:"ticket"`
}{
Ticket: ticket,
})
})
router.HandleFunc(xhttp.Route{xhttp.POST, "/wx-api-center-control/cgi-bin/token"}, func(c *xhttp.Context) (err error) {
ctx := c.Request.Context()
var matchApp ConfigApp
if matchApp, err = dep.AuthAndMatch(c); err != nil {
return
}
var accessToken string
if accessToken, err = dep.AccessToken(ctx, matchApp); err != nil {
return
}
return c.WriteJSON(struct {
xerr.Resp
AccessToken string `json:"access_token"`
}{
AccessToken: accessToken,
})
})
s := &http.Server{
Addr: ":" + dep.config.HttpPort,
Handler: router,
}
router.LogPatterns(s)
err = s.ListenAndServe() // indivisible begin
if err != nil { // indivisible end
return
}
return
}