-
Notifications
You must be signed in to change notification settings - Fork 12
/
httprouter.wrap.go
33 lines (28 loc) · 1000 Bytes
/
httprouter.wrap.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
// Copyright 2020 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package http
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
// WrapHTTPRouterF is a helper function for wrapping httprouter.Handle
// Returns a http middleware
func WrapHTTPRouterF(f httprouter.Handle) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := httprouter.New()
any(h, "/*path", f)
h.ServeHTTP(w, r)
})
}
func any(r *httprouter.Router, path string, handle httprouter.Handle) {
r.Handle(http.MethodGet, path, handle)
r.Handle(http.MethodHead, path, handle)
r.Handle(http.MethodPost, path, handle)
r.Handle(http.MethodPut, path, handle)
r.Handle(http.MethodPatch, path, handle)
r.Handle(http.MethodDelete, path, handle)
r.Handle(http.MethodConnect, path, handle)
r.Handle(http.MethodOptions, path, handle)
r.Handle(http.MethodTrace, path, handle)
}