-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
103 lines (82 loc) · 2.9 KB
/
response.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
103
package gohttpclient
import (
"fmt"
"net/http"
)
// ************************************************************************************************
// ** HTTPResponse
// ************************************************************************************************
type HTTPResponse interface {
GetBody() string
GetStatus() string
GetStatusCode() int
GetHeaders() map[string][]string
}
// ************************************************************************************************
// ** DefaultHTTPResponse
// ************************************************************************************************
type DefaultHTTPResponse struct {
body string
statusCode int
status string
headers map[string][]string
}
func NewDefaultHTTPResponse(body string, statusCode int, status string, headers map[string][]string) *DefaultHTTPResponse {
ret := &DefaultHTTPResponse{}
ret.initialize(body, statusCode, status, headers)
return ret
}
func (st *DefaultHTTPResponse) initialize(body string, statusCode int, status string, headers map[string][]string) {
st.body = body
st.statusCode = statusCode
st.status = status
st.headers = headers
}
func (st *DefaultHTTPResponse) GetBody() string {
return st.body
}
func (st *DefaultHTTPResponse) GetStatus() string {
return st.status
}
func (st *DefaultHTTPResponse) GetStatusCode() int {
return st.statusCode
}
func (st *DefaultHTTPResponse) GetHeaders() map[string][]string {
return st.headers
}
// ************************************************************************************************
// ** ResponseWriter
// ************************************************************************************************
// ** HTTP response writer
// ************************************************************************************************
type ResponseWriter interface {
SetStatusCode(statusCode int)
AddHeader(key string, value string)
Print(message string)
Printf(format string, a ...interface{})
}
// ************************************************************************************************
// ** DefaultResponseWriter
// ************************************************************************************************
// ** ResponseWriter implementation for native GO http
// ************************************************************************************************
func NewDefaultResponseWriter(w http.ResponseWriter) *DefaultResponseWriter {
return &DefaultResponseWriter{
w: w,
}
}
type DefaultResponseWriter struct {
w http.ResponseWriter
}
func (st *DefaultResponseWriter) SetStatusCode(statusCode int) {
st.w.WriteHeader(statusCode)
}
func (st *DefaultResponseWriter) AddHeader(key string, value string) {
st.w.Header().Add(key, value)
}
func (st *DefaultResponseWriter) Print(message string) {
fmt.Fprint(st.w, message)
}
func (st *DefaultResponseWriter) Printf(format string, a ...interface{}) {
fmt.Fprintf(st.w, format, a...)
}