-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtea.go
29 lines (23 loc) · 846 Bytes
/
tea.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
package tea
import (
"net/http"
"github.com/go-chi/render"
)
// Responder is the default responder used to write the messages back to the
// client. It uses render.JSON by default which will respond using JSON encoding.
// You can set it to render.JSON, render.XML, render.Data, etc. See
// github.com/go-chi/render for more info
var Responder = render.JSON
// StatusHandlerFunc is a handler that returns a status code and a message body
type StatusHandlerFunc func(w http.ResponseWriter, r *http.Request) (int, interface{})
// Handler wraps a StatusHandlerFunc and returns a standard lib http.HandlerFunc
func Handler(h StatusHandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
status, response := h(w, r)
w.WriteHeader(status)
if response == nil {
return
}
Responder(w, r, response)
}
}