https://github.com/webstradev/echo-pagination
Simple pagination middleware for the echo framework. Allows for the usage of url parameters like ?page=1&size=25 to paginate data on your API.
https://github.com/webstradev/echo-pagination
Last synced: 11 months ago
JSON representation
Simple pagination middleware for the echo framework. Allows for the usage of url parameters like ?page=1&size=25 to paginate data on your API.
- Host: GitHub
- URL: https://github.com/webstradev/echo-pagination
- Owner: webstradev
- License: apache-2.0
- Created: 2024-10-22T13:19:59.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-26T19:50:23.000Z (about 1 year ago)
- Last Synced: 2025-05-28T08:50:26.581Z (about 1 year ago)
- Language: Go
- Size: 38.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# echo-pagination
[](https://github.com/webstradev/echo-pagination/actions/workflows/test.yml)
[](https://codecov.io/gh/webstradev/echo-pagination)
[](https://pkg.go.dev/github.com/webstradev/echo-pagination)
[](https://goreportcard.com/report/github.com/webstradev/echo-pagination)
[](https://github.com/webstradev/echo-pagination/actions/workflows/codeql.yml)
Simple pagination middleware for the echo framework. Allows for the usage of url parameters like `?page=1&size=25` to paginate data on your API.
# Installation
```bash
$ go get github.com/webstradev/echo-pagination
```
# Default Usage
This package comes with various default options that are configurable using functional options. By default the paginator will use the query parameters `page` and `size` with values of `1` and `10` and a maximum page size of `100`.
### Using the middleware on a router will apply it to all requests on that router:
```go
package main
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/webstradev/echo-pagination/pkg/pagination"
)
func main(){
e := echo.New()
e.Use(pagination.New())
e.GET("/hello", func(c echo.Context) error {
return c.Status(http.StatusOK)
})
e.Logger.Fatal(e.Start(":1323"))
}
```
#### Using the middleware on a single route will only apply it to that route:
```go
package main
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/webstradev/echo-pagination/pkg/pagination"
)
func main(){
e := echo.New()
e.GET("/hello", func(c echo.Context){
page := c.GetInt("page")
return c.String(http.StatusOK, "Hello, World!")
}, pagination.New())
e.Logger.Fatal(e.Start(":1323"))
}
```
The `page` and `size` are now available in the echo context of a request and can be used to paginate your data (for example in an SQL query).
## Custom Usage
To create a pagination middleware with custom parameters the New() function supports various custom options provided as functions that overwrite the default value.
All the options can be seen in the example below.
```go
package main
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/webstradev/echo-pagination/pkg/pagination"
)
func main(){
e := echo.New()
paginator := pagination.New(
pagination.WithPageText("page"),
pagination.WithSizeText("rowsPerPage"),
pagination.WithDefaultPage(1),
pagination.WithDefaultPageSize(15),
pagination.WithMinPageSize(5),
pagination.WithMaxPageSize(15),
)
e.GET("/hello", func(c echo.Context){
return c.String(http.StatusOK, "Hello, World!")
}, paginator)
e.Logger.Fatal(e.Start(":1323"))
}
```
The custom middleware can also be used on an entire router object similarly to the first example fo the Default Usage.