-
-
Notifications
You must be signed in to change notification settings - Fork 432
/
router.go
111 lines (92 loc) · 2.48 KB
/
router.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
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"os"
"sort"
"strings"
"sync"
"time"
fiberlogger "github.com/gofiber/fiber/v2/middleware/logger"
fiberrecover "github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2"
"github.com/mritd/logger"
)
type CommonResp struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
Timestamp int64 `json:"timestamp"`
}
type routerFunc struct {
Name string
Weight int
Func func(router fiber.Router)
}
type routeSlice []routerFunc
func (r routeSlice) Len() int { return len(r) }
func (r routeSlice) Less(i, j int) bool { return r[i].Weight > r[j].Weight }
func (r routeSlice) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
var routerOnce sync.Once
var routes routeSlice
// register new route with key name
// key name is used to eliminate duplicate routes
// key name not case sensitive
func registerRoute(name string, f func(router fiber.Router)) {
registerRouteWithWeight(name, 50, f)
}
// register new route with weight
func registerRouteWithWeight(name string, weight int, f func(router fiber.Router)) {
if weight > 100 || weight < 0 {
logger.Fatalf("route [%s] weight must be >= 0 and <=100", name)
}
for _, r := range routes {
if strings.EqualFold(name, r.Name) {
logger.Fatalf("route [%s] already registered", r.Name)
}
}
routes = append(routes, routerFunc{
Name: name,
Weight: weight,
Func: f,
})
}
func routerSetup(router fiber.Router) {
routerOnce.Do(func() {
router.Use(fiberlogger.New(fiberlogger.Config{
Format: "${time} INFO ${ip} -> [${status}] ${method} ${latency} ${route} => ${url} ${body}\n",
TimeFormat: "2006-01-02 15:04:05",
Output: os.Stdout,
}))
router.Use(fiberrecover.New())
sort.Sort(routes)
for _, r := range routes {
r.Func(router)
logger.Infof("load route [%s] success...", r.Name)
}
})
}
// for the fast return success result
func success() CommonResp {
return CommonResp{
Code: 200,
Message: "success",
Timestamp: time.Now().Unix(),
}
}
// for the fast return failed result
func failed(code int, message string, args ...interface{}) CommonResp {
return CommonResp{
Code: code,
Message: fmt.Sprintf(message, args...),
Timestamp: time.Now().Unix(),
}
}
// for the fast return result with custom data
func data(data interface{}) CommonResp {
return CommonResp{
Code: 200,
Message: "success",
Timestamp: time.Now().Unix(),
Data: data,
}
}