-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalizer.go
44 lines (37 loc) · 1.02 KB
/
finalizer.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
package httper
import (
"io"
"net/http"
)
// Finalizer finalizes an htpp response.
type Finalizer interface {
HandleError(err error, w io.Writer, r *http.Request) bool
HandleSuccess(w io.Writer, r io.Reader) error
}
// DefaultFinalizer for an http response.
type DefaultFinalizer struct {
}
// HandleError prints http 500.
func (f DefaultFinalizer) HandleError(err error, w io.Writer, r *http.Request) bool {
if x, ok := w.(http.ResponseWriter); ok {
http.Error(x, err.Error(), http.StatusInternalServerError)
}
return true
}
// HandleSuccess prints http 200 and prints r.
func (f DefaultFinalizer) HandleSuccess(w io.Writer, r io.Reader) error {
if x, ok := w.(http.ResponseWriter); ok {
x.WriteHeader(http.StatusOK)
}
return nil
}
// HTTPFinalizer finalizes an HTTP response.
type HTTPFinalizer struct {
DefaultFinalizer
}
// HandleSuccess prints http 200 and prints r.
func (f HTTPFinalizer) HandleSuccess(w io.Writer, r io.Reader) error {
f.DefaultFinalizer.HandleSuccess(w, r)
_, err := io.Copy(w, r)
return err
}