diff options
Diffstat (limited to 'utils/urls')
| -rw-r--r-- | utils/urls/attach.go | 35 | ||||
| -rw-r--r-- | utils/urls/functions.go | 30 | ||||
| -rw-r--r-- | utils/urls/path.go | 37 | ||||
| -rw-r--r-- | utils/urls/registry.go | 13 | ||||
| -rw-r--r-- | utils/urls/types.go | 25 |
5 files changed, 140 insertions, 0 deletions
diff --git a/utils/urls/attach.go b/utils/urls/attach.go new file mode 100644 index 0000000..df8a11a --- /dev/null +++ b/utils/urls/attach.go @@ -0,0 +1,35 @@ +package urls + +import ( + "dove/enums" + + "github.com/gofiber/fiber/v2" +) + +func Attach(application *fiber.App) { + registry.Mutex.Lock() + defer registry.Mutex.Unlock() + + for _, route := range registry.Routes { + bindRoute(application, route) + } +} + +func bindRoute(application *fiber.App, route registeredRoute) { + switch route.Method { + case enums.Delete: + application.Delete(route.FullPath, route.Handler) + case enums.Get: + application.Get(route.FullPath, route.Handler) + case enums.Head: + application.Head(route.FullPath, route.Handler) + case enums.Options: + application.Options(route.FullPath, route.Handler) + case enums.Patch: + application.Patch(route.FullPath, route.Handler) + case enums.Post: + application.Post(route.FullPath, route.Handler) + case enums.Put: + application.Put(route.FullPath, route.Handler) + } +} diff --git a/utils/urls/functions.go b/utils/urls/functions.go new file mode 100644 index 0000000..6561025 --- /dev/null +++ b/utils/urls/functions.go @@ -0,0 +1,30 @@ +package urls + +import "strings" + +func resolveFullName(namespace string, name string) string { + switch namespace { + case "": + return name + default: + return namespace + "." + name + } +} + +func resolveFullPath(namespace string, path string) string { + switch namespace { + case "": + return ensureLeadingSlash(path) + default: + return "/" + namespace + ensureLeadingSlash(path) + } +} + +func ensureLeadingSlash(path string) string { + switch strings.HasPrefix(path, "/") { + case true: + return path + default: + return "/" + path + } +} diff --git a/utils/urls/path.go b/utils/urls/path.go new file mode 100644 index 0000000..b865676 --- /dev/null +++ b/utils/urls/path.go @@ -0,0 +1,37 @@ +package urls + +import ( + "dove/enums" + + "github.com/gofiber/fiber/v2" +) + +func Path(method enums.HTTPMethod, path string, handler fiber.Handler, name string) { + registry.Mutex.Lock() + defer registry.Mutex.Unlock() + + namespace := registry.CurrentNamespace + fullName := resolveFullName(namespace, name) + fullPath := resolveFullPath(namespace, path) + + 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, exists := registry.Routes[routeName] + if !exists { + return "", false + } + + return route.FullPath, true +} diff --git a/utils/urls/registry.go b/utils/urls/registry.go new file mode 100644 index 0000000..a713315 --- /dev/null +++ b/utils/urls/registry.go @@ -0,0 +1,13 @@ +package urls + +import "dove/utils/collections" + +var registry = &routeRegistry{ + Routes: make(collections.Record[registeredRoute]), +} + +func SetNamespace(namespace string) { + registry.Mutex.Lock() + defer registry.Mutex.Unlock() + registry.CurrentNamespace = namespace +} diff --git a/utils/urls/types.go b/utils/urls/types.go new file mode 100644 index 0000000..499635b --- /dev/null +++ b/utils/urls/types.go @@ -0,0 +1,25 @@ +package urls + +import ( + "sync" + + "dove/enums" + "dove/utils/collections" + + "github.com/gofiber/fiber/v2" +) + +type registeredRoute struct { + Method enums.HTTPMethod + Path string + Handler fiber.Handler + Namespace string + Name string + FullPath string +} + +type routeRegistry struct { + Mutex sync.Mutex + CurrentNamespace string + Routes collections.Record[registeredRoute] +} |
