-
Notifications
You must be signed in to change notification settings - Fork 12
/
handler.go
58 lines (47 loc) · 1.29 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"log"
"net/http"
)
type Handler func(http.ResponseWriter, *http.Request) Response
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
response := h(w, r)
log.Println("Responding to the HTTP request with:")
response.logResponse()
response.WriteResponse(w)
}
type Response interface {
WriteResponse(http.ResponseWriter)
logResponse()
}
type ErrorResponse struct {
Error error
Code int
ErrorMessage string
}
func (r ErrorResponse) WriteResponse(w http.ResponseWriter) {
http.Error(w, r.ErrorMessage, r.Code)
}
func (r ErrorResponse) logResponse() {
if r.Error != nil {
log.Printf("Error: %s: %v\n", r.ErrorMessage, r.Error)
} else {
log.Printf("Error: %s\n", r.ErrorMessage)
}
}
type SuccessResponse struct {
Message string
}
func (r SuccessResponse) WriteResponse(w http.ResponseWriter) {
w.Write([]byte(r.Message))
}
func (r SuccessResponse) logResponse() {
log.Printf("Success: %s\n", r.Message)
}
// handleAsyncResponse provides consistent error/success logging for operations
// that are left to continue working after the original HTTP request that
// initiated the operation has been handled and closed.
func handleAsyncResponse(response Response) {
log.Println("Finishing an asynchronous operation with:")
response.logResponse()
}