summaryrefslogtreecommitdiff
path: root/utils/urls
diff options
context:
space:
mode:
Diffstat (limited to 'utils/urls')
-rw-r--r--utils/urls/attach.go38
-rw-r--r--utils/urls/namespace.go7
-rw-r--r--utils/urls/path.go51
-rw-r--r--utils/urls/registery.go28
4 files changed, 124 insertions, 0 deletions
diff --git a/utils/urls/attach.go b/utils/urls/attach.go
new file mode 100644
index 0000000..7d5d732
--- /dev/null
+++ b/utils/urls/attach.go
@@ -0,0 +1,38 @@
+package urls
+
+import (
+ "cafe/types"
+ "log"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+var methodBinders = map[types.HTTPMethod]func(fiber.Router, string, fiber.Handler) fiber.Router{
+ types.GET: func(r fiber.Router, path string, h fiber.Handler) fiber.Router { return r.Get(path, h) },
+ types.POST: func(r fiber.Router, path string, h fiber.Handler) fiber.Router { return r.Post(path, h) },
+ types.PUT: func(r fiber.Router, path string, h fiber.Handler) fiber.Router { return r.Put(path, h) },
+ types.PATCH: func(r fiber.Router, path string, h fiber.Handler) fiber.Router { return r.Patch(path, h) },
+ types.DELETE: func(r fiber.Router, path string, h fiber.Handler) fiber.Router { return r.Delete(path, h) },
+ types.OPTIONS: func(r fiber.Router, path string, h fiber.Handler) fiber.Router { return r.Options(path, h) },
+ types.HEAD: func(r fiber.Router, path string, h fiber.Handler) fiber.Router { return r.Head(path, h) },
+}
+
+func Attach(app *fiber.App) {
+ namespaceGroups := make(map[string]fiber.Router)
+
+ for fullName, route := range registry.routes {
+ group, exists := namespaceGroups[route.namespace]
+ if !exists {
+ group = app.Group("/" + route.namespace)
+ namespaceGroups[route.namespace] = group
+ }
+
+ binder, ok := methodBinders[route.method]
+ if !ok {
+ log.Fatalf("%s", "unsupported HTTP method: "+string(route.method))
+ }
+
+ fiberRoute := binder(group, route.path, route.handler)
+ fiberRoute.Name(fullName)
+ }
+}
diff --git a/utils/urls/namespace.go b/utils/urls/namespace.go
new file mode 100644
index 0000000..7bb5311
--- /dev/null
+++ b/utils/urls/namespace.go
@@ -0,0 +1,7 @@
+package urls
+
+func SetNamespace(namespace string) {
+ registry.mutex.Lock()
+ defer registry.mutex.Unlock()
+ registry.currentNamespace = namespace
+}
diff --git a/utils/urls/path.go b/utils/urls/path.go
new file mode 100644
index 0000000..e24ed58
--- /dev/null
+++ b/utils/urls/path.go
@@ -0,0 +1,51 @@
+package urls
+
+import (
+ "cafe/types"
+ "strings"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func Path(method types.HTTPMethod, path string, handler fiber.Handler, name string) {
+ registry.mutex.Lock()
+ defer registry.mutex.Unlock()
+
+ namespace := registry.currentNamespace
+ fullName := name
+ fullPath := path
+
+ if namespace != "" {
+ if !strings.HasPrefix(path, "/") {
+ path = "/" + path
+ }
+
+ fullName = namespace + "." + name
+ fullPath = "/" + namespace + path
+ } else {
+ if !strings.HasPrefix(fullPath, "/") {
+ fullPath = "/" + fullPath
+ }
+ }
+
+ registry.routes[fullName] = registeredRoute{
+ method: method,
+ path: path,
+ handler: handler,
+ namespace: namespace,
+ name: name,
+ fullPath: fullPath,
+ }
+}
+
+func GetFullPath(routeName string) (string, bool) {
+ registry.mutex.Lock()
+ defer registry.mutex.Unlock()
+
+ route, ok := registry.routes[routeName]
+ if !ok {
+ return "", false
+ }
+
+ return route.fullPath, true
+}
diff --git a/utils/urls/registery.go b/utils/urls/registery.go
new file mode 100644
index 0000000..b4e6a36
--- /dev/null
+++ b/utils/urls/registery.go
@@ -0,0 +1,28 @@
+package urls
+
+import (
+ "sync"
+
+ "cafe/types"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+type registeredRoute struct {
+ method types.HTTPMethod
+ path string
+ handler fiber.Handler
+ namespace string
+ name string
+ fullPath string
+}
+
+type routeRegistry struct {
+ mutex sync.Mutex
+ currentNamespace string
+ routes map[string]registeredRoute
+}
+
+var registry = &routeRegistry{
+ routes: make(map[string]registeredRoute),
+}