forked from eolinker/apinto-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.go
41 lines (35 loc) · 851 Bytes
/
module.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
package apinto_dashboard
import (
"net/http"
"strings"
)
type ViewLookup interface {
Lookup(r *http.Request) (view string, data interface{}, has bool)
}
type IModule interface {
http.Handler
ViewLookup
}
type ModuleViewFinder struct {
views map[string]string
prefix string
defaultName string
}
func NewViewModuleEmpty(prefix string, views map[string]string, defaultName string) *ModuleViewFinder {
prefix = strings.TrimSuffix(prefix, "/")
return &ModuleViewFinder{
views: views,
prefix: prefix,
defaultName: defaultName,
}
}
func (v *ModuleViewFinder) Lookup(r *http.Request) (view string, has bool) {
path := r.URL.Path
name := strings.TrimPrefix(path, v.prefix)
if name == "" || name == "/" {
name = v.defaultName
}
name = strings.TrimPrefix(name, "/")
vn, has := v.views[name]
return vn, has
}