fix: add version, rolling ratelimit

This commit is contained in:
httpjamesm 2022-12-29 13:54:37 -05:00
parent 0e9677ec61
commit c46820b6d4
3 changed files with 22 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package middleware
import (
"anonymousoverflow/config"
"sync"
"time"
@ -34,14 +35,23 @@ func Ratelimit() gin.HandlerFunc {
c.HTML(429, "home.html", gin.H{
"errorMessage": "You have exceeded the request limit. Please try again in a minute.",
"theme": c.MustGet("theme").(string),
"version": config.Version,
})
c.Abort()
return
}
// delete the ip from the map after 1 minute
// subtract 1 from the value after 1 minute if the value exists and is greater than 0
time.AfterFunc(time.Minute, func() {
ipMap.Delete(ip)
val, ok := ipMap.Load(ip)
if ok && val.(int) > 0 {
ipMap.Store(ip, val.(int)-1)
}
// if the value is 0, delete the entry from the map
if val.(int) == 0 {
ipMap.Delete(ip)
}
})
}
}

View File

@ -1,6 +1,7 @@
package routes
import (
"anonymousoverflow/config"
"fmt"
"os"
"strings"
@ -21,6 +22,7 @@ func ChangeOptions(c *gin.Context) {
c.HTML(200, "home.html", gin.H{
"successMessage": "Images are now " + text,
"theme": c.MustGet("theme").(string),
"version": config.Version,
})
case "theme":
@ -40,9 +42,8 @@ func ChangeOptions(c *gin.Context) {
}
c.Redirect(302, redirectUrl)
default:
c.String(400, "400 Bad Request")
}
c.Redirect(302, "/")
}

View File

@ -1,6 +1,7 @@
package routes
import (
"anonymousoverflow/config"
"anonymousoverflow/src/utils"
"fmt"
"html"
@ -28,6 +29,7 @@ func ViewQuestion(c *gin.Context) {
c.HTML(400, "home.html", gin.H{
"errorMessage": "Invalid question ID",
"theme": c.MustGet("theme").(string),
"version": config.Version,
})
return
}
@ -36,6 +38,7 @@ func ViewQuestion(c *gin.Context) {
c.HTML(400, "home.html", gin.H{
"errorMessage": "Invalid question ID",
"theme": c.MustGet("theme").(string),
"version": config.Version,
})
return
}
@ -49,6 +52,7 @@ func ViewQuestion(c *gin.Context) {
c.HTML(500, "home.html", gin.H{
"errorMessage": "Unable to fetch question data",
"theme": c.MustGet("theme").(string),
"version": config.Version,
})
return
}
@ -57,6 +61,7 @@ func ViewQuestion(c *gin.Context) {
c.HTML(500, "home.html", gin.H{
"errorMessage": "Received a non-OK status code",
"theme": c.MustGet("theme").(string),
"version": config.Version,
})
return
}
@ -70,6 +75,7 @@ func ViewQuestion(c *gin.Context) {
c.HTML(500, "home.html", gin.H{
"errorMessage": "Unable to parse question data",
"theme": c.MustGet("theme").(string),
"version": config.Version,
})
return
}
@ -91,6 +97,7 @@ func ViewQuestion(c *gin.Context) {
c.HTML(500, "home.html", gin.H{
"errorMessage": "Unable to parse question body",
"theme": c.MustGet("theme").(string),
"version": config.Version,
})
return
}