middleware: Integrate middlewares into router functions
This commit is contained in:
parent
1aa9b78bdc
commit
3bf14b9c04
@ -9,6 +9,7 @@ import (
|
|||||||
// Core is the core struct of the framework
|
// Core is the core struct of the framework
|
||||||
type Core struct {
|
type Core struct {
|
||||||
router map[string]*Trie
|
router map[string]*Trie
|
||||||
|
middlewares []ControllerHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCore initialize the Core.
|
// NewCore initialize the Core.
|
||||||
@ -28,40 +29,44 @@ func NewCore() *Core {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get is a simple get router
|
// Get is a simple get router
|
||||||
func (c *Core) Get(url string, handler ControllerHandler) {
|
func (c *Core) Get(url string, handlers ...ControllerHandler) {
|
||||||
upperUrl := strings.ToUpper(url)
|
allHandlers := append(c.middlewares, handlers...)
|
||||||
if err := c.router["GET"].AddRouter(upperUrl, handler); err != nil {
|
if err := c.router["GET"].AddRouter(url, allHandlers); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post is a simple post router
|
// Post is a simple post router
|
||||||
func (c *Core) Post(url string, handler ControllerHandler) {
|
func (c *Core) Post(url string, handlers ...ControllerHandler) {
|
||||||
upperUrl := strings.ToUpper(url)
|
allHandlers := append(c.middlewares, handlers...)
|
||||||
if err := c.router["POST"].AddRouter(upperUrl, handler); err != nil {
|
if err := c.router["POST"].AddRouter(url, allHandlers); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put is a simple put router
|
// Put is a simple put router
|
||||||
func (c *Core) Put(url string, handler ControllerHandler) {
|
func (c *Core) Put(url string, handlers ...ControllerHandler) {
|
||||||
upperUrl := strings.ToUpper(url)
|
allHandlers := append(c.middlewares, handlers...)
|
||||||
if err := c.router["PUT"].AddRouter(upperUrl, handler); err != nil {
|
if err := c.router["PUT"].AddRouter(url, allHandlers); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete is a simple delete router
|
// Delete is a simple delete router
|
||||||
func (c *Core) Delete(url string, handler ControllerHandler) {
|
func (c *Core) Delete(url string, handlers ...ControllerHandler) {
|
||||||
upperUrl := strings.ToUpper(url)
|
allHandlers := append(c.middlewares, handlers...)
|
||||||
if err := c.router["DELETE"].AddRouter(upperUrl, handler); err != nil {
|
if err := c.router["DELETE"].AddRouter(url, allHandlers); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use registers middlewares
|
||||||
|
func (c *Core) Use(middlewares ...ControllerHandler) {
|
||||||
|
c.middlewares = append(c.middlewares, middlewares...)
|
||||||
|
}
|
||||||
|
|
||||||
// FindRouteByRequest finds route using the request
|
// FindRouteByRequest finds route using the request
|
||||||
func (c *Core) FindRouteByRequest(r *http.Request) []ControllerHandler {
|
func (c *Core) FindRouteByRequest(r *http.Request) []ControllerHandler {
|
||||||
upperUri := strings.ToUpper(r.URL.Path)
|
|
||||||
upperMethod := strings.ToUpper(r.Method)
|
upperMethod := strings.ToUpper(r.Method)
|
||||||
|
|
||||||
mapper, ok := c.router[upperMethod]
|
mapper, ok := c.router[upperMethod]
|
||||||
@ -70,7 +75,7 @@ func (c *Core) FindRouteByRequest(r *http.Request) []ControllerHandler {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
controllers := mapper.FindRoute(upperUri)
|
controllers := mapper.FindRoute(r.URL.Path)
|
||||||
if controllers == nil {
|
if controllers == nil {
|
||||||
log.Printf("URI %q is not recognized\n", r.URL.Path)
|
log.Printf("URI %q is not recognized\n", r.URL.Path)
|
||||||
return nil
|
return nil
|
||||||
|
@ -2,16 +2,18 @@ package framework
|
|||||||
|
|
||||||
// IGroup prefix routes
|
// IGroup prefix routes
|
||||||
type IGroup interface {
|
type IGroup interface {
|
||||||
Get(string, ControllerHandler)
|
Get(string, ...ControllerHandler)
|
||||||
Post(string, ControllerHandler)
|
Post(string, ...ControllerHandler)
|
||||||
Put(string, ControllerHandler)
|
Put(string, ...ControllerHandler)
|
||||||
Delete(string, ControllerHandler)
|
Delete(string, ...ControllerHandler)
|
||||||
|
Use(...ControllerHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group is the implementation of IGroup interface
|
// Group is the implementation of IGroup interface
|
||||||
type Group struct {
|
type Group struct {
|
||||||
core *Core
|
core *Core
|
||||||
prefix string
|
prefix string
|
||||||
|
middlewares []ControllerHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGroup create a new prefix group
|
// NewGroup create a new prefix group
|
||||||
@ -23,21 +25,30 @@ func NewGroup(core *Core, prefix string) *Group {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get is a simple get router of the group
|
// Get is a simple get router of the group
|
||||||
func (g *Group) Get(url string, handler ControllerHandler) {
|
func (g *Group) Get(url string, handlers ...ControllerHandler) {
|
||||||
g.core.Get(g.prefix+url, handler)
|
allHandlers := append(g.middlewares, handlers...)
|
||||||
|
g.core.Get(g.prefix+url, allHandlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post is a simple post router of the group
|
// Post is a simple post router of the group
|
||||||
func (g *Group) Post(url string, handler ControllerHandler) {
|
func (g *Group) Post(url string, handlers ...ControllerHandler) {
|
||||||
g.core.Post(g.prefix+url, handler)
|
allHandlers := append(g.middlewares, handlers...)
|
||||||
|
g.core.Post(g.prefix+url, allHandlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put is a simple put router of the group
|
// Put is a simple put router of the group
|
||||||
func (g *Group) Put(url string, handler ControllerHandler) {
|
func (g *Group) Put(url string, handlers ...ControllerHandler) {
|
||||||
g.core.Put(g.prefix+url, handler)
|
allHandlers := append(g.middlewares, handlers...)
|
||||||
|
g.core.Put(g.prefix+url, allHandlers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete is a simple delete router of the group
|
// Delete is a simple delete router of the group
|
||||||
func (g *Group) Delete(url string, handler ControllerHandler) {
|
func (g *Group) Delete(url string, handlers ...ControllerHandler) {
|
||||||
g.core.Delete(g.prefix+url, handler)
|
allHandlers := append(g.middlewares, handlers...)
|
||||||
|
g.core.Delete(g.prefix+url, allHandlers...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use registers middlewares
|
||||||
|
func (g *Group) Use(middlewares ...ControllerHandler) {
|
||||||
|
g.middlewares = append(g.middlewares, middlewares...)
|
||||||
}
|
}
|
||||||
|
34
framework/middleware/test.go
Normal file
34
framework/middleware/test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"git.vinchent.xyz/vinchent/go-web/framework"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test1() framework.ControllerHandler {
|
||||||
|
return func(c *framework.Context) error {
|
||||||
|
log.Println("middleware test1 pre")
|
||||||
|
c.Next()
|
||||||
|
log.Println("middleware test1 post")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test2() framework.ControllerHandler {
|
||||||
|
return func(c *framework.Context) error {
|
||||||
|
log.Println("middleware test2 pre")
|
||||||
|
c.Next()
|
||||||
|
log.Println("middleware test2 post")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test3() framework.ControllerHandler {
|
||||||
|
return func(c *framework.Context) error {
|
||||||
|
log.Println("middleware test3 pre")
|
||||||
|
c.Next()
|
||||||
|
log.Println("middleware test3 post")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ func NewTrie() *Trie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Trie) FindRoute(uri string) []ControllerHandler {
|
func (t *Trie) FindRoute(uri string) []ControllerHandler {
|
||||||
|
uri = strings.ToUpper(uri)
|
||||||
uri = strings.TrimPrefix(uri, "/")
|
uri = strings.TrimPrefix(uri, "/")
|
||||||
if uri == "" {
|
if uri == "" {
|
||||||
return t.root.handlers
|
return t.root.handlers
|
||||||
@ -28,11 +29,12 @@ func (t *Trie) FindRoute(uri string) []ControllerHandler {
|
|||||||
return found.handlers
|
return found.handlers
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Trie) AddRouter(uri string, handler ControllerHandler) error {
|
func (t *Trie) AddRouter(uri string, handlers []ControllerHandler) error {
|
||||||
|
uri = strings.ToUpper(uri)
|
||||||
uri = strings.TrimPrefix(uri, "/")
|
uri = strings.TrimPrefix(uri, "/")
|
||||||
if uri == "" {
|
if uri == "" {
|
||||||
t.root.isLast = true
|
t.root.isLast = true
|
||||||
t.root.handlers = append(t.root.handlers, handler)
|
t.root.handlers = append(t.root.handlers, handlers...)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +46,7 @@ func (t *Trie) AddRouter(uri string, handler ControllerHandler) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The route does not exist, add it to the tree
|
// The route does not exist, add it to the tree
|
||||||
err := t.root.addRoute(upperUri, handler)
|
err := t.root.addRoute(upperUri, handlers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -109,7 +111,7 @@ func (n *node) findRoute(uri string) *node {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *node) addRoute(uri string, handler ControllerHandler) error {
|
func (n *node) addRoute(uri string, handlers []ControllerHandler) error {
|
||||||
splitted := strings.SplitN(uri, "/", 2)
|
splitted := strings.SplitN(uri, "/", 2)
|
||||||
splittedLen := len(splitted)
|
splittedLen := len(splitted)
|
||||||
isLast := splittedLen == 1
|
isLast := splittedLen == 1
|
||||||
@ -125,12 +127,12 @@ func (n *node) addRoute(uri string, handler ControllerHandler) error {
|
|||||||
} else {
|
} else {
|
||||||
// otherwise, set the child
|
// otherwise, set the child
|
||||||
child.isLast = true
|
child.isLast = true
|
||||||
child.handlers = append(child.handlers, handler)
|
child.handlers = append(child.handlers, handlers...)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// More segments to check
|
// More segments to check
|
||||||
return child.addRoute(splitted[1], handler)
|
return child.addRoute(splitted[1], handlers)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,14 +140,14 @@ func (n *node) addRoute(uri string, handler ControllerHandler) error {
|
|||||||
new := newNode(splitted[0])
|
new := newNode(splitted[0])
|
||||||
if isLast {
|
if isLast {
|
||||||
// this is the end
|
// this is the end
|
||||||
new.handlers = append(new.handlers, handler)
|
new.handlers = append(new.handlers, handlers...)
|
||||||
new.isLast = true
|
new.isLast = true
|
||||||
n.children = append(n.children, new)
|
n.children = append(n.children, new)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// continue
|
// continue
|
||||||
new.isLast = false
|
new.isLast = false
|
||||||
err := new.addRoute(splitted[1], handler)
|
err := new.addRoute(splitted[1], handlers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "git.vinchent.xyz/vinchent/go-web/framework"
|
import (
|
||||||
|
"git.vinchent.xyz/vinchent/go-web/framework"
|
||||||
|
"git.vinchent.xyz/vinchent/go-web/framework/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
func registerRouter(core *framework.Core) {
|
func registerRouter(core *framework.Core) {
|
||||||
|
core.Use(middleware.Test1(), middleware.Test2())
|
||||||
core.Get("/user/login", UserLoginController)
|
core.Get("/user/login", UserLoginController)
|
||||||
|
|
||||||
subjectApi := core.Group("/subject")
|
subjectApi := core.Group("/subject")
|
||||||
{
|
{
|
||||||
|
subjectApi.Use(middleware.Test3())
|
||||||
subjectApi.Delete("/:id", SubjectDelController)
|
subjectApi.Delete("/:id", SubjectDelController)
|
||||||
subjectApi.Put("/:id", SubjectUpdateController)
|
subjectApi.Put("/:id", SubjectUpdateController)
|
||||||
subjectApi.Get("/:id", SubjectGetController)
|
subjectApi.Get("/:id", SubjectGetController)
|
||||||
|
Loading…
Reference in New Issue
Block a user