From c8d0bbb5b54f5cec3ebb245f9a21d8a94b3bd944 Mon Sep 17 00:00:00 2001 From: Bobby <30593201+luciferreeves@users.noreply.github.com> Date: Thu, 15 Jan 2026 15:53:17 +0530 Subject: Add initial project structure with Go Fiber framework and environment configuration --- utils/urls/attach.go | 38 ++++++++++++++++++++++++++++++++++++ utils/urls/namespace.go | 7 +++++++ utils/urls/path.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ utils/urls/registery.go | 28 +++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 utils/urls/attach.go create mode 100644 utils/urls/namespace.go create mode 100644 utils/urls/path.go create mode 100644 utils/urls/registery.go (limited to 'utils/urls') 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), +} -- cgit v1.2.3