package server import ( "net/url" "strconv" ) // Backend pagination shared by the list endpoints: `page` (1-based) and // `per_page` (default 25, at most 100). Out-of-range values are clamped and a // page past the end is an empty slice with the true total. const ( defaultPerPage = 25 maxPerPage = 100 maxPageNo = 100000 ) // pageParams reads page and per_page ("per" is the older spelling the IP list // used). Missing, zero or negative values fall back to page 1 / 25 per page. func pageParams(q url.Values) (page, per int) { page, _ = strconv.Atoi(q.Get("page")) page = min(max(page, 1), maxPageNo) v := q.Get("per_page") if v == "" { v = q.Get("per") } per, _ = strconv.Atoi(v) if per < 1 { per = defaultPerPage } return page, min(per, maxPerPage) } // pageMeta is the metadata block every paginated response carries. type pageMeta struct { Page int `json:"page"` PerPage int `json:"per_page"` Pages int `json:"pages"` } // pageTotal adds the exact (or, for the event log, estimated) item count. type pageTotal struct { pageMeta Total int `json:"total"` } func metaOf(total, page, per int) pageMeta { return pageMeta{Page: page, PerPage: per, Pages: max(1, (total+per-1)/per)} } // window returns the bounds of the page inside a list of n items. func window(n, page, per int) (from, to int) { from = min((page-1)*per, n) return from, min(from+per, n) }