summaryrefslogtreecommitdiffci
path: root/internal/router/path-matcher.go
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
authorGravatar SayaAndy <saya.andy@posteo.com> 2025-10-28 23:04:53 +0700
committerGravatar SayaAndy <saya.andy@posteo.com> 2025-10-28 23:04:53 +0700
commit1862f232eb70105340a91e74a5630c233a411bca (patch)
tree464c18fed0c21b63b65425f48a3a790229461058 /internal/router/path-matcher.go
parent629e275e5270ed146f9cd6dba25383cf80022909 (diff)
downloadweb-1862f232eb70105340a91e74a5630c233a411bca.tar.gz
web-1862f232eb70105340a91e74a5630c233a411bca.zip
refactor: make structurized implementation of routes + separate identity of router
feat: update go to 1.25 feat: update all dependencies debug: display routes when debug logs turned on
Diffstat (limited to 'internal/router/path-matcher.go')
-rw-r--r--internal/router/path-matcher.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/internal/router/path-matcher.go b/internal/router/path-matcher.go
new file mode 100644
index 0000000..c8aef08
--- /dev/null
+++ b/internal/router/path-matcher.go
@@ -0,0 +1,48 @@
+package router
+
+import (
+ "github.com/gofiber/fiber/v2"
+ "github.com/valyala/fasthttp"
+)
+
+type PathMatcher struct {
+ app *fiber.App
+}
+
+func NewPathMatcher() *PathMatcher {
+ app := fiber.New(fiber.Config{
+ DisableStartupMessage: true,
+ })
+ return &PathMatcher{app: app}
+}
+
+func (pm *PathMatcher) AddRoute(method, pattern string) {
+ pm.app.Add(method, pattern, func(c *fiber.Ctx) error {
+ c.Locals("pattern", pattern)
+ return nil
+ })
+}
+
+func (pm *PathMatcher) MatchPath(method, path string) (pattern string, params map[string]string, matched bool) {
+ fctx := &fasthttp.RequestCtx{}
+ fctx.Request.Header.SetMethod(method)
+ fctx.Request.SetRequestURI(path)
+
+ ctx := pm.app.AcquireCtx(fctx)
+ defer pm.app.ReleaseCtx(ctx)
+
+ pm.app.Handler()(fctx)
+
+ if ctx.Route() == nil {
+ return "", nil, false
+ }
+
+ pattern = ctx.Locals("pattern").(string)
+
+ params = make(map[string]string)
+ for _, paramName := range ctx.Route().Params {
+ params[paramName] = ctx.Params(paramName)
+ }
+
+ return pattern, params, true
+}