Skip to content

Commit

Permalink
feat: using negroni middleware we fixed the socket issue (#2200)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevkevinpal authored Dec 16, 2024
1 parent feaaa94 commit 1eb243c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
37 changes: 14 additions & 23 deletions routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"},
Expand Down

0 comments on commit 1eb243c

Please sign in to comment.