-
Notifications
You must be signed in to change notification settings - Fork 13
/
register.go
52 lines (43 loc) · 989 Bytes
/
register.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
package admin
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/eolinker/eosc"
)
var (
ErrorDuplicatePath = errors.New("path duplicate")
ErrorNotExist = errors.New("not exits")
ErrorNotMatch = errors.New("not match profession")
)
type CreateHandler interface {
Create(admin eosc.IAdmin, pref string) http.Handler
}
var (
creatorHandler = make(map[string]CreateHandler)
)
func Register(myPre string, handler CreateHandler) error {
pre := formatPath(myPre)
_, has := creatorHandler[pre]
if has {
return ErrorDuplicatePath
}
creatorHandler[pre] = handler
return nil
}
func load(admin eosc.IAdmin) http.Handler {
mx := http.NewServeMux()
//pre := formatPath(prefix)
for p, h := range creatorHandler {
hs := h.Create(admin, p)
if hs != nil {
mx.Handle(p, hs)
mx.Handle(fmt.Sprintf("%s/", p), hs)
}
}
return mx
}
func formatPath(pre string) string {
return fmt.Sprintf("/%s", strings.TrimSuffix(strings.TrimPrefix(pre, "/"), "/"))
}