Skip to content

Instantly share code, notes, and snippets.

@LouisHrg
Created September 27, 2019 14:21
Show Gist options
  • Save LouisHrg/5a318179f40c3692b021bc5f2e527ae0 to your computer and use it in GitHub Desktop.
Save LouisHrg/5a318179f40c3692b021bc5f2e527ae0 to your computer and use it in GitHub Desktop.
package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-vote/provider"
"github.com/go-vote/model"
)
// IPFirewall : block banned IPs
func IPFirewall() gin.HandlerFunc {
var db = provider.GetDB()
var blocked model.Ip
return func(c *gin.Context) {
if !db.Where("address = ?", c.ClientIP()).Find(&blocked).RecordNotFound() {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"status": http.StatusForbidden,
"message": "Permission denied",
})
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment