You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.8 KiB
92 lines
2.8 KiB
4 years ago
|
package http
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"path"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
3 years ago
|
// HandlerFunc defines a function to serve HTTP requests.
|
||
|
type HandlerFunc func(Context) error
|
||
4 years ago
|
|
||
3 years ago
|
// Router is an HTTP router.
|
||
|
type Router struct {
|
||
4 years ago
|
prefix string
|
||
|
pool sync.Pool
|
||
|
srv *Server
|
||
|
filters []FilterFunc
|
||
|
}
|
||
|
|
||
3 years ago
|
func newRouter(prefix string, srv *Server, filters ...FilterFunc) *Router {
|
||
|
r := &Router{
|
||
4 years ago
|
prefix: prefix,
|
||
|
srv: srv,
|
||
|
filters: filters,
|
||
|
}
|
||
|
r.pool.New = func() interface{} {
|
||
3 years ago
|
return &wrapper{router: r}
|
||
4 years ago
|
}
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
// Handle registers a new route with a matcher for the URL path and method.
|
||
3 years ago
|
func (r *Router) Handle(method, relativePath string, h HandlerFunc, filters ...FilterFunc) {
|
||
4 years ago
|
next := http.Handler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||
|
ctx := r.pool.Get().(Context)
|
||
|
ctx.Reset(res, req)
|
||
|
if err := h(ctx); err != nil {
|
||
|
r.srv.ene(res, req, err)
|
||
|
}
|
||
|
ctx.Reset(nil, nil)
|
||
|
r.pool.Put(ctx)
|
||
|
}))
|
||
|
next = FilterChain(filters...)(next)
|
||
|
next = FilterChain(r.filters...)(next)
|
||
|
r.srv.router.Handle(path.Join(r.prefix, relativePath), next).Methods(method)
|
||
|
}
|
||
|
|
||
|
// GET registers a new GET route for a path with matching handler in the router.
|
||
3 years ago
|
func (r *Router) GET(path string, h HandlerFunc, m ...FilterFunc) {
|
||
4 years ago
|
r.Handle(http.MethodGet, path, h, m...)
|
||
|
}
|
||
|
|
||
|
// HEAD registers a new HEAD route for a path with matching handler in the router.
|
||
3 years ago
|
func (r *Router) HEAD(path string, h HandlerFunc, m ...FilterFunc) {
|
||
4 years ago
|
r.Handle(http.MethodHead, path, h, m...)
|
||
|
}
|
||
|
|
||
|
// POST registers a new POST route for a path with matching handler in the router.
|
||
3 years ago
|
func (r *Router) POST(path string, h HandlerFunc, m ...FilterFunc) {
|
||
4 years ago
|
r.Handle(http.MethodPost, path, h, m...)
|
||
|
}
|
||
|
|
||
|
// PUT registers a new PUT route for a path with matching handler in the router.
|
||
3 years ago
|
func (r *Router) PUT(path string, h HandlerFunc, m ...FilterFunc) {
|
||
4 years ago
|
r.Handle(http.MethodPut, path, h, m...)
|
||
|
}
|
||
|
|
||
|
// PATCH registers a new PATCH route for a path with matching handler in the router.
|
||
3 years ago
|
func (r *Router) PATCH(path string, h HandlerFunc, m ...FilterFunc) {
|
||
4 years ago
|
r.Handle(http.MethodPatch, path, h, m...)
|
||
|
}
|
||
|
|
||
|
// DELETE registers a new DELETE route for a path with matching handler in the router.
|
||
3 years ago
|
func (r *Router) DELETE(path string, h HandlerFunc, m ...FilterFunc) {
|
||
4 years ago
|
r.Handle(http.MethodDelete, path, h, m...)
|
||
|
}
|
||
|
|
||
|
// CONNECT registers a new CONNECT route for a path with matching handler in the router.
|
||
3 years ago
|
func (r *Router) CONNECT(path string, h HandlerFunc, m ...FilterFunc) {
|
||
4 years ago
|
r.Handle(http.MethodConnect, path, h, m...)
|
||
|
}
|
||
|
|
||
|
// OPTIONS registers a new OPTIONS route for a path with matching handler in the router.
|
||
3 years ago
|
func (r *Router) OPTIONS(path string, h HandlerFunc, m ...FilterFunc) {
|
||
4 years ago
|
r.Handle(http.MethodOptions, path, h, m...)
|
||
|
}
|
||
|
|
||
|
// TRACE registers a new TRACE route for a path with matching handler in the router.
|
||
3 years ago
|
func (r *Router) TRACE(path string, h HandlerFunc, m ...FilterFunc) {
|
||
4 years ago
|
r.Handle(http.MethodTrace, path, h, m...)
|
||
|
}
|