-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
62 lines (51 loc) · 1.02 KB
/
api.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
package middleware
import (
"io"
"net/http"
)
type MiddlewareResponse struct {
isError bool
error error
responseSpec *responseSpec
}
type MiddlewareValue interface {
Present() bool
Value() interface{}
}
func NewErrorResult(err error) *MiddlewareResponse {
return &MiddlewareResponse{isError: true, error: err}
}
type responseSpec struct {
Header http.Header
StatusCode int
// TODO
}
func Response(
statusCode int,
body io.Reader,
header http.Header,
) *MiddlewareResponse {
return &MiddlewareResponse{
responseSpec: &responseSpec{
Header: header,
StatusCode: statusCode,
},
}
}
func DefaultRespond(overide *MiddlewareResponse, res http.ResponseWriter) {
if overide == nil {
// programming error
res.WriteHeader(500)
res.Write([]byte("Server Misconfigured"))
return
}
if overide.isError {
// handle error
res.WriteHeader(500)
res.Write([]byte("Server Error"))
return
}
// set headers, etc
res.WriteHeader(overide.responseSpec.StatusCode)
res.Write([]byte{})
}