2023-07-09 00:37:41 +00:00
|
|
|
package api
|
2021-02-25 03:41:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2021-07-15 02:26:51 +00:00
|
|
|
|
2022-12-06 06:41:09 +00:00
|
|
|
"github.com/TwiN/gatus/v5/storage/store/common"
|
2023-07-09 00:37:41 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-02-25 03:41:36 +00:00
|
|
|
)
|
|
|
|
|
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
|
2021-08-13 01:54:23 +00:00
|
|
|
MaximumPageSize = common.MaximumNumberOfResults
|
2021-02-25 04:26:13 +00:00
|
|
|
)
|
|
|
|
|
2023-07-09 00:37:41 +00:00
|
|
|
func extractPageAndPageSizeFromRequest(c *fiber.Ctx) (page, pageSize int) {
|
2021-02-25 03:41:36 +00:00
|
|
|
var err error
|
2023-07-09 00:37:41 +00:00
|
|
|
if pageParameter := c.Query("page"); len(pageParameter) == 0 {
|
2021-02-25 04:26:13 +00:00
|
|
|
page = DefaultPage
|
2021-02-25 03:41:36 +00:00
|
|
|
} else {
|
|
|
|
page, err = strconv.Atoi(pageParameter)
|
|
|
|
if err != nil {
|
2021-02-25 04:26:13 +00:00
|
|
|
page = DefaultPage
|
|
|
|
}
|
|
|
|
if page < 1 {
|
|
|
|
page = DefaultPage
|
2021-02-25 03:41:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-09 00:37:41 +00:00
|
|
|
if pageSizeParameter := c.Query("pageSize"); len(pageSizeParameter) == 0 {
|
2021-02-25 04:26:13 +00:00
|
|
|
pageSize = DefaultPageSize
|
2021-02-25 03:41:36 +00:00
|
|
|
} else {
|
|
|
|
pageSize, err = strconv.Atoi(pageSizeParameter)
|
|
|
|
if err != nil {
|
2021-02-25 04:26:13 +00:00
|
|
|
pageSize = DefaultPageSize
|
2021-02-25 03:41:36 +00:00
|
|
|
}
|
2021-02-25 04:26:13 +00:00
|
|
|
if pageSize > MaximumPageSize {
|
|
|
|
pageSize = MaximumPageSize
|
|
|
|
} else if pageSize < 1 {
|
|
|
|
pageSize = DefaultPageSize
|
2021-02-25 03:41:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|