-
Notifications
You must be signed in to change notification settings - Fork 0
/
Responder.go
70 lines (65 loc) · 2.16 KB
/
Responder.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
package goose
/**
* Sahil Gulati {[email protected]}
* This file will contains functionality of preparing full and final response to be emitted out.
**/
import (
"encoding/json"
"encoding/xml"
"net/http"
)
type GooseResponder struct {
writer http.ResponseWriter
route *GooseRoute
response *GooseResponse
}
func (gr GooseResponder) GetInstance() *GooseResponder {
gooseResponder := new(GooseResponder)
gooseResponder.route = gr.route
gooseResponder.writer = gr.writer
gooseResponder.response = gr.response
return gooseResponder
}
func (gr *GooseResponder) Respond() error {
gr.writeHeaders(
gr.prepareHeaders(),
)
return gr.writeResponse(
gr.prepareResponse(),
)
}
func (gr *GooseResponder) prepareHeaders() (map[string]string, int) {
gr.response.Headers = ifElse(len(gr.response.Headers) == 0, make(map[string]string), gr.response.Headers).(map[string]string)
gr.response.StatusCode = ifElse(gr.response.StatusCode == 0, 200, gr.response.StatusCode).(int)
gr.response.Headers[CONTENT_TYPE] = ifElse(gr.response.Headers[CONTENT_TYPE] != "", gr.response.Headers[CONTENT_TYPE], PLAIN_TEXT).(string)
if gr.route.hasCors {
gr.response.Headers["Access-Control-Allow-Origin"] = "*"
gr.response.Headers["Access-Control-Allow-Methods"] = "GET, POST, PUT"
gr.response.Headers["Access-Control-Allow-Headers"] = "Content-Type"
}
return gr.response.Headers, gr.response.StatusCode
}
func (gr *GooseResponder) writeHeaders(headers map[string]string, statusCode int) *GooseResponder {
for headerName, headerValue := range headers {
gr.writer.Header().Add(headerName, headerValue)
}
gr.writer.WriteHeader(statusCode)
return gr
}
func (gr *GooseResponder) prepareResponse() *GooseResponse {
return gr.response
}
func (gr *GooseResponder) writeResponse(response *GooseResponse) error {
switch response.Headers[CONTENT_TYPE] {
case APPLICATION_JSON:
return json.NewEncoder(gr.writer).Encode(response.Response)
case APPLICATION_XML:
gr.writer.Write([]byte(xml.Header))
encoder := xml.NewEncoder(gr.writer)
encoder.Indent(" ", " ")
return encoder.Encode(response.Response)
default:
gr.writer.Write([]byte(response.Response.(string)))
return nil
}
}