forked from celrenheit/lion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
48 lines (42 loc) · 919 Bytes
/
utils.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
package lion
import (
"fmt"
"net/http"
"path"
"strings"
)
func cleanPath(p string) string {
if p == "" {
return "/"
}
if p[0] != '/' {
p = "/" + p
}
newpath := path.Clean(p)
if newpath != "/" && p[len(p)-1] == '/' {
return newpath + "/"
}
return newpath
}
func panicl(format string, args ...interface{}) {
panic(fmt.Sprintf("lion: "+format, args...))
}
func reverseHostStdLib(pattern string) string {
reversed := strings.Split(pattern, ".")
for i, j := 0, len(reversed)-1; i < j; i, j = i+1, j-1 {
reversed[i], reversed[j] = reversed[j], reversed[i]
}
return strings.Join(reversed, ".")
}
func wrap(ctxHandler func(Context)) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
c := C(r)
ctxHandler(c)
}
return http.HandlerFunc(fn)
}
func unwrap(handler http.Handler) func(Context) {
return func(c Context) {
handler.ServeHTTP(c, c.Request().WithContext(c))
}
}