summaryrefslogtreecommitdiff
path: root/group.go
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-12-03 14:36:10 +0800
committerEltrac <hi@guhub.cn>2025-12-03 14:36:10 +0800
commitf7643a06846043cf932cf3d2eb7f49be5322ca56 (patch)
tree5366abbeb5e8428e12dccd4b322d79e5452bad9d /group.go
parent59ebb0972ee5107a208ef5e02759a7299afa9288 (diff)
feat: routes grouping
Diffstat (limited to 'group.go')
-rw-r--r--group.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/group.go b/group.go
new file mode 100644
index 0000000..7cb56ad
--- /dev/null
+++ b/group.go
@@ -0,0 +1,50 @@
+package growel
+
+import (
+ "strings"
+)
+
+/* constructor */
+
+// Engine.Group creates a Group
+// pass the Group as a parameter to the callback function
+// used to create route groups
+func (e *Engine) Group(prefix string, fn func(*Group)) {
+ g := &Group{
+ prefix: prefix,
+ engine: e,
+ }
+
+ fn(g)
+}
+
+/* Inherit Engine methods */
+
+func (g *Group) GET(path string, handler Handler) {
+ full := joinPath(g.prefix, path)
+ g.engine.GET(full, handler)
+}
+
+func (g *Group) POST(path string, handler Handler) {
+ full := joinPath(g.prefix, path)
+ g.engine.POST(full, handler)
+}
+
+func (g *Group) DELETE(path string, handler Handler) {
+ full := joinPath(g.prefix, path)
+ g.engine.DELETE(full, handler)
+}
+
+func (g *Group) PUT(path string, handler Handler) {
+ full := joinPath(g.prefix, path)
+ g.engine.PUT(full, handler)
+}
+
+/* utils */
+
+func joinPath(a, b string) string {
+ if a == "/" {
+ return a + strings.TrimPrefix(b, "/")
+ }
+ return strings.TrimSuffix(a, "/") + "/" + strings.TrimPrefix(b, "/")
+}