-
Notifications
You must be signed in to change notification settings - Fork 11
/
helper.go
102 lines (93 loc) · 1.86 KB
/
helper.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package echo
import (
"mime"
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/webx-top/com"
)
// HandlerName returns the handler name
func HandlerName(h interface{}) string {
v := reflect.ValueOf(h)
t := v.Type()
if t.Kind() == reflect.Func {
return runtime.FuncForPC(v.Pointer()).Name()
}
return t.String()
}
// HandlerPath returns the handler path
func HandlerPath(h interface{}) string {
v := reflect.ValueOf(h)
t := v.Type()
switch t.Kind() {
case reflect.Func:
return runtime.FuncForPC(v.Pointer()).Name()
case reflect.Ptr:
t = t.Elem()
fallthrough
case reflect.Struct:
return t.PkgPath() + `.` + t.Name()
}
return ``
}
func HandlerTmpl(handlerPath string) string {
name := path.Base(handlerPath)
var r []string
var u []rune
for _, b := range name {
switch b {
case '*', '(', ')':
continue
case '-':
goto END
case '.':
r = append(r, string(u))
u = []rune{}
default:
u = append(u, b)
}
}
END:
if len(u) > 0 {
r = append(r, string(u))
u = []rune{}
}
for i, s := range r {
r[i] = com.SnakeCase(s)
}
return `/` + strings.Join(r, `/`)
}
// Methods returns methods
func Methods() []string {
return methods
}
// ContentTypeByExtension returns the MIME type associated with the file based on
// its extension. It returns `application/octet-stream` incase MIME type is not
// found.
func ContentTypeByExtension(name string) (t string) {
if t = mime.TypeByExtension(filepath.Ext(name)); len(t) == 0 {
t = MIMEOctetStream
}
return
}
func static(r RouteRegister, prefix, root string) {
var err error
root, err = filepath.Abs(root)
if err != nil {
panic(err)
}
h := func(c Context) error {
name := filepath.Join(root, c.Param("*"))
if !strings.HasPrefix(name, root) {
return ErrNotFound
}
return c.File(name)
}
if prefix == "/" {
r.Get(prefix+"*", h)
} else {
r.Get(prefix+"/*", h)
}
}