1
0
Fork 0
mirror of https://github.com/TwiN/gatus.git synced 2024-12-15 17:51:09 +00:00
twin-gatus/controller/util.go

47 lines
1.1 KiB
Go
Raw Normal View History

package controller
import (
"net/http"
"strconv"
)
2021-02-25 04:26:13 +00:00
const (
// DefaultPage is the default page to use if none is specified or an invalid value is provided
DefaultPage = 1
// DefaultPageSize is the default page siZE to use if none is specified or an invalid value is provided
DefaultPageSize = 20
// MaximumPageSize is the maximum page size allowed
MaximumPageSize = 100
)
func extractPageAndPageSizeFromRequest(r *http.Request) (page int, pageSize int) {
var err error
if pageParameter := r.URL.Query().Get("page"); len(pageParameter) == 0 {
2021-02-25 04:26:13 +00:00
page = DefaultPage
} else {
page, err = strconv.Atoi(pageParameter)
if err != nil {
2021-02-25 04:26:13 +00:00
page = DefaultPage
}
if page < 1 {
page = DefaultPage
}
}
if pageSizeParameter := r.URL.Query().Get("pageSize"); len(pageSizeParameter) == 0 {
2021-02-25 04:26:13 +00:00
pageSize = DefaultPageSize
} else {
pageSize, err = strconv.Atoi(pageSizeParameter)
if err != nil {
2021-02-25 04:26:13 +00:00
pageSize = DefaultPageSize
}
2021-02-25 04:26:13 +00:00
if pageSize > MaximumPageSize {
pageSize = MaximumPageSize
} else if pageSize < 1 {
pageSize = DefaultPageSize
}
}
return
}