Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/varthanv/gin-idempotency
An idempotency key middleware for Go gin framework with pluggable configurations
https://github.com/varthanv/gin-idempotency
gin golang idempotency middleware
Last synced: about 2 months ago
JSON representation
An idempotency key middleware for Go gin framework with pluggable configurations
- Host: GitHub
- URL: https://github.com/varthanv/gin-idempotency
- Owner: VarthanV
- Created: 2023-02-04T14:59:22.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-05T03:00:48.000Z (almost 2 years ago)
- Last Synced: 2024-06-21T10:27:06.451Z (6 months ago)
- Topics: gin, golang, idempotency, middleware
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gin-idempotency
## Introduction
gin-idempotency is a middleware for the [Gin](https://gin-gonic.com/) webframework. It checks whether the Idempotency key is passed in the headersAn idempotency key is a unique value generated by you. Our servers use this key to recognize subsequent retries of the same request.
It is used in places where we need to prevent the impact recognize subsequent retries of the same request. It is mainly used in payment/transaction API's. More about idempotency
- [Stripe Reference](http://bit.ly/3WYB3Wc)
- [Razorpay Reference](http://bit.ly/3latbDV)## Usage
Download and install it using go get
```
go get github.com/VarthanV/gin-idempotency
```Import it in your code
```golang
import "github.com/VarthanV/gin-idempotency"```
## Examples
**using default config**
```golang
package mainimport (
"github.com/Varthan/idempotency"
"github.com/gin-gonic/gin"
)func main() {
r := gin.Default()
r.Use(idempotency.New(idempotency.Default()))r.POST("/transfer", func(c *gin.Context) {
var existingIdempotentKey = "foo"
// The value in the header will parsed and will be set in the context with the following key name by default
var idempotencyKeyFromCtx = c.GetString("IdempotencyKey")
if idempotencyKeyFromCtx == existingIdempotentKey {
c.JSON(403, gin.H{"message":"send an new idempotency key"})
return
}
})
r.Run(":8000")
}
```**using custom config**
```golang
package mainimport (
"github.com/Varthan/idempotency"
"github.com/gin-gonic/gin"
)func main() {
r := gin.Default()
r.Use(idempotency.New(idempotency.IdempotencyConfig{
/* The middleware by default looks for the header with the key name ``Idempotency-Key``*/
HeaderName: "foo" ,/* The value in the header will parsed and will be set in the context with the key name IdempotencyKey in the gin context by default, You can customise it based on your needs by the ContextKeyName param */
ContextKeyName: "foo-ctx" ,/*
The httpStatusCode which you want to return incase the idempotencykey is not present default is 403
*/
StatusCode: 418 ,
/*
Response payload which you want to send to the client when the key is not present. Default response is {"error":"${{HeaderName}} is missing"}
*/
Response: map[string]string{"message":"idempotency-key-doesnt-exist"},
/*
Whitelist certain HTTP methods based on your needs
*/
WhitelistHTTPMethods: []string{"GET"}
}))r.POST("/fund-transfer", func(c *gin.Context) {
var existingIdempotentKey = "foo"
// The value in the header will parsed and will be set in the context with the following key name by default
var idempotencyKeyFromCtx = c.GetString("IdempotencyKey")
if idempotencyKeyFromCtx == existingIdempotentKey {
c.JSON(403, gin.H{"message":"send an new idempotency key"})
return
}
})
r.Run(":8000")
}
```