-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
47 lines (44 loc) · 1.36 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
package openapirouter
import (
"encoding/json"
"log"
"net/http"
)
// Response is a data struct that depicts the http response to be returned.
type Response struct {
// http StatusCode to return
StatusCode int
// Body of the http request to return. If it is set to string, a plain text response is return. If it is anything
// else, the response is returned in JSON format.
Body interface{}
// http Headers to add to the response
Headers map[string]string
}
// write is used by the requestHandler and writes the result of the request as an http response.
func (response *Response) write(writer http.ResponseWriter) {
var err error
for key, value := range response.Headers {
writer.Header().Set(key, value)
}
switch data := response.Body.(type) {
case string:
writer.Header().Set("Content-Type", "text/plain; charset=utf-8")
writer.WriteHeader(response.StatusCode)
_, err = writer.Write([]byte(data))
default:
if data != nil {
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(response.StatusCode)
err = json.NewEncoder(writer).Encode(response.Body)
} else {
writer.WriteHeader(response.StatusCode)
_, err = writer.Write(make([]byte, 0))
}
}
if err != nil {
log.Println("Could not write response", err)
if response.StatusCode != http.StatusInternalServerError {
error500Response.write(writer)
}
}
}