This commit is contained in:
VnPower
2024-08-28 16:07:32 +07:00
committed by iacore
parent 16c9174d05
commit ab6e6793f6
2 changed files with 37 additions and 4 deletions
+3 -3
View File
@@ -2,7 +2,7 @@
{{ block body() }}
<div class="container">
{{ if !.LoggedIn }}
{{ if !.LoggedIn }}
<p>Not logged in. You can login <a href="/settings">here</a>.</p>
<div class="component-header">
<h2>Today's illustration rankings</h2>
@@ -12,7 +12,7 @@
{{ include "components/ranking-tn" .NoTokenData.Contents }}
</div>
{{ else }}
{{ else }}
<div class="switcher">
<span class="switch-title">Filter</span>
<a href="/?mode=all" class="switch-button">All</a>
@@ -39,6 +39,6 @@
<br />
{{ end }}
{{ end }}
{{ end }}
</div>
{{ end }}
+34 -1
View File
@@ -92,6 +92,36 @@ func (lim *IPRateLimiter) Allow(ip string) bool {
return rl.Allow()
}
var limiter *IPRateLimiter = NewIPRateLimiter(0, 0)
func MiddlewareChain(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
if CanRequestSkipLogger(r) {
handler.ServeHTTP(w, r)
return
}
if !limiter.Allow(ip) {
err := errors.New("Too many requests")
log.Println("Within handler: ", err)
code := http.StatusTooManyRequests
w.WriteHeader(code)
// Send custom error page
err = template.Render(w, r, routes.Data_error{Title: "Error", Error: err})
if err != nil {
err = utils.SendString(w, (fmt.Sprintf("Internal Server Error: %s", err)))
if err != nil {
return
}
}
}
handler.ServeHTTP(w, r)
})
}
func main() {
config.GlobalServerConfig.InitializeConfig()
if config.GlobalServerConfig.InDevelopment {
@@ -104,10 +134,10 @@ func main() {
defer cancel()
config.InitializeProxyChecker(ctx_timeout)
_ = NewIPRateLimiter(0, 0)
router := defineRoutes()
main_handler := func(w_ http.ResponseWriter, r *http.Request) {
println("main handler")
w := &ResponseWriterInterceptStatus{
statusCode: 0,
ResponseWriter: w_,
@@ -145,6 +175,7 @@ func main() {
}
}
}
return nil
})(w, r)
@@ -222,6 +253,8 @@ func handlePrefix(router *mux.Router, pathPrefix string, handler http.Handler) *
func defineRoutes() *mux.Router {
router := mux.NewRouter()
//router.Use(MiddlewareChain)
router.HandleFunc("/favicon.ico", serveFile("./assets/img/favicon.ico"))
router.HandleFunc("/robots.txt", serveFile("./assets/robots.txt"))
handlePrefix(router, "/img/", http.FileServer(http.Dir("./assets/img")))