forked from moogar0880/problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
49 lines (44 loc) · 1.5 KB
/
web.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
package problems
import (
"encoding/json"
"encoding/xml"
"net/http"
)
// ProblemHandler returns an http.HandlerFunc which writes a provided problem
// to an http.ResponseWriter as JSON
func ProblemHandler(p Problem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ProblemMediaType)
json.NewEncoder(w).Encode(p)
}
}
// XMLProblemHandler returns an http.HandlerFunc which writes a provided problem
// to an http.ResponseWriter as XML
func XMLProblemHandler(p Problem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ProblemMediaTypeXML)
xml.NewEncoder(w).Encode(p)
}
}
// StatusProblemHandler returns an http.HandlerFunc which writes a provided
// problem to an http.ResponseWriter as JSON with the status code
func StatusProblemHandler(p StatusProblem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ProblemMediaType)
if p.ProblemStatus() != 0 {
w.WriteHeader(p.ProblemStatus())
}
json.NewEncoder(w).Encode(p)
}
}
// XMLStatusProblemHandler returns an http.HandlerFunc which writes a provided
// problem to an http.ResponseWriter as XML with the status code
func XMLStatusProblemHandler(p StatusProblem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ProblemMediaTypeXML)
if p.ProblemStatus() != 0 {
w.WriteHeader(p.ProblemStatus())
}
xml.NewEncoder(w).Encode(p)
}
}