-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.go
More file actions
67 lines (55 loc) · 1.17 KB
/
handler.go
File metadata and controls
67 lines (55 loc) · 1.17 KB
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
package fastjsonrpc
import (
"github.com/valyala/fasthttp"
"github.com/valyala/fastjson"
)
func Rpc(h Handler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
c := getContext()
defer func() {
_, _ = c.w.WriteTo(ctx)
putContext(c)
}()
c.Ctx = ctx
ctx.Response.Header.Set("Content-Type", "application/json; charset=UTF-8")
var err error
if c.request, err = c.pr.ParseBytes(ctx.Request.Body()); err != nil {
_, _ = c.w.Write(errParse)
return
}
if c.request.Type() != fastjson.TypeObject {
_, _ = c.w.Write(errInvalidRequest)
return
}
c.setRequest(c.request)
if len(c.Method) == 0 {
_, _ = c.w.Write(errInvalidRequest)
return
}
h(c)
if c.Error == nil {
c.writeResult(c.w)
} else {
c.writeError(c.w)
}
}
}
var null = []byte("null")
func Get(h Handler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
c := getContext()
defer func() {
_, _ = c.w.WriteTo(ctx)
putContext(c)
}()
c.Ctx = ctx
ctx.Response.Header.Set("Content-Type", "application/json; charset=UTF-8")
c.id = null
h(c)
if c.Error == nil {
c.writeResult(c.w)
} else {
c.writeError(c.w)
}
}
}