aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
Diffstat (limited to 'utils')
-rw-r--r--utils/collections/types.go2
-rw-r--r--utils/toml/load.go4
-rw-r--r--utils/urls/attach.go35
-rw-r--r--utils/urls/functions.go30
-rw-r--r--utils/urls/path.go37
-rw-r--r--utils/urls/registry.go13
-rw-r--r--utils/urls/types.go25
7 files changed, 143 insertions, 3 deletions
diff --git a/utils/collections/types.go b/utils/collections/types.go
index c75d1ea..ac84e76 100644
--- a/utils/collections/types.go
+++ b/utils/collections/types.go
@@ -1,3 +1,3 @@
package collections
-type Record map[string]any
+type Record[T any] map[string]T
diff --git a/utils/toml/load.go b/utils/toml/load.go
index a0cab4b..7b3c31a 100644
--- a/utils/toml/load.go
+++ b/utils/toml/load.go
@@ -8,7 +8,7 @@ import (
"dove/utils/errors"
)
-var loadedData collections.Record
+var loadedData collections.Record[any]
func LoadFile(filePath string) error {
fileContent, readError := os.ReadFile(filePath)
@@ -16,6 +16,6 @@ func LoadFile(filePath string) error {
return errors.Error(messages.ConfigFileReadFailed, filePath, readError.Error())
}
- loadedData = make(collections.Record)
+ loadedData = make(collections.Record[any])
return unmarshalContent(fileContent, &loadedData)
}
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]
+}