-
Notifications
You must be signed in to change notification settings - Fork 6
/
handlers.go
44 lines (35 loc) · 1.87 KB
/
handlers.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 slimhttp
import (
"net/http"
"github.com/gorilla/mux"
)
// HandleStatic binds a new fileserver using the passed prefix and
// path to the router
func (r *Router) HandleStatic(prefix, path string) *mux.Route {
fs := http.FileServer(http.Dir(path))
return r.Router.PathPrefix(prefix).Handler(http.StripPrefix(prefix, fs))
}
// HandleEndpoint binds a new Endpoint and Encoder handler to the router
func (r *Router) HandleEndpoint(pattern string, endpoint Endpoint, encoder Encoder, opts ...Option) *mux.Route {
return r.Router.HandleFunc(pattern, r.endpointWrapper(endpoint, encoder, opts...))
}
// HandleJSONEndpoint binds a new JSON Endpoint handler to the router
func (r *Router) HandleJSONEndpoint(pattern string, endpoint Endpoint, opts ...Option) *mux.Route {
return r.Router.HandleFunc(pattern, r.endpointWrapper(endpoint, encodeJSON, opts...))
}
// HandleXMLEndpoint binds a new XML Endpoint handler to the router
func (r *Router) HandleXMLEndpoint(pattern string, endpoint Endpoint, opts ...Option) *mux.Route {
return r.Router.HandleFunc(pattern, r.endpointWrapper(endpoint, encodeXML, opts...))
}
// HandleTextEndpoint binds a new Text Endpoint handler to the router
func (r *Router) HandleTextEndpoint(pattern string, endpoint Endpoint, opts ...Option) *mux.Route {
return r.Router.HandleFunc(pattern, r.endpointWrapper(endpoint, encodeText, opts...))
}
// HandleBytesEndpoint binds a new Bytes Endpoint handler to the router
func (r *Router) HandleBytesEndpoint(pattern string, endpoint Endpoint, opts ...Option) *mux.Route {
return r.Router.HandleFunc(pattern, r.endpointWrapper(endpoint, encodeBytes, opts...))
}
// HandleHTMLEndpoint binds a new HTML Endpoint handler to the router
func (r *Router) HandleHTMLEndpoint(pattern string, endpoint Endpoint, opts ...Option) *mux.Route {
return r.Router.HandleFunc(pattern, r.endpointWrapper(endpoint, encodeHTML, opts...))
}