Add rate limitting

This commit is contained in:
Manu Mtz-Almeida 2015-05-13 03:19:44 +02:00
parent 53d29b14f6
commit 313d05ed68
2 changed files with 18 additions and 1 deletions

View File

@ -0,0 +1,15 @@
package main
import "github.com/gin-gonic/gin"
import "github.com/manucorporat/stats"
var ips = stats.New()
func ratelimit(c *gin.Context) {
ip := c.ClientIP()
value := ips.Add(ip, 1)
if value > 1000 {
c.AbortWithStatus(401)
}
}

View File

@ -8,7 +8,9 @@ import (
) )
func main() { func main() {
router := gin.Default() router := gin.New()
router.Use(ratelimit, gin.Recovery(), gin.Logger())
router.LoadHTMLGlob("resources/*.templ.html") router.LoadHTMLGlob("resources/*.templ.html")
router.Static("/static", "resources/static") router.Static("/static", "resources/static")
router.GET("/", index) router.GET("/", index)