2021-09-13 00:39:09 +02:00
|
|
|
package handler
|
2021-02-25 04:41:36 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2021-07-15 04:26:51 +02:00
|
|
|
|
2022-06-21 03:25:14 +02:00
|
|
|
"github.com/TwiN/gatus/v4/storage/store/common"
|
2021-02-25 04:41:36 +01:00
|
|
|
)
|
|
|
|
|
2021-02-25 05:26:13 +01: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
|
2021-08-13 03:54:23 +02:00
|
|
|
MaximumPageSize = common.MaximumNumberOfResults
|
2021-02-25 05:26:13 +01:00
|
|
|
)
|
|
|
|
|
2021-02-25 04:41:36 +01:00
|
|
|
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 05:26:13 +01:00
|
|
|
page = DefaultPage
|
2021-02-25 04:41:36 +01:00
|
|
|
} else {
|
|
|
|
page, err = strconv.Atoi(pageParameter)
|
|
|
|
if err != nil {
|
2021-02-25 05:26:13 +01:00
|
|
|
page = DefaultPage
|
|
|
|
}
|
|
|
|
if page < 1 {
|
|
|
|
page = DefaultPage
|
2021-02-25 04:41:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if pageSizeParameter := r.URL.Query().Get("pageSize"); len(pageSizeParameter) == 0 {
|
2021-02-25 05:26:13 +01:00
|
|
|
pageSize = DefaultPageSize
|
2021-02-25 04:41:36 +01:00
|
|
|
} else {
|
|
|
|
pageSize, err = strconv.Atoi(pageSizeParameter)
|
|
|
|
if err != nil {
|
2021-02-25 05:26:13 +01:00
|
|
|
pageSize = DefaultPageSize
|
2021-02-25 04:41:36 +01:00
|
|
|
}
|
2021-02-25 05:26:13 +01:00
|
|
|
if pageSize > MaximumPageSize {
|
|
|
|
pageSize = MaximumPageSize
|
|
|
|
} else if pageSize < 1 {
|
|
|
|
pageSize = DefaultPageSize
|
2021-02-25 04:41:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|