Skip to content

Commit

Permalink
feat: group interface (#52)
Browse files Browse the repository at this point in the history
Co-authored-by: ToTu-Dev <[email protected]>
  • Loading branch information
apoorvcodes and apoorvcodes authored Mar 6, 2022
1 parent a63caae commit f0e55c2
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 208 deletions.
11 changes: 11 additions & 0 deletions _examples/group/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package group

import "github.com/gominima/minima"

func RouteGroup() *minima.Group {
grp := minima.NewGroup("/v1")
grp.Get("/auth", func(res *minima.Response, req *minima.Request) {
res.Send("auth")
})
return grp
}
12 changes: 6 additions & 6 deletions _examples/rtr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package rtr

import (
"github.com/gominima/minima"
"github.com/gominima/minima/_examples/rtr/auth"
"github.com/gominima/minima/_examples/rtr/test_routes"
)

func SimpleTest() minima.Handler {
Expand All @@ -13,10 +13,10 @@ func SimpleTest() minima.Handler {

func Router() *minima.Router {
rt := minima.NewRouter()
rt.Get("/test/one", auth.SimpleTests())
rt.Get("/test/two", auth.SimpleTests1())
rt.Get("/test/three", auth.SimpleTests2())
rt.Get("/test/four", auth.SimpleTests3())
rt.Get("/test/last", auth.SimpleTests4())
rt.Get("/test/one", test.SimpleTests())
rt.Get("/test/two", test.SimpleTests1())
rt.Get("/test/three", test.SimpleTests2())
rt.Get("/test/four", test.SimpleTests3())
rt.Get("/test/last", test.SimpleTests4())
return rt
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package auth
package test
import "github.com/gominima/minima"

func SimpleTests() minima.Handler {
Expand Down
2 changes: 2 additions & 0 deletions _examples/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SOFTWARE.
import (
"fmt"
"github.com/gominima/minima"
"github.com/gominima/minima/_examples/group"
"github.com/gominima/minima/_examples/rtr"
"net/http"
)
Expand All @@ -39,6 +40,7 @@ func main() {
app := minima.New()
app.UseRaw(SimpleTest())
app.UseRouter(rtr.Router())
app.UseGroup(group.RouteGroup())
app.Get("/", func(res *minima.Response, req *minima.Request) {
res.Send("Hello")
res.Send(req.Query("name"))
Expand Down
114 changes: 114 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package minima

/**
* @info The minima group structure
* @property {[]cacheroute} [route] The array of cached routes
* @property {[string} [prefix] The group prefix
*/
type Group struct {
route []*cacheRoute
prefix string
}

/**
* @info Creates a new minima group
* @return {Group}
*/
func NewGroup(prefix string) *Group {
return &Group{
route: make([]*cacheRoute, 0),
prefix: prefix,
}
}

func (g *Group) register(method string, path string, handler Handler) {
g.route = append(g.route, &cacheRoute{
method: method,
path: g.prefix + path,
handler: handler,
})
}

/**
* @info Adds route with Get method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Router}
*/
func (g *Group) Get(path string, handler Handler) *Group {
g.register("GET", path, handler)
return g
}

/**
* @info Adds route with Post method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Group}
*/
func (g *Group) Post(path string, handler Handler) *Group {
g.register("POST", path, handler)
return g
}

/**
* @info Adds route with Put method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Group}
*/
func (g *Group) Put(path string, handler Handler) *Group {
g.register("PUT", path, handler)
return g
}

/**
* @info Adds route with Patch method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Group}
*/
func (g *Group) Patch(path string, handler Handler) {
g.register("PATCH", path, handler)
}

/**
* @info Adds route with Options method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Group}
*/
func (g *Group) Options(path string, handler Handler) *Group {
g.register("OPTIONS", path, handler)
return g
}

/**
* @info Adds route with Head method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Group}
*/
func (g *Group) Head(path string, handler Handler) *Group {
g.register("HEAD", path, handler)
return g
}

/**
* @info Adds route with Delete method
* @param {string} [path] The route path
* @param {...Handler} [handler] The handler for the given route
* @returns {*Group}
*/
func (g *Group) Delete(path string, handler Handler) *Group {
g.register("DELETE", path, handler)
return g
}

/**
* @info Returns all routes for the group
* @return {[]cachRoute}
*/
func (g *Group) GetGroupRoutes() []*cacheRoute {
return g.route
}
13 changes: 13 additions & 0 deletions minima.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,16 @@ func (m *Minima) Use(handler Handler) *Minima {
m.router.use(build(handler, nil))
return m
}

/**
* @info Injects minima group to main router stack
* @param {Group} [grp] The minima group to append
* @returns {}
*/
func (m *Minima) UseGroup(grp *Group) *Minima {
for _, v := range grp.GetGroupRoutes() {
fmt.Print(v.method)
m.router.routes[v.method].InsertNode(v.path, v.handler)
}
return m
}
196 changes: 0 additions & 196 deletions mux.go

This file was deleted.

Loading

0 comments on commit f0e55c2

Please sign in to comment.