Пагинация cursor/limit для CDN, AS, domain и IP range list; ответы с next_cursor и has_more по OpenAPI. Добавлен writePaginatedListJSON и тест. Co-authored-by: Cursor <[email protected]>
39 lines
892 B
Go
39 lines
892 B
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"evobgp/internal/store"
|
|
)
|
|
|
|
func parseListLimit(r *http.Request) int {
|
|
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
|
if limit <= 0 {
|
|
return 50
|
|
}
|
|
if limit > 500 {
|
|
return 500
|
|
}
|
|
return limit
|
|
}
|
|
|
|
func strPtrOrNull(s string) any {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
return s
|
|
}
|
|
|
|
// writePaginatedListJSON returns a cursor/limit page as OpenAPI list envelopes (items, next_cursor, has_more).
|
|
func writePaginatedListJSON[T any](w http.ResponseWriter, r *http.Request, all []T, toItem func(T) map[string]any) {
|
|
page, next, more := store.PaginateOffset(all, r.URL.Query().Get("cursor"), parseListLimit(r))
|
|
items := make([]map[string]any, 0, len(page))
|
|
for _, x := range page {
|
|
items = append(items, toItem(x))
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"items": items, "next_cursor": strPtrOrNull(next), "has_more": more,
|
|
})
|
|
}
|