Skip to content

Commit

Permalink
Modified the TrailingSlashMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravsarma1992 committed Dec 11, 2024
1 parent 94d75e3 commit 2e45060
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
24 changes: 12 additions & 12 deletions internal/middleware/trailingslash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package middleware
import (
"net/http"
"strings"

"github.com/gin-gonic/gin"
)

func TrailingSlashMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" && strings.HasSuffix(r.URL.Path, "/") {
newPath := strings.TrimSuffix(r.URL.Path, "/")
newURL := newPath
if r.URL.RawQuery != "" {
newURL += "?" + r.URL.RawQuery
}
http.Redirect(w, r, newURL, http.StatusMovedPermanently)
return
func TrailingSlashMiddleware(c *gin.Context) {
if c.Request.URL.Path != "/" && strings.HasSuffix(c.Request.URL.Path, "/") {
newPath := strings.TrimSuffix(c.Request.URL.Path, "/")
newURL := newPath
if c.Request.URL.RawQuery != "" {
newURL += "?" + c.Request.URL.RawQuery
}
next.ServeHTTP(w, r)
})
http.Redirect(c.Writer, c.Request, newURL, http.StatusMovedPermanently)
return
}
c.Next()
}
16 changes: 1 addition & 15 deletions internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"errors"
"log/slog"
"net/http"
"strings"
"time"

"server/internal/db"
"server/internal/middleware"
util "server/util"

"github.com/gin-gonic/gin"
Expand All @@ -21,11 +19,6 @@ type HTTPServer struct {
DiceClient *db.DiceDB
}

type HandlerMux struct {
mux *http.ServeMux
rateLimiter func(http.ResponseWriter, *http.Request, http.Handler)
}

type HTTPResponse struct {
Data interface{} `json:"data"`
}
Expand All @@ -45,14 +38,7 @@ func errorResponse(response string) string {
return string(jsonResponse)
}

func (cim *HandlerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
middleware.TrailingSlashMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.ToLower(r.URL.Path)
cim.rateLimiter(w, r, cim.mux)
})).ServeHTTP(w, r)
}

func NewHTTPServer(router *gin.Engine, mux *http.ServeMux, diceDBAdminClient *db.DiceDB, diceClient *db.DiceDB,
func NewHTTPServer(router *gin.Engine, diceDBAdminClient *db.DiceDB, diceClient *db.DiceDB,
limit int64, window float64) *HTTPServer {

Check failure on line 42 in internal/server/http.go

View workflow job for this annotation

GitHub Actions / lint

empty-lines: extra empty line at the start of a block (revive)

return &HTTPServer{
Expand Down
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ func main() {
c.Next()
})

router.Use(middleware.TrailingSlashMiddleware)
router.Use((middleware.NewRateLimiterMiddleware(diceDBAdminClient,
configValue.Server.RequestLimitPerMin,
configValue.Server.RequestWindowSec,
).Exec))

httpServer := server.NewHTTPServer(router, nil, diceDBAdminClient, diceDBClient, configValue.Server.RequestLimitPerMin,
configValue.Server.RequestWindowSec)
httpServer := server.NewHTTPServer(
router,
diceDBAdminClient,
diceDBClient,
configValue.Server.RequestLimitPerMin,
configValue.Server.RequestWindowSec,
)

// Register routes
router.GET("/health", gin.WrapF(httpServer.HealthCheck))
Expand Down

0 comments on commit 2e45060

Please sign in to comment.