-
Notifications
You must be signed in to change notification settings - Fork 0
/
response-handler.go
35 lines (31 loc) · 957 Bytes
/
response-handler.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
package gfs
import (
"encoding/json"
"encoding/xml"
"html/template"
"log"
"net/http"
)
// A basic handler that suplies basic logic for writing responses
type responseHandler struct {
}
// Takes care of writing a response in the correct format
func (r *responseHandler) WriteResponse(writer http.ResponseWriter, statusCode int, htmlTemplate *template.Template, format string, response interface{}) error {
switch format {
default:
log.Println("Unknown response format", format)
fallthrough
case FormatHtml:
writer.Header().Set("content-type", FormatHtml)
writer.WriteHeader(statusCode)
return htmlTemplate.Execute(writer, response)
case FormatJson:
writer.Header().Set("content-type", FormatJson)
writer.WriteHeader(statusCode)
return json.NewEncoder(writer).Encode(response)
case FormatXml:
writer.Header().Set("content-type", FormatXml)
writer.WriteHeader(statusCode)
return xml.NewEncoder(writer).Encode(response)
}
}