-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
55 lines (44 loc) · 1 KB
/
api.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
package sapi
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
var (
gRouter = mux.NewRouter()
Methods = []string{GET, DELETE, POST, UPDATE}
)
type ApiEndpoints map[string]map[string]http.Handler
type Apier interface {
Mapper() ApiEndpoints
}
type ApiMapperFunc func() ApiEndpoints
func (f ApiMapperFunc) Mapper() ApiEndpoints {
return f()
}
func Regist(apiers ...Apier) {
for _, apier := range apiers {
mapping := apier.Mapper()
for method, routeMap := range mapping {
for route, handler := range routeMap {
gRouter.Handle(route, handler).Methods(method)
}
}
}
}
func MakeApiEndpoints(method, route string, f http.Handler) ApiMapperFunc {
return func() ApiEndpoints {
endpoints := make(ApiEndpoints)
endpoints[method] = map[string]http.Handler{route: f}
return endpoints
}
}
func Router() *mux.Router {
return gRouter
}
func HttpError(rw http.ResponseWriter, err string, e error, code int) {
http.Error(rw, err, code)
if e != nil {
Log().Error(fmt.Sprintf("%s", e))
}
}