IP access control middleware for Gin, echo and fiber web frameworks. This middleware enhances your application's security by allowing you to define a whitelist of permitted IP addresses while providing extended features for handling non-whitelisted IPs.
To install GoHealthWatch, use the following command:
go get github.com/pcpratheesh/ip-guard-middleware/
Import the package
import (
guard "github.com/pcpratheesh/ip-guard-middleware/middleware/gin"
)
Initializing the options
var opts = []options.Options{
options.WithWhiteListIPs([]string{
"your-ip",
}),
}
Custom fallback handler function
var fallbackHandler = func(ctx *gin.Context, clientIP string) {
ctx.JSON(http.StatusForbidden, map[string]string{
"message": "Not allowed to access.",
})
ctx.Abort()
return
}
...
options.SetFallbackHandler(
options.GinFallbackHandler(fallbackHandler),
)