fix: add version, rolling ratelimit
This commit is contained in:
parent
0e9677ec61
commit
c46820b6d4
@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"anonymousoverflow/config"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -34,14 +35,23 @@ func Ratelimit() gin.HandlerFunc {
|
|||||||
c.HTML(429, "home.html", gin.H{
|
c.HTML(429, "home.html", gin.H{
|
||||||
"errorMessage": "You have exceeded the request limit. Please try again in a minute.",
|
"errorMessage": "You have exceeded the request limit. Please try again in a minute.",
|
||||||
"theme": c.MustGet("theme").(string),
|
"theme": c.MustGet("theme").(string),
|
||||||
|
"version": config.Version,
|
||||||
})
|
})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
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() {
|
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)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"anonymousoverflow/config"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -21,6 +22,7 @@ func ChangeOptions(c *gin.Context) {
|
|||||||
c.HTML(200, "home.html", gin.H{
|
c.HTML(200, "home.html", gin.H{
|
||||||
"successMessage": "Images are now " + text,
|
"successMessage": "Images are now " + text,
|
||||||
"theme": c.MustGet("theme").(string),
|
"theme": c.MustGet("theme").(string),
|
||||||
|
"version": config.Version,
|
||||||
})
|
})
|
||||||
|
|
||||||
case "theme":
|
case "theme":
|
||||||
@ -40,9 +42,8 @@ func ChangeOptions(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.Redirect(302, redirectUrl)
|
c.Redirect(302, redirectUrl)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
c.String(400, "400 Bad Request")
|
c.String(400, "400 Bad Request")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Redirect(302, "/")
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"anonymousoverflow/config"
|
||||||
"anonymousoverflow/src/utils"
|
"anonymousoverflow/src/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
@ -28,6 +29,7 @@ func ViewQuestion(c *gin.Context) {
|
|||||||
c.HTML(400, "home.html", gin.H{
|
c.HTML(400, "home.html", gin.H{
|
||||||
"errorMessage": "Invalid question ID",
|
"errorMessage": "Invalid question ID",
|
||||||
"theme": c.MustGet("theme").(string),
|
"theme": c.MustGet("theme").(string),
|
||||||
|
"version": config.Version,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -36,6 +38,7 @@ func ViewQuestion(c *gin.Context) {
|
|||||||
c.HTML(400, "home.html", gin.H{
|
c.HTML(400, "home.html", gin.H{
|
||||||
"errorMessage": "Invalid question ID",
|
"errorMessage": "Invalid question ID",
|
||||||
"theme": c.MustGet("theme").(string),
|
"theme": c.MustGet("theme").(string),
|
||||||
|
"version": config.Version,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -49,6 +52,7 @@ func ViewQuestion(c *gin.Context) {
|
|||||||
c.HTML(500, "home.html", gin.H{
|
c.HTML(500, "home.html", gin.H{
|
||||||
"errorMessage": "Unable to fetch question data",
|
"errorMessage": "Unable to fetch question data",
|
||||||
"theme": c.MustGet("theme").(string),
|
"theme": c.MustGet("theme").(string),
|
||||||
|
"version": config.Version,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -57,6 +61,7 @@ func ViewQuestion(c *gin.Context) {
|
|||||||
c.HTML(500, "home.html", gin.H{
|
c.HTML(500, "home.html", gin.H{
|
||||||
"errorMessage": "Received a non-OK status code",
|
"errorMessage": "Received a non-OK status code",
|
||||||
"theme": c.MustGet("theme").(string),
|
"theme": c.MustGet("theme").(string),
|
||||||
|
"version": config.Version,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -70,6 +75,7 @@ func ViewQuestion(c *gin.Context) {
|
|||||||
c.HTML(500, "home.html", gin.H{
|
c.HTML(500, "home.html", gin.H{
|
||||||
"errorMessage": "Unable to parse question data",
|
"errorMessage": "Unable to parse question data",
|
||||||
"theme": c.MustGet("theme").(string),
|
"theme": c.MustGet("theme").(string),
|
||||||
|
"version": config.Version,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -91,6 +97,7 @@ func ViewQuestion(c *gin.Context) {
|
|||||||
c.HTML(500, "home.html", gin.H{
|
c.HTML(500, "home.html", gin.H{
|
||||||
"errorMessage": "Unable to parse question body",
|
"errorMessage": "Unable to parse question body",
|
||||||
"theme": c.MustGet("theme").(string),
|
"theme": c.MustGet("theme").(string),
|
||||||
|
"version": config.Version,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user