-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from evanofslack/backend
Change up middlewares and fix nil pointer deref
- Loading branch information
Showing
11 changed files
with
131 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
"runtime/debug" | ||
"time" | ||
|
||
"github.com/go-chi/chi/v5/middleware" | ||
) | ||
|
||
func (server *Server) logRequests(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
start := time.Now() | ||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor) | ||
ctx := r.Context() | ||
|
||
next.ServeHTTP(ww, r) | ||
|
||
defer func() { | ||
if rec := recover(); rec != nil { | ||
err := rec.(error) | ||
server.logger.Log(). | ||
Stack(). | ||
Err(err). | ||
Ctx(ctx). | ||
Bytes("debug_stack", debug.Stack()). | ||
Msg("Caught error with recoverer") | ||
http.Error(ww, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
} | ||
|
||
// don't log healthcheck requests | ||
if p := r.URL.Path; p == healthRoute || p == readyRoute { | ||
return | ||
} | ||
|
||
authorized := false | ||
if a := r.Context().Value(authKey); a != nil { | ||
authorized = true | ||
} | ||
|
||
// log end request | ||
server.logger.Info(). | ||
Ctx(ctx). | ||
Fields(map[string]interface{}{ | ||
"remote_ip": r.RemoteAddr, | ||
"path": r.URL.Path, | ||
"proto": r.Proto, | ||
"method": r.Method, | ||
"user_agent": r.Header.Get("User-Agent"), | ||
"status": ww.Status(), | ||
"latency_ms": float64(time.Since(start).Nanoseconds()) / 1000000.0, | ||
"bytes_in": r.Header.Get("Content-Length"), | ||
"bytes_out": ww.BytesWritten(), | ||
"authorized": authorized, | ||
}). | ||
Msg("Incoming request") | ||
}() | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-chi/chi/v5/middleware" | ||
"github.com/go-chi/httprate" | ||
) | ||
|
||
func (server *Server) addRatelimiter() { | ||
|
||
if !server.config.App.RateLimitEnabled { | ||
return | ||
} | ||
|
||
// rate limit by IP with json response | ||
rateLimiter := httprate.Limit(rateLimit, rateLimitPeriod, | ||
httprate.WithKeyFuncs(httprate.KeyByIP), | ||
httprate.WithLimitHandler(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusTooManyRequests) | ||
w.Write([]byte(`{"error": "Too many requests"}`)) | ||
})) | ||
|
||
server.router.Use(middleware.Maybe(rateLimiter, server.applyRateLimit)) | ||
server.logger.Info().Msg("Added rate limiting middleware") | ||
} | ||
|
||
// apply rate limit only if user is not authenticated | ||
func (server *Server) applyRateLimit(r *http.Request) bool { | ||
|
||
rl_username := server.config.Auth.RateLimitUsername | ||
rl_password := server.config.Auth.RateLimitPassword | ||
|
||
authenticated := server.passBasicAuth(rl_username, rl_password, r) | ||
if authenticated { | ||
return false | ||
} | ||
return true | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters