-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
140 lines (116 loc) · 3.42 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package Sex
import (
"fmt"
"net/http"
"time"
)
// Response to make complete response with Cookies, Headers, and all http.ResponseWrite another features
type Response struct {
http.ResponseWriter
Headers *http.Header
Body []byte
Status int
err error
}
// Error provides Pistol last error
func (r *Response) Error() error { return r.err }
// SetErr sets Pistol last error
func (r *Response) SetErr(err error) { r.err = err }
// WriteHeader sets Response status code
func (r *Response) WriteHeader(status int) {
r.SetStatus(status)
}
// SetBody sets Response body
func (r *Response) SetBody(v []byte) *Response {
r.Body = v
return r
}
// SetStatus sets Response status code
func (r *Response) SetStatus(code int) *Response {
r.Status = 200
return r
}
// SetCookie sets Response cookies
func (r *Response) SetCookie(key string, value string, expires time.Duration) *Response {
cookie := &http.Cookie{
Name: key,
Value: value,
Expires: time.Now().Add(expires),
}
http.SetCookie(r, cookie)
return r
}
// Header returns response headers setter
func (r *Response) Header() http.Header {
if r.Headers == nil {
r.Headers = &http.Header{
"Content-Type": {"text/pain; charset=UTF-8"},
"Content-Length": {"0"},
}
}
return *r.Headers
}
// NewResponse provides new Response
func NewResponse() *Response {
return new(Response)
}
// runRoute run sex route function
func runRoute(route_func interface{}, response Response, r Request) error {
if route_func, ok := route_func.(func(Response, Request)); ok {
route_func(response, r)
}
if route_func, ok := route_func.(func(*Response, *Request)); ok {
route_func(&response, &r)
}
if route_func, ok := route_func.(func(http.ResponseWriter, *http.Request)); ok {
route_func(response.ResponseWriter, &r.Request)
}
if route_func, ok := route_func.(func(Request) ([]byte, int)); ok {
res, status := route_func(r)
response.WriteHeader(status)
response.Write(res)
}
if route_func, ok := route_func.(func(Request) []byte); ok {
res := route_func(r)
response.Write(res)
}
if route_func, ok := route_func.(func(Request) (string, int)); ok {
res, status := route_func(r)
response.WriteHeader(status)
response.Write([]byte(res))
}
if route_func, ok := route_func.(func(Request) string); ok {
res := route_func(r)
response.Write([]byte(res))
}
if route_func, ok := route_func.(func(Request) (*Response, int)); ok {
res, status := route_func(r)
response.WriteHeader(status)
response.Write(res.Body)
}
if route_func, ok := route_func.(func(Request) *Response); ok {
res := route_func(r)
response.Write(res.Body)
}
if route_func, ok := route_func.(func(Request) (Json, int)); ok {
res, status := route_func(r)
response.Header().Set("Content-Type", "application/json")
response.WriteHeader(status)
response.Write(Byteify(res))
}
if route_func, ok := route_func.(func(Request) Json); ok {
res := route_func(r)
response.Header().Set("Content-Type", "application/json")
response.Write(Byteify(res))
}
response_log_message := Fmt("%s %s %s %d: %s", r.Method, r.URL.Path, r.URL.RawQuery, response.Status, StatusText(response.Status))
if response.Status >= 400 {
RawLog(LogLevelError, false, response_log_message)
return fmt.Errorf(string(response.Body))
} else if response.Status >= 300 {
RawLog(LogLevelWarn, false, response_log_message)
} else if response.Status >= 200 {
RawLog(LogLevelInfo, false, response_log_message)
}
return nil
}