-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
48 lines (40 loc) · 1.19 KB
/
handler.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
package gin
import (
"net/http"
"github.com/go-chassis/go-chassis/v2/core/handler"
"github.com/go-chassis/go-chassis/v2/core/invocation"
"github.com/go-chassis/go-chassis/v2/core/server"
)
// ResourceHandler wraps go-chassis restful function
type ResourceHandler struct {
handleFunc func(ctx *Context)
rc *Context
opts server.Options
}
// Handle is to handle the router related things
func (h *ResourceHandler) Handle(chain *handler.Chain, inv *invocation.Invocation, cb invocation.ResponseCallBack) {
Invocation2HTTPRequest(inv, h.rc.C)
// check body size
if h.opts.BodyLimit > 0 {
h.rc.C.Request.Body = http.MaxBytesReader(h.rc.C.Writer, h.rc.C.Request.Body, h.opts.BodyLimit)
}
h.rc.Ctx = inv.Ctx
// call real route func
h.handleFunc(h.rc)
ir := &invocation.Response{}
ir.Status = h.rc.C.Writer.Status()
ir.Result = h.rc.C.Writer
//call next chain
cb(ir)
}
func newHandler(f func(ctx *Context), rc *Context, opts server.Options) handler.Handler {
return &ResourceHandler{
handleFunc: f,
rc: rc,
opts: opts,
}
}
// Name returns the name string
func (h *ResourceHandler) Name() string {
return "gin"
}