diff --git a/xfiber/route.go b/xfiber/route.go deleted file mode 100644 index ae73da3..0000000 --- a/xfiber/route.go +++ /dev/null @@ -1,90 +0,0 @@ -package xfiber - -import ( - "github.com/gofiber/fiber" - "strconv" -) - -// developing -type CompositeOption interface { - Check(param string) bool - Do(c *fiber.Ctx) -} - -// Option for main handler -type MainHandler struct { - Handlers []fiber.Handler -} - -// Option for specific prefix route -type PrefixOption struct { - Prefix string - Handlers []fiber.Handler -} - -// Option for numeric route -type NumericOption struct { - Handlers []fiber.Handler -} - -func M(handlers ...fiber.Handler) *MainHandler { - return &MainHandler{Handlers: handlers} -} - -func P(prefix string, handlers ...fiber.Handler) *PrefixOption { - return &PrefixOption{Prefix: prefix, Handlers: handlers} -} - -func N(handlers ...fiber.Handler) *NumericOption { - return &NumericOption{Handlers: handlers} -} - -func (p *PrefixOption) Check(param string) bool { - return p.Prefix == param -} - -func (n *NumericOption) Check(param string) bool { - _, err := strconv.Atoi(param) - return err == nil -} - -func (m *MainHandler) Do(c *fiber.Ctx) { - for _, handle := range m.Handlers { - handle(c) - if c.Error() != nil { - break - } - } -} - -func (p *PrefixOption) Do(c *fiber.Ctx) { - for _, handle := range p.Handlers { - handle(c) - if c.Error() != nil { - break - } - } -} - -func (n *NumericOption) Do(c *fiber.Ctx) { - for _, handle := range n.Handlers { - handle(c) - if c.Error() != nil { - break - } - } -} - -func Composite(key string, main *MainHandler, options ...CompositeOption) fiber.Handler { - return func(c *fiber.Ctx) { - subPath := c.Params(key) - do := main.Do - for _, option := range options { - if option.Check(subPath) { - do = option.Do - break - } - } - do(c) - } -} diff --git a/xfiber/xfiber_test.go b/xfiber/xfiber_test.go index d08f389..1748f36 100644 --- a/xfiber/xfiber_test.go +++ b/xfiber/xfiber_test.go @@ -6,36 +6,6 @@ import ( "testing" ) -func TestComposite(t *testing.T) { - app := fiber.New() - app.Get("/:id", Composite("id", - M(func(c *fiber.Ctx) { - log.Println(11, c.Params("id")) - }, func(c *fiber.Ctx) { - log.Println(12, c.Params("id")) - }), - P("a", func(c *fiber.Ctx) { - log.Println(21, c.Params("id")) - }, func(c *fiber.Ctx) { - log.Println(22, c.Params("id")) - c.Next() - }, func(c *fiber.Ctx) { - log.Println(23, c.Params("id")) - }), - P("b", func(c *fiber.Ctx) { - log.Println(31, c.Params("id")) - }, func(c *fiber.Ctx) { - log.Println(32, c.Params("id")) - }), - N(func(c *fiber.Ctx) { - log.Println(41, c.Params("id")) - }), - ), func(c *fiber.Ctx) { - log.Println(51, c.Params("id")) - }) - _ = app.Listen("1234") -} - func TestDumpRequest(t *testing.T) { app := fiber.New() app.Get("a", func(c *fiber.Ctx) { diff --git a/xgin/route.go b/xgin/route.go deleted file mode 100644 index 142ee91..0000000 --- a/xgin/route.go +++ /dev/null @@ -1,90 +0,0 @@ -package xgin - -import ( - "github.com/gin-gonic/gin" - "strconv" -) - -type CompositeOption interface { - Check(param string) bool - Do(c *gin.Context) -} - -// Option for main handler -type MainHandler struct { - Handlers []gin.HandlerFunc -} - -// Option for specific prefix route -type PrefixOption struct { - Prefix string - Handlers []gin.HandlerFunc -} - -// Option for numeric route -type NumericOption struct { - Handlers []gin.HandlerFunc -} - -func M(handlers ...gin.HandlerFunc) *MainHandler { - return &MainHandler{Handlers: handlers} -} - -func P(prefix string, handlers ...gin.HandlerFunc) *PrefixOption { - return &PrefixOption{Prefix: prefix, Handlers: handlers} -} - -func N(handlers ...gin.HandlerFunc) *NumericOption { - return &NumericOption{Handlers: handlers} -} - -func (p *PrefixOption) Check(param string) bool { - return p.Prefix == param -} - -func (n *NumericOption) Check(param string) bool { - _, err := strconv.Atoi(param) - return err == nil -} - -func (m *MainHandler) Do(c *gin.Context) { - for _, handle := range m.Handlers { - handle(c) - if c.IsAborted() { - return - } - } -} - -func (p *PrefixOption) Do(c *gin.Context) { - for _, handle := range p.Handlers { - handle(c) - if c.IsAborted() { - return - } - } -} - -func (n *NumericOption) Do(c *gin.Context) { - for _, handle := range n.Handlers { - handle(c) - if c.IsAborted() { - return - } - } -} - -// panic: 'xxx' in new path '/user/xxx' conflicts with existing wildcard ':id' in existing Prefix '/user/:id' [recovered] -func Composite(key string, main *MainHandler, options ...CompositeOption) gin.HandlerFunc { - return func(c *gin.Context) { - subPath := c.Param(key) - do := main.Do - for _, option := range options { - if option.Check(subPath) { - do = option.Do - break - } - } - do(c) - } -} diff --git a/xgin/xgin_test.go b/xgin/xgin_test.go index d274673..c8e7e7c 100644 --- a/xgin/xgin_test.go +++ b/xgin/xgin_test.go @@ -15,89 +15,3 @@ func TestDumpRequest(t *testing.T) { }) _ = app.Run(":1234") } - -func handle(c *gin.Context) { - log.Println(c.Request.URL.Path, "!", c.Param("id")) -} - -func handle2(c *gin.Context) { - log.Println(c.Request.URL.Path, "!!", c.Param("id")) -} - -func handle3(c *gin.Context) { - log.Println(c.Request.URL.Path, "!!!", c.Param("id")) -} - -func handle4(c *gin.Context) { - log.Println(c.Request.URL.Path, "!!!!", c.Param("id")) -} - -func handle5(c *gin.Context) { - log.Println(c.Request.URL.Path, "!!!!!", c.Param("id")) -} - -func handle6(c *gin.Context) { - log.Println(c.Request.URL.Path, "!!!!!!", c.Param("id")) -} - -func TestComposite(t *testing.T) { - gin.SetMode(gin.TestMode) - engine := gin.New() - testGroup := engine.Group("/test") - { - testGroup.GET("", handle) - testGroup.GET("/:id/:id2", Composite("id", - M(handle), // /?/? - P("test", handle2), // /test/? - P("test2", Composite("id2", - M(handle2), // /test2/? - P("test", handle3), // /test2/test - P("test2", handle3), // /test2/test2 - N(handle4), // /test2/0 - )), - N(handle4, // /0/? - Composite("id2", - M(handle5), // /0/? - P("test", handle6), // /0/test - ), - ), - )) - } - ctxGroup := engine.Group("/ctx") - { - ctxGroup.GET("", func(c *gin.Context) { - log.Println(1) - }, func(c *gin.Context) { - log.Println(2) - }) - ctxGroup.GET("/:id", Composite("id", - M(func(c *gin.Context) { - log.Println(11) - }, func(c *gin.Context) { - log.Println(12) - c.Abort() - }), - P("test", func(c *gin.Context) { - log.Println(21) - }, func(c *gin.Context) { - log.Println(22) - }), - P("test2", func(c *gin.Context) { - log.Println(31) - }, func(c *gin.Context) { - log.Println(32) - c.Abort() - }, func(c *gin.Context) { - log.Println(33) - c.Abort() - }), - N(func(c *gin.Context) { - log.Println(41) - c.Abort() - }, func(c *gin.Context) { - log.Println(42) - }), - )) - } - _ = engine.Run(":1234") -}