diff options
| author | Bobby <[email protected]> | 2026-03-06 23:01:58 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-03-06 23:01:58 +0530 |
| commit | ed2a033d7c08e448f5c6fd035e2de8f51431b597 (patch) | |
| tree | abe46463fc761b2e4266aadedb6b88c5493ce29b /utils | |
| parent | 3f07a4b6c745707f135a7a97e93b0fa770b67873 (diff) | |
| download | dove-ed2a033d7c08e448f5c6fd035e2de8f51431b597.tar.xz dove-ed2a033d7c08e448f5c6fd035e2de8f51431b597.zip | |
Implement database connection and routing system with HTTP method support
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/collections/types.go | 2 | ||||
| -rw-r--r-- | utils/toml/load.go | 4 | ||||
| -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 |
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] +} |
