summaryrefslogtreecommitdiffstats
path: root/modules/context/api.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/context/api.go')
-rw-r--r--modules/context/api.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/modules/context/api.go b/modules/context/api.go
index 44232d71a6..8d036be9c0 100644
--- a/modules/context/api.go
+++ b/modules/context/api.go
@@ -5,10 +5,15 @@
package context
import (
+ "fmt"
+ "strings"
+
+ "github.com/Unknwon/paginater"
"gopkg.in/macaron.v1"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
+ "github.com/gogits/gogs/modules/setting"
)
type APIContext struct {
@@ -35,6 +40,27 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
})
}
+func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
+ page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0)
+ links := make([]string, 0, 4)
+ if page.HasNext() {
+ links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppUrl, ctx.Req.URL.Path[1:], page.Next()))
+ }
+ if !page.IsLast() {
+ links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppUrl, ctx.Req.URL.Path[1:], page.TotalPages()))
+ }
+ if !page.IsFirst() {
+ links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppUrl, ctx.Req.URL.Path[1:]))
+ }
+ if page.HasPrevious() {
+ links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppUrl, ctx.Req.URL.Path[1:], page.Previous()))
+ }
+
+ if len(links) > 0 {
+ ctx.Header().Set("Link", strings.Join(links, ","))
+ }
+}
+
func APIContexter() macaron.Handler {
return func(c *Context) {
ctx := &APIContext{