diff options
| author | Bobby <[email protected]> | 2026-01-15 15:53:17 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-01-15 15:53:17 +0530 |
| commit | c8d0bbb5b54f5cec3ebb245f9a21d8a94b3bd944 (patch) | |
| tree | 6a5c2500da90253ad07a0d5192071bb77f093d36 /utils/urls/attach.go | |
| download | cafe-c8d0bbb5b54f5cec3ebb245f9a21d8a94b3bd944.tar.xz cafe-c8d0bbb5b54f5cec3ebb245f9a21d8a94b3bd944.zip | |
Add initial project structure with Go Fiber framework and environment configuration
Diffstat (limited to 'utils/urls/attach.go')
| -rw-r--r-- | utils/urls/attach.go | 38 |
1 files changed, 38 insertions, 0 deletions
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) + } +} |
