diff --git a/controllers/baseHandler.go b/controllers/baseHandler.go index 62a73a8..e87d3b1 100644 --- a/controllers/baseHandler.go +++ b/controllers/baseHandler.go @@ -9,10 +9,9 @@ import ( service "github.com/Real-Dev-Squad/discord-service/service" "github.com/Real-Dev-Squad/discord-service/utils" "github.com/bwmarrin/discordgo" - "github.com/julienschmidt/httprouter" ) -func HomeHandler(response http.ResponseWriter, request *http.Request, params httprouter.Params) { +func HomeHandler(response http.ResponseWriter, request *http.Request) { payload, err := io.ReadAll(request.Body) if err != nil || len(payload) == 0 { utils.Errors.NewBadRequestError(response, "Invalid Request Payload") diff --git a/controllers/baseHandler_test.go b/controllers/baseHandler_test.go index 0f0b7b7..a7fd6a8 100644 --- a/controllers/baseHandler_test.go +++ b/controllers/baseHandler_test.go @@ -19,7 +19,7 @@ func TestHomeHandler(t *testing.T) { t.Run("Should return 400 when request body is empty", func(t *testing.T) { w := httptest.NewRecorder() r, _ := http.NewRequest("POST", "/", http.NoBody) - controllers.HomeHandler(w, r, nil) + controllers.HomeHandler(w, r) assert.Equal(t, http.StatusBadRequest, w.Code) }) @@ -27,7 +27,7 @@ func TestHomeHandler(t *testing.T) { t.Run("Should return 500 when request body is malformed", func(t *testing.T) { w := httptest.NewRecorder() r, _ := http.NewRequest("POST", "/", bytes.NewBuffer([]byte("malformed request"))) - controllers.HomeHandler(w, r, nil) + controllers.HomeHandler(w, r) assert.Equal(t, http.StatusInternalServerError, w.Code) }) @@ -37,7 +37,7 @@ func TestHomeHandler(t *testing.T) { message := dtos.DiscordMessage{Type: discordgo.InteractionPing} jsonBytes, _ := json.Marshal(message) r, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes)) - controllers.HomeHandler(w, r, nil) + controllers.HomeHandler(w, r) assert.Equal(t, http.StatusOK, w.Code) var response map[string]interface{} @@ -50,7 +50,7 @@ func TestHomeHandler(t *testing.T) { w := httptest.NewRecorder() jsonBytes, _ := json.Marshal(fixtures.HelloCommand) r, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes)) - controllers.HomeHandler(w, r, nil) + controllers.HomeHandler(w, r) assert.Equal(t, http.StatusOK, w.Code) var response map[string]interface{} @@ -65,7 +65,7 @@ func TestHomeHandler(t *testing.T) { message := dtos.DiscordMessage{} jsonBytes, _ := json.Marshal(message) r, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes)) - controllers.HomeHandler(w, r, nil) + controllers.HomeHandler(w, r) assert.Equal(t, http.StatusOK, w.Code) }) diff --git a/controllers/healthCheckHandler.go b/controllers/healthCheckHandler.go index ec0594e..9450320 100644 --- a/controllers/healthCheckHandler.go +++ b/controllers/healthCheckHandler.go @@ -4,8 +4,6 @@ import ( "encoding/json" "net/http" "time" - - "github.com/julienschmidt/httprouter" ) type HealthCheckResponse struct { @@ -13,7 +11,7 @@ type HealthCheckResponse struct { Timestamp string `json:"timestamp"` } -func HealthCheckHandler(response http.ResponseWriter, request *http.Request, params httprouter.Params) { +func HealthCheckHandler(response http.ResponseWriter, request *http.Request) { response.Header().Set("Content-Type", "application/json") data := HealthCheckResponse{ diff --git a/controllers/healthCheckHandler_test.go b/controllers/healthCheckHandler_test.go index af1c15e..2ab6c53 100644 --- a/controllers/healthCheckHandler_test.go +++ b/controllers/healthCheckHandler_test.go @@ -17,7 +17,7 @@ func TestHealthCheckHandler(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - controllers.HealthCheckHandler(w, r, nil) + controllers.HealthCheckHandler(w, r) }) handler.ServeHTTP(rr, req) diff --git a/go.mod b/go.mod index 2ee5740..feca194 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gorilla/mux v1.8.1 github.com/gorilla/websocket v1.5.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/crypto v0.28.0 // indirect diff --git a/go.sum b/go.sum index 937bf1a..97952c9 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= diff --git a/middleware/verifyCommand.go b/middleware/verifyCommand.go index 9f2e9bd..41bc480 100644 --- a/middleware/verifyCommand.go +++ b/middleware/verifyCommand.go @@ -7,13 +7,12 @@ import ( "github.com/Real-Dev-Squad/discord-service/config" "github.com/Real-Dev-Squad/discord-service/utils" "github.com/bwmarrin/discordgo" - "github.com/julienschmidt/httprouter" ) var VerifyInteraction = discordgo.VerifyInteraction -func VerifyCommand(next httprouter.Handle) httprouter.Handle { - return func(response http.ResponseWriter, request *http.Request, params httprouter.Params) { +func VerifyCommand(next http.HandlerFunc) http.HandlerFunc { + return func(response http.ResponseWriter, request *http.Request) { response.Header().Set("Content-Type", "application/json; charset=UTF-8") publicKeyBytes, err := hex.DecodeString(config.AppConfig.DISCORD_PUBLIC_KEY) if err != nil { @@ -25,6 +24,6 @@ func VerifyCommand(next httprouter.Handle) httprouter.Handle { utils.Errors.NewUnauthorisedError(response) return } - next(response, request, params) + next(response, request) } } diff --git a/middleware/verifyCommand_test.go b/middleware/verifyCommand_test.go index e795b0f..bbf5c1b 100644 --- a/middleware/verifyCommand_test.go +++ b/middleware/verifyCommand_test.go @@ -9,21 +9,21 @@ import ( "github.com/Real-Dev-Squad/discord-service/config" "github.com/Real-Dev-Squad/discord-service/middleware" _ "github.com/Real-Dev-Squad/discord-service/tests/helpers" - "github.com/julienschmidt/httprouter" + "github.com/gorilla/mux" "github.com/stretchr/testify/assert" ) var testControllerCalled bool = false -func testController(response http.ResponseWriter, request *http.Request, params httprouter.Params) { +func testController(response http.ResponseWriter, request *http.Request) { testControllerCalled = true response.Header().Set("Content-Type", "application/json; charset=UTF-8") response.WriteHeader(http.StatusOK) } -func setup() *httprouter.Router { +func setup() *mux.Router { testControllerCalled = false - router := httprouter.New() - router.POST("/", middleware.VerifyCommand(testController)) + router := mux.NewRouter() + router.HandleFunc("/", middleware.VerifyCommand(testController)).Methods(("POST")) return router } diff --git a/routes/baseRoute.go b/routes/baseRoute.go index 10206d1..11a8171 100644 --- a/routes/baseRoute.go +++ b/routes/baseRoute.go @@ -3,10 +3,10 @@ package routes import ( "github.com/Real-Dev-Squad/discord-service/controllers" "github.com/Real-Dev-Squad/discord-service/middleware" - "github.com/julienschmidt/httprouter" + "github.com/gorilla/mux" ) -func SetupBaseRoutes(router *httprouter.Router) { - router.POST("/", middleware.VerifyCommand(controllers.HomeHandler)) - router.GET("/health", controllers.HealthCheckHandler) +func SetupBaseRoutes(router *mux.Router) { + router.HandleFunc("/", middleware.VerifyCommand(controllers.HomeHandler)).Methods("POST") + router.HandleFunc("/health", controllers.HealthCheckHandler).Methods("GET") } diff --git a/routes/main.go b/routes/main.go index 8e4526c..82baaec 100644 --- a/routes/main.go +++ b/routes/main.go @@ -3,13 +3,13 @@ package routes import ( "net/http" - "github.com/julienschmidt/httprouter" + "github.com/gorilla/mux" "github.com/rs/cors" "github.com/sirupsen/logrus" ) func SetupV1Routes() http.Handler { - router := httprouter.New() + router := mux.NewRouter() corsConfig := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PATCH", "DELETE", "PUT"},