diff --git a/block.go b/block.go index d13b935..52abd8d 100644 --- a/block.go +++ b/block.go @@ -8,7 +8,7 @@ import ( "time" ) -func agentBlock(next http.Handler) http.Handler { +func agentBlock(next http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ua := r.Header.Get("User-Agent") for _, bua := range []string{"Amazonbot", "semrush", "Bytespider", "AhrefsBot"} { @@ -22,7 +22,7 @@ func agentBlock(next http.Handler) http.Handler { }) } -func cloudflareBlock(next http.Handler) http.Handler { +func cloudflareBlock(next http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ip := net.ParseIP(actualIP(r)) if ip != nil { diff --git a/http_middleware.go b/http_middleware.go index ae09ff4..5f477d4 100644 --- a/http_middleware.go +++ b/http_middleware.go @@ -2,7 +2,7 @@ package main import "net/http" -func loggingMiddleware(next http.Handler) http.Handler { +func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path if r.URL.RawQuery != "" { diff --git a/main.go b/main.go index ea5bd2e..94804fc 100644 --- a/main.go +++ b/main.go @@ -144,15 +144,21 @@ func main() { mux.HandleFunc("/{code}", renderEvent) mux.HandleFunc("/{$}", renderHomepage) - var mainHandler http.Handler = relay - // apply http middlewares - for _, middleware := range []func(http.Handler) http.Handler{ - cors.Default().Handler, - loggingMiddleware, - agentBlock, - cloudflareBlock, - } { - mainHandler = middleware(mainHandler) + corsH := cors.Default() + corsM := func(next http.HandlerFunc) http.HandlerFunc { + return corsH.Handler(next).ServeHTTP + } + + var mainHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { + cloudflareBlock( + agentBlock( + loggingMiddleware( + corsM( + relay.ServeHTTP, + ), + ), + ), + )(w, r) } log.Print("listening at http://0.0.0.0:" + s.Port)