diff --git a/go.mod b/go.mod index 38aed3db3..8b0898986 100644 --- a/go.mod +++ b/go.mod @@ -60,6 +60,7 @@ require ( github.com/lightningnetwork/lnd/tor v1.1.2 // indirect github.com/onsi/gomega v1.26.0 // indirect github.com/robfig/cron v1.2.0 + github.com/urfave/negroni v1.0.0 // indirect golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect gopkg.in/go-playground/assert.v1 v1.2.1 // indirect ) diff --git a/go.sum b/go.sum index ba38bcd7f..d389d674c 100644 --- a/go.sum +++ b/go.sum @@ -2166,6 +2166,8 @@ github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0o github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/cli v1.22.9/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= +github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo= github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8= diff --git a/routes/index.go b/routes/index.go index 030a04db8..bc12cdc1b 100644 --- a/routes/index.go +++ b/routes/index.go @@ -11,6 +11,7 @@ import ( "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/rs/cors" + "github.com/urfave/negroni" "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" @@ -142,35 +143,25 @@ func getFromAuth(path string) (*extractResponse, error) { } // Middleware to handle InternalServerError -// func internalServerErrorHandler(next http.Handler) http.Handler { -// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { -// rr := &responseRecorder{ResponseWriter: w, statusCode: http.StatusOK} -// next.ServeHTTP(rr, r) - -// if rr.statusCode == http.StatusInternalServerError { -// fmt.Printf("Internal Server Error: %s %s\n", r.Method, r.URL.Path) -// http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) -// } -// }) -// } - -// Custom ResponseWriter to capture status codes -// type responseRecorder struct { -// http.ResponseWriter -// statusCode int -// } - -// func (rr *responseRecorder) WriteHeader(code int) { -// rr.statusCode = code -// rr.ResponseWriter.WriteHeader(code) -// } +func internalServerErrorHandler(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + rr := negroni.NewResponseWriter(w) + next.ServeHTTP(rr, r) + + if rr.Status() == http.StatusInternalServerError { + fmt.Printf("Internal Server Error: %s %s\n", r.Method, r.URL.Path) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + } + }) +} + func initChi() *chi.Mux { r := chi.NewRouter() r.Use(middleware.RequestID) r.Use(middleware.Logger) r.Use(middleware.Recoverer) - // r.Use(internalServerErrorHandler) + r.Use(internalServerErrorHandler) cors := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},