-
Notifications
You must be signed in to change notification settings - Fork 11
/
helper.go
54 lines (49 loc) · 1.06 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
package echo
import (
"mime"
"path/filepath"
"reflect"
"runtime"
"strings"
)
// 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()
}
// 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)
}
}