SetLinkHeader sets pagination link header by given total number and page size.
(total, pageSize int)
| 71 | |
| 72 | // SetLinkHeader sets pagination link header by given total number and page size. |
| 73 | func (c *APIContext) SetLinkHeader(total, pageSize int) { |
| 74 | page := paginater.New(total, pageSize, c.QueryInt("page"), 0) |
| 75 | links := make([]string, 0, 4) |
| 76 | if page.HasNext() { |
| 77 | links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", conf.Server.ExternalURL, c.Req.URL.Path[1:], page.Next())) |
| 78 | } |
| 79 | if !page.IsLast() { |
| 80 | links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", conf.Server.ExternalURL, c.Req.URL.Path[1:], page.TotalPages())) |
| 81 | } |
| 82 | if !page.IsFirst() { |
| 83 | links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", conf.Server.ExternalURL, c.Req.URL.Path[1:])) |
| 84 | } |
| 85 | if page.HasPrevious() { |
| 86 | links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", conf.Server.ExternalURL, c.Req.URL.Path[1:], page.Previous())) |
| 87 | } |
| 88 | |
| 89 | if len(links) > 0 { |
| 90 | c.Header().Set("Link", strings.Join(links, ",")) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func APIContexter() macaron.Handler { |
| 95 | return func(ctx *Context) { |
no test coverage detected