/
paging.go
71 lines (60 loc) · 1.75 KB
/
paging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package sgul
import (
"context"
"errors"
"net/http"
"strconv"
"github.com/go-chi/chi/middleware"
)
// Page defines the struct with paging info to send into the request context.
type Page struct {
Page int
Size int
}
type ctxPKey int
const ctxPageKey ctxPKey = iota + 1
// ErrPagerNotInContext is returned if there is no Pager in the request context.
var ErrPagerNotInContext = errors.New("Pager info not in Context")
func pager(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Query().Get("page")
s := r.URL.Query().Get("size")
if p != "" && s != "" {
var pVal int
var sVal int
var err error
pVal, err = strconv.Atoi(p)
if err != nil {
RenderError(w, NewHTTPError(err, http.StatusBadRequest, "Malformed 'page' param", middleware.GetReqID(r.Context())))
return
}
sVal, err = strconv.Atoi(s)
if err != nil {
RenderError(w, NewHTTPError(err, http.StatusBadRequest, "Malformed 'size' param", middleware.GetReqID(r.Context())))
return
}
page := Page{Page: pVal, Size: sVal}
ctx := context.WithValue(r.Context(), ctxPageKey, page)
next.ServeHTTP(w, r.WithContext(ctx))
} else {
next.ServeHTTP(w, r)
}
}
}
// Pager is the query paging middleware.
func Pager() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(pager(next))
}
}
// RoutePager is the query paging middleware to be used on routes.
func RoutePager(next http.HandlerFunc) http.HandlerFunc {
return pager(next)
}
// GetPage return the pager struct from request Context.
func GetPage(ctx context.Context) (Page, error) {
if page, ok := ctx.Value(ctxPageKey).(Page); ok {
return page, nil
}
return Page{}, ErrPagerNotInContext
}