-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
92 lines (79 loc) · 2.08 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
package server
import (
"fmt";
"time";
"net/http";
"path/filepath";
"encoding/json";
)
const (
TEXT string = "TEXT_RESPONSE"
FILE string = "FILE_RESPONSE"
REDIRECT string = "REDIRECT_RESPONSE"
)
func newResponse(request *Request) *Response {
return &Response{
request: request,
Code: 200,
}
}
type Response struct {
request *Request
responseType string
Code int
Body string
Duration time.Duration
}
func (r *Response) Plain(data string, params ...interface{}) *Response {
r.responseType = TEXT
r.Body = fmt.Sprintf(data, params...)
return r
}
func (r *Response) Json(data interface{}) *Response {
r.SetHeader("Content-Type", "application/json")
marshaled, err := json.Marshal(data)
if err != nil {
return r.Error(500, "Unable to encode response. %s", err.Error())
}
return r.Plain(string(marshaled))
}
func (r *Response) Error(code int, message string, params ...interface{}) *Response {
r.SetCode(code)
return r.Plain(message, params...)
}
func (r *Response) ErrorJson(code int, data interface{}) *Response {
r.SetCode(code)
return r.Json(data)
}
func (r *Response) File(path string) *Response {
r.responseType = FILE
r.Body = filepath.Join(r.request.server.config.StaticRoot, path)
return r
}
func (r *Response) Redirect(code int, url string) *Response {
r.responseType = REDIRECT
r.Body = url
return r.SetCode(code)
}
func (r *Response) SetCode(code int) *Response {
r.Code = code
return r
}
func (r *Response) SetHeader(key string, value string) *Response {
r.request.responseWriter.Header().Set(key, value)
return r
}
func (r *Response) write() {
r.Duration = time.Now().Sub(r.request.createdAt)
switch r.responseType {
case TEXT:
r.request.responseWriter.WriteHeader(r.Code)
fmt.Fprint(r.request.responseWriter, r.Body)
case FILE:
http.ServeFile(r.request.responseWriter, r.request.rawRequest, r.Body)
case REDIRECT:
http.Redirect(r.request.responseWriter, r.request.rawRequest, r.Body, r.Code)
default:
fmt.Fprint(r.request.responseWriter, "No response type specified.")
}
}