pagination

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2024 License: Apache-2.0 Imports: 3 Imported by: 2

README

gin-pagination

Run Tests codecov Go Reference Go Report Card CodeQL

Simple pagination middleware for the gin framework. Allows for the usage of url parameters like ?page=1&size=25 to paginate data on your API.

Installation

$ go get github.com/webstradev/gin-pagination

Default Usage

This package comes with a default pagination handler. This uses query parameters page and size with default values of 1 and 10 and a maximum page size of 100.

Using the middleware on a router will apply the it to all request on that router:
package main

import (
  "net/http"

  "github.com/gin-gonic/gin"
  "github.com/webstradev/gin-pagination"
)

func main(){
  r := gin.Default()
  
  r.Use(pagination.Default())
  
  r.GET("/hello", func(c *gin.Context){
    c.Status(http.StatusOK)  
  })
  
  r.Run(":3000")
}
Using the middleware on a single route will only apply it to that route:
package main

import (
  "net/http"
  
  "github.com/gin-gonic/gin"
  "github.com/webstradev/gin-pagination"
)

func main(){
  r := gin.Default()
  
  r.GET("/hello", pagination.Default(), func(c *gin.Context){
    page := c.GetInt("page")
  
    c.JSON(http.StatusOK, gin.H{"page":page})  
  })
  
  r.Run(":3000")
}

The page and size are now available in the gin context of a request and can be used to paginate your date (for example in an SQL query).

Custom Usage

To create a pagination middleware with custom parameters use the pagination.New() function.

package main

import (
  "net/http"
  
  "github.com/gin-gonic/gin"
  "github.com/webstradev/gin-pagination"
)

func main(){
  r := gin.Default()
  
  paginator := pagination.New("page", "rowsPerPage", "1", "15", 5, 150)
  
  r.GET("/hello", paginator, func(c *gin.Context){
    c.Status(http.StatusOK)  
  })
  
  r.Run(":3000")
}

The custom middleware can also be used on an entire router object similarly to the first example fo the Default Usage.

Documentation

Index

Constants

View Source
const (
	DEFAULT_PAGE_TEXT    = "page"
	DEFAULT_SIZE_TEXT    = "size"
	DEFAULT_PAGE         = "1"
	DEFAULT_PAGE_SIZE    = "10"
	DEFAULT_MIN_PAGESIZE = 10
	DEFAULT_MAX_PAGESIZE = 100
)

Variables

This section is empty.

Functions

func Default

func Default() gin.HandlerFunc

Create a new pagination middleware with default values

func New

func New(pageText, sizeText, defaultPage, defaultPageSize string, minPageSize, maxPageSize int) gin.HandlerFunc

Create a new pagniation middleware with custom values

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL