/
context.go
executable file
·102 lines (82 loc) · 2.77 KB
/
context.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package restful
import (
"context"
"net/http"
"github.com/emicklei/go-restful"
)
//Context is a struct which has both request and response objects
type Context struct {
ctx context.Context
req *restful.Request
resp *restful.Response
}
//NewBaseServer is a function which return context
func NewBaseServer(ctx context.Context) *Context {
return &Context{
ctx: ctx,
}
}
// write is the response writer.
func (bs *Context) Write(body []byte) {
bs.resp.Write(body)
}
//WriteHeader is the response head writer
func (bs *Context) WriteHeader(httpStatus int) {
bs.resp.WriteHeader(httpStatus)
}
//AddHeader is a function used to add header to a response
func (bs *Context) AddHeader(header string, value string) {
bs.resp.AddHeader(header, value)
}
//WriteError is a function used to write error into a response
func (bs *Context) WriteError(httpStatus int, err error) error {
return bs.resp.WriteError(httpStatus, err)
}
// WriteJSON used to write a JSON file into response
func (bs *Context) WriteJSON(value interface{}, contentType string) error {
return bs.resp.WriteJson(value, contentType)
}
// WriteHeaderAndJSON used to write head and JSON file in to response
func (bs *Context) WriteHeaderAndJSON(status int, value interface{}, contentType string) error {
return bs.resp.WriteHeaderAndJson(status, value, contentType)
}
//ReadEntity is request reader
func (bs *Context) ReadEntity(schema interface{}) (err error) {
return bs.req.ReadEntity(schema)
}
//ReadHeader is used to read header of request
func (bs *Context) ReadHeader(name string) string {
return bs.req.HeaderParameter(name)
}
//ReadPathParameter is used to read path parameter of a request
func (bs *Context) ReadPathParameter(name string) string {
return bs.req.PathParameter(name)
}
//ReadPathParameters used to read multiple path parameters of a request
func (bs *Context) ReadPathParameters() map[string]string {
return bs.req.PathParameters()
}
//ReadQueryParameter is used to read query parameter of a request
func (bs *Context) ReadQueryParameter(name string) string {
return bs.req.QueryParameter(name)
}
//ReadBodyParameter used to read body parameter of a request
func (bs *Context) ReadBodyParameter(name string) (string, error) {
return bs.req.BodyParameter(name)
}
//ReadRequest return a native net/http request
func (bs *Context) ReadRequest() *http.Request {
return bs.req.Request
}
//ReadRestfulRequest return a native go-restful request
func (bs *Context) ReadRestfulRequest() *restful.Request {
return bs.req
}
//ReadResponseWriter return a native net/http ResponseWriter
func (bs *Context) ReadResponseWriter() http.ResponseWriter {
return bs.resp.ResponseWriter
}
//ReadRestfulResponse return a native go-restful Response
func (bs *Context) ReadRestfulResponse() *restful.Response {
return bs.resp
}