From fe3c788fba499c0ccb8976351bb02c6e8652b594 Mon Sep 17 00:00:00 2001 From: Anton Zadvorny Date: Sun, 7 Jan 2024 06:09:11 +0300 Subject: [PATCH] Reafactor responses --- auth/jwt/jwt.go | 7 +------ auth/token/token.go | 7 +------ types.go => context.go | 7 ------- paginate/paginate.go | 7 +------ query/filter.go | 2 +- query/response.go | 10 --------- recoverer/recoverer.go | 7 +------ response.go | 33 ++++++++++++++++++++++++++++++ throttle/throttle.go | 7 +------ timeout/timeout.go | 7 +------ middleware.go => wrap.go | 0 middleware_test.go => wrap_test.go | 0 12 files changed, 40 insertions(+), 54 deletions(-) rename types.go => context.go (57%) delete mode 100644 query/response.go create mode 100644 response.go rename middleware.go => wrap.go (100%) rename middleware_test.go => wrap_test.go (100%) diff --git a/auth/jwt/jwt.go b/auth/jwt/jwt.go index 408b7b2..0f64c9c 100644 --- a/auth/jwt/jwt.go +++ b/auth/jwt/jwt.go @@ -28,7 +28,7 @@ var DefaultOptions = Options( WithFindTokenFn(middleware.TokenFromAuthorizationHeader), WithFindTokenFn(middleware.TokenFromQuery("jwt")), WithFindTokenFn(middleware.TokenFromCookie("jwt")), - SetResponseHandler(RespondWithUnauthorized), + SetResponseHandler(middleware.RespondWithUnauthorized), SetValidateTokenFn(allowAll), ) @@ -160,11 +160,6 @@ func Middleware(key interface{}, alg jwa.SignatureAlgorithm, opts ...Option) fun } } -// RespondWithUnauthorized is a default response handler -func RespondWithUnauthorized(w http.ResponseWriter, _ *http.Request, _ error) { - http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) -} - func allowAll(_ jwt.Token) (bool, interface{}) { return true, nil } diff --git a/auth/token/token.go b/auth/token/token.go index b006712..6935d56 100644 --- a/auth/token/token.go +++ b/auth/token/token.go @@ -25,7 +25,7 @@ var DefaultOptions = Options( WithFindTokenFn(middleware.TokenFromHeader("X-Token")), WithFindTokenFn(middleware.TokenFromQuery("token")), SetValidateTokenFn(rejectAll), - SetResponseHandler(RespondWithUnauthorized), + SetResponseHandler(middleware.RespondWithUnauthorized), ) // Options turns a list of option instances into an option @@ -113,11 +113,6 @@ func Middleware(opts ...Option) func(next http.Handler) http.Handler { } } -// RespondWithUnauthorized is a default response handler -func RespondWithUnauthorized(w http.ResponseWriter, _ *http.Request, _ error) { - http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) -} - func rejectAll(_ string) (bool, interface{}) { return false, nil } diff --git a/types.go b/context.go similarity index 57% rename from types.go rename to context.go index 2362e8e..9224998 100644 --- a/types.go +++ b/context.go @@ -1,12 +1,5 @@ package middleware -import ( - "net/http" -) - -// ResponseHandle is a function that middleware call in case of stop chain -type ResponseHandle func(w http.ResponseWriter, r *http.Request, err error) - // CtxKey is a key to use with context.WithValue type CtxKey struct { Pkg string diff --git a/paginate/paginate.go b/paginate/paginate.go index 8250e58..e150c3e 100644 --- a/paginate/paginate.go +++ b/paginate/paginate.go @@ -24,7 +24,7 @@ var DefaultOptions = Options( WithFindPaginationFn(PaginationFromQuery("page", "pageSize")), SetPaginationDefaults(1, 50), SetValidatePaginationFn(allowAll), - SetResponseHandler(RespondWithBadRequest), + SetResponseHandler(middleware.RespondWithBadRequest), ) // Pagination represents pagination info @@ -149,11 +149,6 @@ func PaginationFromQuery(pageParam string, pageSizeParam string) func(r *http.Re } } -// RespondWithBadRequest is a default response handler -func RespondWithBadRequest(w http.ResponseWriter, _ *http.Request, _ error) { - http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) -} - func allowAll(_ *Pagination) error { return nil } diff --git a/query/filter.go b/query/filter.go index 40e4fbc..1b4e1fb 100644 --- a/query/filter.go +++ b/query/filter.go @@ -56,7 +56,7 @@ func Filter[T any](opts ...FilterOption[T]) func(next http.Handler) http.Handler f := &filter[T]{ defaultFilter: *new(T), parseFilterFns: []func(r *http.Request) (T, error){}, - responseHandler: RespondWithBadRequest, + responseHandler: middleware.RespondWithBadRequest, } for _, opt := range opts { diff --git a/query/response.go b/query/response.go deleted file mode 100644 index 52695a0..0000000 --- a/query/response.go +++ /dev/null @@ -1,10 +0,0 @@ -package query - -import ( - "net/http" -) - -func RespondWithBadRequest(w http.ResponseWriter, _ *http.Request, err error) { - text := http.StatusText(http.StatusBadRequest) + ": " + err.Error() - http.Error(w, text, http.StatusBadRequest) -} diff --git a/recoverer/recoverer.go b/recoverer/recoverer.go index d077c9f..db54f41 100644 --- a/recoverer/recoverer.go +++ b/recoverer/recoverer.go @@ -12,7 +12,7 @@ import ( var DefaultOptions = Options( SetLogStackFn(defaultLogStackFn), SetLogRecoverFn(defaultLogRecoverFn), - SetResponseHandler(RespondWithInternalServerError), + SetResponseHandler(middleware.RespondWithInternalServerError), ) // Options turns a list of option instances into an option @@ -78,11 +78,6 @@ func Middleware(opts ...Option) func(next http.Handler) http.Handler { } } -// RespondWithInternalServerError is a default response handler -func RespondWithInternalServerError(w http.ResponseWriter, _ *http.Request, _ error) { - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) -} - func defaultLogStackFn(stack []byte) { fmt.Println(string(stack)) } diff --git a/response.go b/response.go new file mode 100644 index 0000000..194061e --- /dev/null +++ b/response.go @@ -0,0 +1,33 @@ +package middleware + +import ( + "net/http" +) + +// ResponseHandle is a function that middleware call in case of stop chain +type ResponseHandle func(w http.ResponseWriter, r *http.Request, err error) + +// RespondWithBadRequest is a default bad request response handler +func RespondWithBadRequest(w http.ResponseWriter, _ *http.Request, _ error) { + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) +} + +// RespondWithUnauthorized is a default unathorized response handler +func RespondWithUnauthorized(w http.ResponseWriter, _ *http.Request, _ error) { + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) +} + +// RespondWithTooManyRequests is a default too many requests response handler +func RespondWithTooManyRequests(w http.ResponseWriter, _ *http.Request, _ error) { + http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests) +} + +// RespondWithInternalServerError is a default internal server error response handler +func RespondWithInternalServerError(w http.ResponseWriter, _ *http.Request, _ error) { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) +} + +// RespondWithTimeout is a default gateway timeout response handler +func RespondWithGatewayTimeout(w http.ResponseWriter, _ *http.Request, _ error) { + http.Error(w, http.StatusText(http.StatusGatewayTimeout), http.StatusGatewayTimeout) +} diff --git a/throttle/throttle.go b/throttle/throttle.go index 23db557..d39577a 100644 --- a/throttle/throttle.go +++ b/throttle/throttle.go @@ -19,7 +19,7 @@ var ( // DefaultOptions represents default throttle middleware options var DefaultOptions = Options( SetLimit(100), - SetResponseHandler(RespondWithTooManyRequests), + SetResponseHandler(middleware.RespondWithTooManyRequests), ) // Options turns a list of option instances into an option @@ -169,8 +169,3 @@ func Middleware(opts ...Option) func(http.Handler) http.Handler { }) } } - -// RespondWithTooManyRequests is a default response handler -func RespondWithTooManyRequests(w http.ResponseWriter, _ *http.Request, _ error) { - http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests) -} diff --git a/timeout/timeout.go b/timeout/timeout.go index 1e80a33..0913de6 100644 --- a/timeout/timeout.go +++ b/timeout/timeout.go @@ -11,7 +11,7 @@ import ( // DefaultOptions represents default timeout middleware options var DefaultOptions = Options( SetTimeout(time.Second*30), - SetResponseHandler(RespondWithTimeout), + SetResponseHandler(middleware.RespondWithGatewayTimeout), ) // Options turns a list of option instances into an option @@ -74,8 +74,3 @@ func Middleware(opts ...Option) func(next http.Handler) http.Handler { }) } } - -// RespondWithTimeout is a default response handler -func RespondWithTimeout(w http.ResponseWriter, _ *http.Request, _ error) { - http.Error(w, http.StatusText(http.StatusGatewayTimeout), http.StatusGatewayTimeout) -} diff --git a/middleware.go b/wrap.go similarity index 100% rename from middleware.go rename to wrap.go diff --git a/middleware_test.go b/wrap_test.go similarity index 100% rename from middleware_test.go rename to wrap_test.go