Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/mux support #33

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions controllers/baseHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions controllers/baseHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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)
})

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)
})
Expand All @@ -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{}
Expand All @@ -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{}
Expand All @@ -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)
})
Expand Down
4 changes: 1 addition & 3 deletions controllers/healthCheckHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import (
"encoding/json"
"net/http"
"time"

"github.com/julienschmidt/httprouter"
)

type HealthCheckResponse struct {
Status string `json:"status"`
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{
Expand Down
2 changes: 1 addition & 1 deletion controllers/healthCheckHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
7 changes: 3 additions & 4 deletions middleware/verifyCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,6 +24,6 @@ func VerifyCommand(next httprouter.Handle) httprouter.Handle {
utils.Errors.NewUnauthorisedError(response)
return
}
next(response, request, params)
next(response, request)
}
}
10 changes: 5 additions & 5 deletions middleware/verifyCommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions routes/baseRoute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
4 changes: 2 additions & 2 deletions routes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
Loading