From 09acb111a2b22f5794541fac175b024dd0f9100e Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 20 May 2022 11:17:31 -0600 Subject: Rename api package to http --- srv/src/api/middleware.go | 95 ----------------------------------------------- 1 file changed, 95 deletions(-) delete mode 100644 srv/src/api/middleware.go (limited to 'srv/src/api/middleware.go') diff --git a/srv/src/api/middleware.go b/srv/src/api/middleware.go deleted file mode 100644 index 974889b..0000000 --- a/srv/src/api/middleware.go +++ /dev/null @@ -1,95 +0,0 @@ -package api - -import ( - "net" - "net/http" - "time" - - "github.com/mediocregopher/blog.mediocregopher.com/srv/api/apiutil" - "github.com/mediocregopher/mediocre-go-lib/v2/mctx" - "github.com/mediocregopher/mediocre-go-lib/v2/mlog" -) - -func addResponseHeaders(headers map[string]string, h http.Handler) http.Handler { - return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - for k, v := range headers { - rw.Header().Set(k, v) - } - h.ServeHTTP(rw, r) - }) -} - -func setLoggerMiddleware(logger *mlog.Logger, h http.Handler) http.Handler { - return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - type reqInfoKey string - - ip, _, _ := net.SplitHostPort(r.RemoteAddr) - - ctx := r.Context() - ctx = mctx.Annotate(ctx, - reqInfoKey("remote_ip"), ip, - reqInfoKey("url"), r.URL, - reqInfoKey("method"), r.Method, - ) - - r = r.WithContext(ctx) - r = apiutil.SetRequestLogger(r, logger) - h.ServeHTTP(rw, r) - }) -} - -type logResponseWriter struct { - http.ResponseWriter - http.Hijacker - statusCode int -} - -func newLogResponseWriter(rw http.ResponseWriter) *logResponseWriter { - h, _ := rw.(http.Hijacker) - return &logResponseWriter{ - ResponseWriter: rw, - Hijacker: h, - statusCode: 200, - } -} - -func (lrw *logResponseWriter) WriteHeader(statusCode int) { - lrw.statusCode = statusCode - lrw.ResponseWriter.WriteHeader(statusCode) -} - -func logReqMiddleware(h http.Handler) http.Handler { - return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - lrw := newLogResponseWriter(rw) - - started := time.Now() - h.ServeHTTP(lrw, r) - took := time.Since(started) - - type logCtxKey string - - ctx := r.Context() - ctx = mctx.Annotate(ctx, - logCtxKey("took"), took.String(), - logCtxKey("response_code"), lrw.statusCode, - ) - - apiutil.GetRequestLogger(r).Info(ctx, "handled HTTP request") - }) -} - -func disallowGetMiddleware(h http.Handler) http.Handler { - return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - - // we allow websockets to be GETs because, well, they must be - if r.Method != "GET" || r.Header.Get("Upgrade") == "websocket" { - h.ServeHTTP(rw, r) - return - } - - apiutil.GetRequestLogger(r).WarnString(r.Context(), "method not allowed") - rw.WriteHeader(405) - }) -} -- cgit v1.2.3