-
Notifications
You must be signed in to change notification settings - Fork 40
/
http.go
115 lines (97 loc) · 2.86 KB
/
http.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
112
113
114
115
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package http
import (
"net/http"
"strconv"
"github.com/polarismesh/polaris-console/common/log"
)
const (
// QueryDefaultOffset default query offset
QueryDefaultOffset = 0
// QueryDefaultLimit default query limit
QueryDefaultLimit = 100
// QueryMaxLimit default query max
QueryMaxLimit = 100
// MaxBatchSize max batch size
MaxBatchSize = 100
// MaxQuerySize max query size
MaxQuerySize = 100
)
// ParseQueryParams 解析并获取HTTP的query params
func ParseQueryParams(req *http.Request) map[string]string {
queryParams := make(map[string]string)
for key, value := range req.URL.Query() {
if len(value) > 0 {
queryParams[key] = value[0] // 暂时默认只支持一个查询
}
}
return queryParams
}
// ParseWildName 判断是否为格式化查询条件并且返回真正的查询信息
func ParseWildName(name string) (string, bool) {
length := len(name)
ok := length >= 1 && name[length-1:length] == "*"
if ok {
return name[:len(name)-1], ok
}
return name, false
}
// ParseOffsetAndLimit 统一格式化处理Offset和limit参数
func ParseOffsetAndLimit(query map[string]string) (uint32, uint32, error) {
ofs, err := ParseQueryOffset(query["offset"])
if err != nil {
return 0, 0, err
}
delete(query, "offset")
var lmt uint32
lmt, err = ParseQueryLimit(query["limit"])
if err != nil {
return 0, 0, err
}
delete(query, "limit")
return ofs, lmt, nil
}
// ParseQueryOffset 格式化处理offset参数
func ParseQueryOffset(offset string) (uint32, error) {
if offset == "" {
return QueryDefaultOffset, nil
}
tmp, err := strconv.ParseUint(offset, 10, 32)
if err != nil {
log.Errorf("[Server][Query] attribute(offset:%s) is invalid, parse err: %s",
offset, err.Error())
return 0, err
}
return uint32(tmp), nil
}
// ParseQueryLimit 格式化处理limit参数
func ParseQueryLimit(limit string) (uint32, error) {
if limit == "" {
return QueryDefaultLimit, nil
}
tmp, err := strconv.ParseUint(limit, 10, 32)
if err != nil {
log.Errorf("[Server][Query] attribute(offset:%s) is invalid, parse err: %s",
limit, err.Error())
return 0, err
}
if tmp > QueryMaxLimit {
tmp = QueryMaxLimit
}
return uint32(tmp), nil
}