diff options
Diffstat (limited to 'srv/api/middleware.go')
-rw-r--r-- | srv/api/middleware.go | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/srv/api/middleware.go b/srv/api/middleware.go index e3e85bb..2605d93 100644 --- a/srv/api/middleware.go +++ b/srv/api/middleware.go @@ -5,6 +5,7 @@ import ( "net/http" "time" + "github.com/mediocregopher/blog.mediocregopher.com/srv/api/apiutils" "github.com/mediocregopher/mediocre-go-lib/v2/mctx" "github.com/mediocregopher/mediocre-go-lib/v2/mlog" ) @@ -57,7 +58,7 @@ func (lrw *logResponseWriter) WriteHeader(statusCode int) { func logMiddleware(logger *mlog.Logger, h http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - r = setRequestLogger(r, logger) + r = apiutils.SetRequestLogger(r, logger) lrw := newLogResponseWriter(rw) @@ -76,3 +77,15 @@ func logMiddleware(logger *mlog.Logger, h http.Handler) http.Handler { logger.Info(ctx, "handled HTTP request") }) } + +func allowedMethod(method string, h http.Handler) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if r.Method == method { + h.ServeHTTP(rw, r) + return + } + + apiutils.GetRequestLogger(r).WarnString(r.Context(), "method not allowed") + rw.WriteHeader(405) + }) +} |