Răsfoiți Sursa

Mark non-exact routes via path

DarthSim 1 lună în urmă
părinte
comite
196b4dfc46
3 a modificat fișierele cu 33 adăugiri și 27 ștergeri
  1. 8 8
      main.go
  2. 14 8
      server/router.go
  3. 11 11
      server/router_test.go

+ 8 - 8
main.go

@@ -35,22 +35,22 @@ const (
 )
 
 func buildRouter(r *server.Router) *server.Router {
-	r.GET("/", true, handlers.LandingHandler)
-	r.GET("", true, handlers.LandingHandler)
+	r.GET("/", handlers.LandingHandler)
+	r.GET("", handlers.LandingHandler)
 
-	r.GET(faviconPath, true, r.NotFoundHandler).Silent()
-	r.GET(healthPath, true, handlers.HealthHandler).Silent()
+	r.GET(faviconPath, r.NotFoundHandler).Silent()
+	r.GET(healthPath, handlers.HealthHandler).Silent()
 	if config.HealthCheckPath != "" {
-		r.GET(config.HealthCheckPath, true, handlers.HealthHandler).Silent()
+		r.GET(config.HealthCheckPath, handlers.HealthHandler).Silent()
 	}
 
 	r.GET(
-		"/", false, handleProcessing,
+		"/*", handleProcessing,
 		r.WithSecret, r.WithCORS, r.WithPanic, r.WithReportError, r.WithMonitoring,
 	)
 
-	r.HEAD("/", false, r.OkHandler, r.WithCORS)
-	r.OPTIONS("/", false, r.OkHandler, r.WithCORS)
+	r.HEAD("/*", r.OkHandler, r.WithCORS)
+	r.OPTIONS("/*", r.OkHandler, r.WithCORS)
 
 	return r
 }

+ 14 - 8
server/router.go

@@ -57,14 +57,20 @@ func NewRouter(config *Config) (*Router, error) {
 }
 
 // add adds an abitary route to the router
-func (r *Router) add(method, prefix string, exact bool, handler RouteHandler, middlewares ...Middleware) *route {
+func (r *Router) add(method, path string, handler RouteHandler, middlewares ...Middleware) *route {
 	for _, m := range middlewares {
 		handler = m(handler)
 	}
 
+	exact := true
+	if strings.HasSuffix(path, "*") {
+		exact = false
+		path = strings.TrimSuffix(path, "*")
+	}
+
 	newRoute := &route{
 		method:  method,
-		path:    r.config.PathPrefix + prefix,
+		path:    r.config.PathPrefix + path,
 		handler: handler,
 		exact:   exact,
 	}
@@ -88,18 +94,18 @@ func (r *Router) add(method, prefix string, exact bool, handler RouteHandler, mi
 }
 
 // GET adds GET route
-func (r *Router) GET(prefix string, exact bool, handler RouteHandler, middlewares ...Middleware) *route {
-	return r.add(http.MethodGet, prefix, exact, handler, middlewares...)
+func (r *Router) GET(path string, handler RouteHandler, middlewares ...Middleware) *route {
+	return r.add(http.MethodGet, path, handler, middlewares...)
 }
 
 // OPTIONS adds OPTIONS route
-func (r *Router) OPTIONS(prefix string, exact bool, handler RouteHandler, middlewares ...Middleware) *route {
-	return r.add(http.MethodOptions, prefix, exact, handler, middlewares...)
+func (r *Router) OPTIONS(path string, handler RouteHandler, middlewares ...Middleware) *route {
+	return r.add(http.MethodOptions, path, handler, middlewares...)
 }
 
 // HEAD adds HEAD route
-func (r *Router) HEAD(prefix string, exact bool, handler RouteHandler, middlewares ...Middleware) *route {
-	return r.add(http.MethodHead, prefix, exact, handler, middlewares...)
+func (r *Router) HEAD(path string, handler RouteHandler, middlewares ...Middleware) *route {
+	return r.add(http.MethodHead, path, handler, middlewares...)
 }
 
 // ServeHTTP serves routes

+ 11 - 11
server/router_test.go

@@ -54,9 +54,9 @@ func (s *RouterTestSuite) TestHTTPMethods() {
 	}
 
 	// Register routes with different configurations
-	s.router.GET("/get-test", true, getHandler)              // exact match
-	s.router.OPTIONS("/options-test", false, optionsHandler) // prefix match
-	s.router.HEAD("/head-test", true, headHandler)           // exact match
+	s.router.GET("/get-test", getHandler)              // exact match
+	s.router.OPTIONS("/options-test*", optionsHandler) // prefix match
+	s.router.HEAD("/head-test", headHandler)           // exact match
 
 	tests := []struct {
 		name          string
@@ -133,7 +133,7 @@ func (s *RouterTestSuite) TestMiddlewareOrder() {
 		return nil
 	}
 
-	s.router.GET("/test", true, handler, middleware2, middleware1)
+	s.router.GET("/test", handler, middleware2, middleware1)
 
 	req := httptest.NewRequest(http.MethodGet, "/api/test", nil)
 	rw := httptest.NewRecorder()
@@ -153,7 +153,7 @@ func (s *RouterTestSuite) TestServeHTTP() {
 		return nil
 	}
 
-	s.router.GET("/test", true, handler)
+	s.router.GET("/test", handler)
 
 	req := httptest.NewRequest(http.MethodGet, "/api/test", nil)
 	rw := httptest.NewRecorder()
@@ -174,7 +174,7 @@ func (s *RouterTestSuite) TestRequestID() {
 		return nil
 	}
 
-	s.router.GET("/test", true, handler)
+	s.router.GET("/test", handler)
 
 	// Test request ID passthrough (if present)
 	req := httptest.NewRequest(http.MethodGet, "/api/test", nil)
@@ -214,7 +214,7 @@ func (s *RouterTestSuite) TestLambdaRequestIDExtraction() {
 		return nil
 	}
 
-	s.router.GET("/test", true, handler)
+	s.router.GET("/test", handler)
 
 	// Test with valid Lambda context
 	req := httptest.NewRequest(http.MethodGet, "/api/test", nil)
@@ -235,7 +235,7 @@ func (s *RouterTestSuite) TestReplaceIP() {
 		return nil
 	}
 
-	s.router.GET("/test", true, handler)
+	s.router.GET("/test", handler)
 
 	tests := []struct {
 		name         string
@@ -302,9 +302,9 @@ func (s *RouterTestSuite) TestRouteOrder() {
 		return nil
 	}
 
-	s.router.GET("/test", false, h)
-	s.router.GET("/test/path", true, h)
-	s.router.GET("/test/path/nested", true, h)
+	s.router.GET("/test*", h)
+	s.router.GET("/test/path", h)
+	s.router.GET("/test/path/nested", h)
 
 	s.Require().Equal("/api/test/path", s.router.routes[0].path)
 	s.Require().Equal("/api/test/path/nested", s.router.routes[1].path)