diff options
| author | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
| commit | 9eb9b7f4bd552a641235764f66483e1f940fcfd9 (patch) | |
| tree | da520b923b5e6758d5457b6233dd6671fc640914 /nexus/utils/urls | |
| parent | 65a143a0871c35989b7c7ea6723d39a0585c089e (diff) | |
| download | echoes-of-vaelun-main.tar.xz echoes-of-vaelun-main.zip | |
Diffstat (limited to 'nexus/utils/urls')
| -rw-r--r-- | nexus/utils/urls/attach.go | 12 | ||||
| -rw-r--r-- | nexus/utils/urls/path.go | 114 | ||||
| -rw-r--r-- | nexus/utils/urls/registry.go | 34 |
3 files changed, 160 insertions, 0 deletions
diff --git a/nexus/utils/urls/attach.go b/nexus/utils/urls/attach.go new file mode 100644 index 0000000..b8a1458 --- /dev/null +++ b/nexus/utils/urls/attach.go @@ -0,0 +1,12 @@ +package urls
+
+import "github.com/gofiber/fiber/v2"
+
+func Attach(application *fiber.App) {
+ registry.Mutex.Lock()
+ defer registry.Mutex.Unlock()
+
+ for _, route := range registry.Routes.All() {
+ bindPath(application, route)
+ }
+}
diff --git a/nexus/utils/urls/path.go b/nexus/utils/urls/path.go new file mode 100644 index 0000000..ca44726 --- /dev/null +++ b/nexus/utils/urls/path.go @@ -0,0 +1,114 @@ +package urls
+
+import (
+ "strings"
+
+ "nexus/utils/collections"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+type HTTPMethod string
+
+const (
+ Delete HTTPMethod = "DELETE"
+ Get HTTPMethod = "GET"
+ Head HTTPMethod = "HEAD"
+ Options HTTPMethod = "OPTIONS"
+ Patch HTTPMethod = "PATCH"
+ Post HTTPMethod = "POST"
+ Put HTTPMethod = "PUT"
+)
+
+func Path(method 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.Set(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.Get(routeName)
+ if !exists {
+ return "", false
+ }
+
+ return route.FullPath, true
+}
+
+func ResolvePath(routeName string, params collections.Record[string, string]) (string, bool) {
+ registry.Mutex.Lock()
+ defer registry.Mutex.Unlock()
+
+ route, exists := registry.Routes.Get(routeName)
+ if !exists {
+ return "", false
+ }
+
+ resolved := route.FullPath
+ for key, value := range params {
+ resolved = strings.ReplaceAll(resolved, ":"+key, value)
+ }
+
+ return resolved, true
+}
+
+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 bindPath(application *fiber.App, route RegisteredRoute) {
+ switch route.Method {
+ case Delete:
+ application.Delete(route.FullPath, route.Handler)
+ case Get:
+ application.Get(route.FullPath, route.Handler)
+ case Head:
+ application.Head(route.FullPath, route.Handler)
+ case Options:
+ application.Options(route.FullPath, route.Handler)
+ case Patch:
+ application.Patch(route.FullPath, route.Handler)
+ case Post:
+ application.Post(route.FullPath, route.Handler)
+ case Put:
+ application.Put(route.FullPath, route.Handler)
+ }
+}
+
+func ensureLeadingSlash(path string) string {
+ switch strings.HasPrefix(path, "/") {
+ case true:
+ return path
+ default:
+ return "/" + path
+ }
+}
diff --git a/nexus/utils/urls/registry.go b/nexus/utils/urls/registry.go new file mode 100644 index 0000000..548a820 --- /dev/null +++ b/nexus/utils/urls/registry.go @@ -0,0 +1,34 @@ +package urls
+
+import (
+ "sync"
+
+ "nexus/utils/collections"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+type RegisteredRoute struct {
+ Method HTTPMethod
+ Path string
+ Handler fiber.Handler
+ Namespace string
+ Name string
+ FullPath string
+}
+
+type RouteRegistry struct {
+ Mutex sync.Mutex
+ CurrentNamespace string
+ Routes collections.OrderedMap[string, RegisteredRoute]
+}
+
+var registry = &RouteRegistry{
+ Routes: collections.OrderedMapOf[string, RegisteredRoute](),
+}
+
+func SetNamespace(namespace string) {
+ registry.Mutex.Lock()
+ defer registry.Mutex.Unlock()
+ registry.CurrentNamespace = namespace
+}
|
