Skip to content

Commit

Permalink
middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kevkevinpal committed Dec 13, 2024
1 parent 79341e5 commit 6ac80ba
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 96 deletions.
34 changes: 1 addition & 33 deletions .github/workflows/prjob_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,7 @@ jobs:
run: go get golang.org/x/tools/cmd/cover

- name: Tests
continue-on-error: true
run: |
if ! sudo V2_BOT_URL=http://localhost:3005 V2_BOT_TOKEN=xyzxyzxyz go test ./... -failfast -race -v -coverprofile=coverage.out > test_output.log 2>&1; then
echo "::set-output name=test_status::failed"
fi
./cover-check.sh coverage.out 8.4
- name: Upload test log
if: ${{steps.tests.outputs.test_status}} == 'failed'
uses: actions/upload-artifact@v3
with:
name: test-logs
path: test_output.log

- name: Parse test log
if: ${{steps.tests.outputs.test_status}} == 'failed'
run: |
cat test_output.log
if grep -q "\-\-\- FAIL" test_output.log; then
echo "Errors found in the logs."
# Write a loop here to get all the errors
# Send a curl request to jarvis use the env vars to set the url and token
#curl -X POST \
#-H "Content-Type: application/json" \
#-H "Authorization: Bearer ${{ secrets.JARVIS_API_TOKEN }}" \
#-d '{"log": "<log variable here>"}' \
#${{ secrets.JARVIS_URL }}/endpoint
exit 1
else
echo "No errors found in the logs."
fi
run: sudo V2_BOT_URL=http://localhost:3005 V2_BOT_TOKEN=xyzxyzxyz go test ./... -race -v -coverprofile=coverage.out && ./cover-check.sh coverage.out 8.4

- name: Droping DB with docker compose
run: docker compose -f ./docker/testdb-docker-compose.yml -p test_db down
Expand Down
2 changes: 1 addition & 1 deletion handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestGetAdminPubkeys(t *testing.T) {

handler.ServeHTTP(rr, req)

if status := rr.Code; status != rr.Status {
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
Expand Down
1 change: 0 additions & 1 deletion handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,6 @@ func TestDeleteBounty(t *testing.T) {
}
handler.ServeHTTP(rr, req)

//Check that this fails
assert.Equal(t, http.StatusInternalServerError, rr.Code)
})

Expand Down
4 changes: 0 additions & 4 deletions routes/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import (
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/handlers"
"github.com/stakwork/sphinx-tribes/utils"
)

func BountyRoutes() chi.Router {
r := chi.NewRouter()
bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB)

r.Use(utils.ErrorHandler)

r.Group(func(r chi.Router) {
r.Get("/all", bountyHandler.GetAllBounties)

Expand Down
25 changes: 25 additions & 0 deletions routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,36 @@ func getFromAuth(path string) (*extractResponse, error) {
}, nil
}

// 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 initChi() *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(internalServerErrorHandler)
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
Expand Down
3 changes: 0 additions & 3 deletions routes/ticket_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import (
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/handlers"
"github.com/stakwork/sphinx-tribes/utils"
)

func TicketRoutes() chi.Router {
r := chi.NewRouter()
ticketHandler := handlers.NewTicketHandler(http.DefaultClient, db.DB)

r.Use(utils.ErrorHandler)

r.Group(func(r chi.Router) {
r.Get("/{uuid}", ticketHandler.GetTicket)
r.Post("/review", ticketHandler.ProcessTicketReview)
Expand Down
54 changes: 0 additions & 54 deletions utils/error_handler.go

This file was deleted.

0 comments on commit 6ac80ba

Please sign in to comment.