/
message.go
49 lines (43 loc) · 1.81 KB
/
message.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
package main
import (
"github.com/ugorji/go/codec"
"reflect"
)
type Request struct {
V int `codec:"v" msgpack:"v"`
Env struct {
ReferenceIp string `codec:"REFERENCE_IP", msgpack:"REFERENCE_IP"`
Method string `codec:"METHOD", msgpack:"METHOD"`
Url string `codec:"URL", msgpack:"URL"`
OauthRequesterId string `codec:"OAUTH_REQUESTER_ID", msgpack:"OAUTH_REQUESTER_ID"`
ContentType string `codec:"CONTENT_TYPE", msgpack:"CONTENT_TYPE"`
ServerName string `codec:"SERVER_NAME", msgpack:"SERVER_NAME"`
ServerPort int `codec:"SERVER_PORT", msgpack:"SERVER_PORT"`
PathInfo string `codec:"PATH_INFO", msgpack:"PATH_INFO"`
QueryString string `codec:"QUERY_STRING", msgpack:"QUERY_STRING"`
} `codec:"env" msgpack:"env"`
Headers map[string]string `codec:"headers", msgpack:"headers"`
Body []byte `codec:"body", msgpack:"body"`
BodyEnciding string `codec:"body_encoding", msgpack:"body_encoding"`
Options map[string]interface{} `codec:"options", msgpack:"options"`
}
type Response struct {
Headers map[string]string `codec:"headers", msgpack:"headers"`
Status string `codec:"status", msgpack:"status"`
Body []byte `codec:"body", msgpack:"body"`
BodyEncoding string `codec:"body_encoding", msgpack:"body_encoding"`
}
func DecodeRequest(body []byte, req *Request) (err error) {
mh := codec.MsgpackHandle{RawToString: true}
mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
dec := codec.NewDecoderBytes(body, &mh)
err = dec.Decode(req)
return
}
func (res *Response) Encode(body *[]byte) error {
mh := codec.MsgpackHandle{RawToString: true}
enc := codec.NewEncoderBytes(body, &mh)
err := enc.Encode(res)
return err
}
// vim:set noexpandtab ts=2: