2019-09-02 09:38:04 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2024-02-19 14:44:03 +00:00
|
|
|
"slices"
|
2019-09-02 09:38:04 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultPerPage = 100
|
|
|
|
defaultPage = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
const nextPageHeader = "X-Next-Page"
|
|
|
|
|
|
|
|
type pageInfo struct {
|
|
|
|
startIndex int
|
|
|
|
endIndex int
|
|
|
|
nextPage int
|
|
|
|
}
|
|
|
|
|
|
|
|
type searchCriterion struct {
|
2023-01-09 16:24:05 +00:00
|
|
|
Search string `url:"search"`
|
|
|
|
Status string `url:"status"`
|
|
|
|
ServiceName string `url:"serviceName"`
|
|
|
|
MiddlewareName string `url:"middlewareName"`
|
2019-09-02 09:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSearchCriterion(query url.Values) *searchCriterion {
|
|
|
|
if len(query) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
search := query.Get("search")
|
|
|
|
status := query.Get("status")
|
2023-01-09 16:24:05 +00:00
|
|
|
serviceName := query.Get("serviceName")
|
|
|
|
middlewareName := query.Get("middlewareName")
|
2019-09-02 09:38:04 +00:00
|
|
|
|
2023-01-09 16:24:05 +00:00
|
|
|
if status == "" && search == "" && serviceName == "" && middlewareName == "" {
|
2019-09-02 09:38:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-09 16:24:05 +00:00
|
|
|
return &searchCriterion{
|
|
|
|
Search: search,
|
|
|
|
Status: status,
|
|
|
|
ServiceName: serviceName,
|
|
|
|
MiddlewareName: middlewareName,
|
|
|
|
}
|
2019-09-02 09:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchCriterion) withStatus(name string) bool {
|
|
|
|
return c.Status == "" || strings.EqualFold(name, c.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchCriterion) searchIn(values ...string) bool {
|
|
|
|
if c.Search == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-02-19 14:44:03 +00:00
|
|
|
return slices.ContainsFunc(values, func(v string) bool {
|
|
|
|
return strings.Contains(strings.ToLower(v), strings.ToLower(c.Search))
|
|
|
|
})
|
2023-01-09 16:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchCriterion) filterService(name string) bool {
|
|
|
|
if c.ServiceName == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(name, "@") {
|
|
|
|
return c.ServiceName == name
|
|
|
|
}
|
|
|
|
|
|
|
|
before, _, _ := strings.Cut(c.ServiceName, "@")
|
|
|
|
|
|
|
|
return before == name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *searchCriterion) filterMiddleware(mns []string) bool {
|
|
|
|
if c.MiddlewareName == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mn := range mns {
|
|
|
|
if c.MiddlewareName == mn {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 09:38:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-08-28 13:00:06 +00:00
|
|
|
func pagination(request *http.Request, maximum int) (pageInfo, error) {
|
2019-09-02 09:38:04 +00:00
|
|
|
perPage, err := getIntParam(request, "per_page", defaultPerPage)
|
|
|
|
if err != nil {
|
|
|
|
return pageInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
page, err := getIntParam(request, "page", defaultPage)
|
|
|
|
if err != nil {
|
|
|
|
return pageInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
startIndex := (page - 1) * perPage
|
2024-08-28 13:00:06 +00:00
|
|
|
if startIndex != 0 && startIndex >= maximum {
|
2019-09-02 09:38:04 +00:00
|
|
|
return pageInfo{}, fmt.Errorf("invalid request: page: %d, per_page: %d", page, perPage)
|
|
|
|
}
|
|
|
|
|
|
|
|
endIndex := startIndex + perPage
|
2024-08-28 13:00:06 +00:00
|
|
|
if endIndex >= maximum {
|
|
|
|
endIndex = maximum
|
2019-09-02 09:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nextPage := 1
|
2024-08-28 13:00:06 +00:00
|
|
|
if page*perPage < maximum {
|
2019-09-02 09:38:04 +00:00
|
|
|
nextPage = page + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return pageInfo{startIndex: startIndex, endIndex: endIndex, nextPage: nextPage}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIntParam(request *http.Request, key string, defaultValue int) (int, error) {
|
|
|
|
raw := request.URL.Query().Get(key)
|
|
|
|
if raw == "" {
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := strconv.Atoi(raw)
|
|
|
|
if err != nil || value <= 0 {
|
|
|
|
return 0, fmt.Errorf("invalid request: %s: %d", key, value)
|
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|