-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
85 lines (75 loc) · 1.62 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
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"net/url"
"strconv"
"strings"
)
type RouterConfig struct {
Host string
Port int
}
type Route struct {
Path string
Method string
handle httprouter.Handle
}
type Handler interface {
GetRoutes() []Route
}
func newRouter(handlers ...Handler) http.Handler {
router := httprouter.New()
for _, h := range handlers {
for _, r := range h.GetRoutes() {
router.Handle(r.Method, r.Path, r.handle)
}
}
return router
}
type QueryParameters struct {
values url.Values
}
func (qp QueryParameters) GetString(key string) string {
return strings.TrimSpace(qp.values.Get(key))
}
func (qp QueryParameters) GetStringSlice(key string) []string {
var ss []string
if qp.values == nil {
return ss
}
for _, v := range qp.values[key] {
s := strings.TrimSpace(v)
if len(s) > 0 {
ss = append(ss, s)
}
}
return ss
}
func (qp QueryParameters) GetInt(key string) (int, error) {
s := qp.GetString(key)
if len(s) == 0 {
return 0, nil
}
i, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("QueryParameters.GetInt: %w", err)
}
return i, nil
}
func (qp QueryParameters) GetSortBy(key string) (string, SortOrder, error) {
s := qp.GetString(key)
if len(s) == 0 {
return "", OrderUndefined, nil
}
ss := strings.Split(s, ".")
if len(ss) != 2 || len(ss[0]) == 0 || (ss[1] != "asc" && ss[1] != "desc") {
return "", OrderUndefined, fmt.Errorf("QueryParameters.GetSortBy: invalid format: expect \"{field name}.{asc}/{desc}\"")
}
sortOrder := OrderAscending
if ss[1] == "desc" {
sortOrder = OrderDescending
}
return ss[0], sortOrder, nil
}