定义中间件
- 中间件的作用是给应用添加一些额外的功能,但是不会影响原有应用的编码方式,想用的时候直接添加,不想用的时候也可以轻松去除,实现所谓的可插拔。
- 中间件的实现位置在哪里?
- 不能耦合在用户的代码中
- 需要独立存在,但是又能拿到上下文并作出影响
- 位置:在处理器的前后
- 注意:中间件是一个调用链条,所以在处理真正的业务之前可能会经过多个中间件。
type MiddlewareFunc func(handleFunc HandleFunc) HandleFunc
前置中间件
package zjgo
import (
"fmt"
"log"
"net/http"
)
const ANY = "ANY"
type HandleFunc func(ctx *Context)
type MiddlewareFunc func(handleFunc HandleFunc) HandleFunc
type routerGroup struct {
name string
handleFuncMap map[string]map[string]HandleFunc
handlerMethodMap map[string][]string
TreeNode *TreeNode
preMiddlewares []MiddlewareFunc
postMiddlewares []MiddlewareFunc
}
func (routerGroup *routerGroup) PreHandle(middlewareFunc ...MiddlewareFunc) {
routerGroup.preMiddlewares = append(routerGroup.preMiddlewares, middlewareFunc...)
}
func (routerGroup *routerGroup) PostHandle(middlewareFunc ...MiddlewareFunc) {
routerGroup.postMiddlewares = append(routerGroup.postMiddlewares, middlewareFunc...)
}
func (routerGroup *routerGroup) methodHandle(handle HandleFunc, ctx *Context) {
if routerGroup.preMiddlewares != nil {
for _, middlewareFunc := range routerGroup.preMiddlewares {
handle = middlewareFunc(handle)
}
}
handle(ctx)
}
type router struct {
routerGroups []*routerGroup
}
func (r *router) Group(name string) *routerGroup {
routerGroup := &routerGroup{
name: name,
handleFuncMap: make(map[string]map[string]HandleFunc),
handlerMethodMap: make(map[string][]string),
TreeNode: &TreeNode{name: "/", children: make([]*TreeNode, 0)},
}
r.routerGroups = append(r.routerGroups, routerGroup)
return routerGroup
}
func (routerGroup *routerGroup) handleRequest(name string, method string, handleFunc HandleFunc) {
if _, exist := routerGroup.handleFuncMap[name]; !exist {
routerGroup.handleFuncMap[name] = make(map[string]HandleFunc)
}
if _, exist := routerGroup.handleFuncMap[name][method]; !exist {
routerGroup.handleFuncMap[name][method] = handleFunc
routerGroup.handlerMethodMap[method] = append(routerGroup.handlerMethodMap[method], name)
} else {
panic("Under the same route, duplication is not allowed!!!")
}
routerGroup.TreeNode.Put(name)
}
func (routerGroup *routerGroup) Any(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, ANY, handleFunc)
}
func (routerGroup *routerGroup) Post(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodPost, handleFunc)
}
func (routerGroup *routerGroup) Get(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodGet, handleFunc)
}
func (routerGroup *routerGroup) Delete(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodDelete, handleFunc)
}
func (routerGroup *routerGroup) Put(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodPut, handleFunc)
}
func (routerGroup *routerGroup) Patch(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodPatch, handleFunc)
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.httpRequestHandle(w, r)
}
func (e *Engine) httpRequestHandle(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
fmt.Fprintf(w, "这是一个 GET 请求!!! ")
} else if r.Method == http.MethodPost {
fmt.Fprintf(w, "这是一个 POST 请求!!! ")
} else {
fmt.Fprintf(w, "这是一个其他类型的请求:%s!!! ", r.Method)
}
for _, group := range e.routerGroups {
routerName := SubStringLast(r.RequestURI, "/"+group.name)
if node := group.TreeNode.Get(routerName); node != nil && node.isEnd {
ctx := &Context{W: w, R: r}
if handle, exist := group.handleFuncMap[node.routerName][ANY]; exist {
group.methodHandle(handle, ctx)
return
}
if handle, exist := group.handleFuncMap[node.routerName][r.Method]; exist {
group.methodHandle(handle, ctx)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "%s %s not allowed!!!\n", r.Method, r.RequestURI)
return
}
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "%s %s not found!!!\n", r.Method, r.RequestURI)
return
}
type Engine struct {
router
}
func New() *Engine {
return &Engine{
router: router{},
}
}
func (e *Engine) Run() {
http.Handle("/", e)
err := http.ListenAndServe(":3986", nil)
if err != nil {
log.Fatal(err)
}
}
package main
import (
"fmt"
"net/http"
"github.com/ErizJ/ZJGo/zjgo"
)
func main() {
fmt.Println("Hello World!")
engine := zjgo.New()
g1 := engine.Group("user")
g1.PreHandle(func(handleFunc zjgo.HandleFunc) zjgo.HandleFunc {
return func(ctx *zjgo.Context) {
fmt.Println("Pre Middleware ON!!!")
handleFunc(ctx)
}
})
g1.Get("/hello", func(ctx *zjgo.Context) {
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/hello")
})
g1.Post("/info", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodPost+" Hello Go!——user/info——POST")
})
g1.Get("/info", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/info——GET")
})
g1.Get("/get/:id", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/get/:id——GET")
})
g1.Get("/isEnd/get", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/isEnd/get——GET")
})
g1.Any("/any", func(ctx *zjgo.Context) {
fmt.Fprintf(ctx.W, " Hello Go!——user/any")
})
fmt.Println("Starting...")
engine.Run()
}
后置中间件
package zjgo
import (
"fmt"
"log"
"net/http"
)
const ANY = "ANY"
type HandleFunc func(ctx *Context)
type MiddlewareFunc func(handleFunc HandleFunc) HandleFunc
type routerGroup struct {
name string
handleFuncMap map[string]map[string]HandleFunc
handlerMethodMap map[string][]string
TreeNode *TreeNode
preMiddlewares []MiddlewareFunc
postMiddlewares []MiddlewareFunc
}
func (routerGroup *routerGroup) PreHandle(middlewareFunc ...MiddlewareFunc) {
routerGroup.preMiddlewares = append(routerGroup.preMiddlewares, middlewareFunc...)
}
func (routerGroup *routerGroup) PostHandle(middlewareFunc ...MiddlewareFunc) {
routerGroup.postMiddlewares = append(routerGroup.postMiddlewares, middlewareFunc...)
}
func (routerGroup *routerGroup) methodHandle(handle HandleFunc, ctx *Context) {
if routerGroup.preMiddlewares != nil {
for _, middlewareFunc := range routerGroup.preMiddlewares {
handle = middlewareFunc(handle)
}
}
handle(ctx)
if routerGroup.postMiddlewares != nil {
for _, middlewareFunc := range routerGroup.postMiddlewares {
handle = middlewareFunc(handle)
}
}
handle(ctx)
}
type router struct {
routerGroups []*routerGroup
}
func (r *router) Group(name string) *routerGroup {
routerGroup := &routerGroup{
name: name,
handleFuncMap: make(map[string]map[string]HandleFunc),
handlerMethodMap: make(map[string][]string),
TreeNode: &TreeNode{name: "/", children: make([]*TreeNode, 0)},
}
r.routerGroups = append(r.routerGroups, routerGroup)
return routerGroup
}
func (routerGroup *routerGroup) handleRequest(name string, method string, handleFunc HandleFunc) {
if _, exist := routerGroup.handleFuncMap[name]; !exist {
routerGroup.handleFuncMap[name] = make(map[string]HandleFunc)
}
if _, exist := routerGroup.handleFuncMap[name][method]; !exist {
routerGroup.handleFuncMap[name][method] = handleFunc
routerGroup.handlerMethodMap[method] = append(routerGroup.handlerMethodMap[method], name)
} else {
panic("Under the same route, duplication is not allowed!!!")
}
routerGroup.TreeNode.Put(name)
}
func (routerGroup *routerGroup) Any(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, ANY, handleFunc)
}
func (routerGroup *routerGroup) Post(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodPost, handleFunc)
}
func (routerGroup *routerGroup) Get(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodGet, handleFunc)
}
func (routerGroup *routerGroup) Delete(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodDelete, handleFunc)
}
func (routerGroup *routerGroup) Put(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodPut, handleFunc)
}
func (routerGroup *routerGroup) Patch(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodPatch, handleFunc)
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.httpRequestHandle(w, r)
}
func (e *Engine) httpRequestHandle(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
fmt.Fprintf(w, "这是一个 GET 请求!!! ")
} else if r.Method == http.MethodPost {
fmt.Fprintf(w, "这是一个 POST 请求!!! ")
} else {
fmt.Fprintf(w, "这是一个其他类型的请求:%s!!! ", r.Method)
}
for _, group := range e.routerGroups {
routerName := SubStringLast(r.RequestURI, "/"+group.name)
if node := group.TreeNode.Get(routerName); node != nil && node.isEnd {
ctx := &Context{W: w, R: r}
if handle, exist := group.handleFuncMap[node.routerName][ANY]; exist {
group.methodHandle(handle, ctx)
return
}
if handle, exist := group.handleFuncMap[node.routerName][r.Method]; exist {
group.methodHandle(handle, ctx)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "%s %s not allowed!!!\n", r.Method, r.RequestURI)
return
}
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "%s %s not found!!!\n", r.Method, r.RequestURI)
return
}
type Engine struct {
router
}
func New() *Engine {
return &Engine{
router: router{},
}
}
func (e *Engine) Run() {
http.Handle("/", e)
err := http.ListenAndServe(":3986", nil)
if err != nil {
log.Fatal(err)
}
}
package main
import (
"fmt"
"net/http"
"github.com/ErizJ/ZJGo/zjgo"
)
func main() {
fmt.Println("Hello World!")
engine := zjgo.New()
g1 := engine.Group("user")
g1.PreHandle(func(handleFunc zjgo.HandleFunc) zjgo.HandleFunc {
return func(ctx *zjgo.Context) {
fmt.Println("Pre Middleware ON!!!")
handleFunc(ctx)
}
})
g1.PostHandle(func(handleFunc zjgo.HandleFunc) zjgo.HandleFunc {
return func(ctx *zjgo.Context) {
fmt.Println("Post Middleware ON!!!")
}
})
g1.Get("/hello", func(ctx *zjgo.Context) {
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/hello")
})
g1.Post("/info", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodPost+" Hello Go!——user/info——POST")
})
g1.Get("/info", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/info——GET")
})
g1.Get("/get/:id", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/get/:id——GET")
})
g1.Get("/isEnd/get", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/isEnd/get——GET")
})
g1.Any("/any", func(ctx *zjgo.Context) {
fmt.Fprintf(ctx.W, " Hello Go!——user/any")
})
fmt.Println("Starting...")
engine.Run()
}
- 但是这里前置和后置中间件似乎有点多余了,因为在前置中间件中,执行完一系列前置中间件和主体业务函数后,就可以执行后置中间件了,不用其他冗余代码。
package zjgo
import (
"fmt"
"log"
"net/http"
)
const ANY = "ANY"
type HandleFunc func(ctx *Context)
type MiddlewareFunc func(handleFunc HandleFunc) HandleFunc
type routerGroup struct {
name string
handleFuncMap map[string]map[string]HandleFunc
handlerMethodMap map[string][]string
TreeNode *TreeNode
middlewares []MiddlewareFunc
}
func (routerGroup *routerGroup) UseMiddleware(middlewareFunc ...MiddlewareFunc) {
routerGroup.middlewares = append(routerGroup.middlewares, middlewareFunc...)
}
func (routerGroup *routerGroup) methodHandle(handle HandleFunc, ctx *Context) {
if routerGroup.middlewares != nil {
for _, middlewareFunc := range routerGroup.middlewares {
handle = middlewareFunc(handle)
}
}
handle(ctx)
}
type router struct {
routerGroups []*routerGroup
}
func (r *router) Group(name string) *routerGroup {
routerGroup := &routerGroup{
name: name,
handleFuncMap: make(map[string]map[string]HandleFunc),
handlerMethodMap: make(map[string][]string),
TreeNode: &TreeNode{name: "/", children: make([]*TreeNode, 0)},
}
r.routerGroups = append(r.routerGroups, routerGroup)
return routerGroup
}
func (routerGroup *routerGroup) handleRequest(name string, method string, handleFunc HandleFunc) {
if _, exist := routerGroup.handleFuncMap[name]; !exist {
routerGroup.handleFuncMap[name] = make(map[string]HandleFunc)
}
if _, exist := routerGroup.handleFuncMap[name][method]; !exist {
routerGroup.handleFuncMap[name][method] = handleFunc
routerGroup.handlerMethodMap[method] = append(routerGroup.handlerMethodMap[method], name)
} else {
panic("Under the same route, duplication is not allowed!!!")
}
routerGroup.TreeNode.Put(name)
}
func (routerGroup *routerGroup) Any(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, ANY, handleFunc)
}
func (routerGroup *routerGroup) Post(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodPost, handleFunc)
}
func (routerGroup *routerGroup) Get(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodGet, handleFunc)
}
func (routerGroup *routerGroup) Delete(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodDelete, handleFunc)
}
func (routerGroup *routerGroup) Put(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodPut, handleFunc)
}
func (routerGroup *routerGroup) Patch(name string, handleFunc HandleFunc) {
routerGroup.handleRequest(name, http.MethodPatch, handleFunc)
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.httpRequestHandle(w, r)
}
func (e *Engine) httpRequestHandle(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
fmt.Fprintf(w, "这是一个 GET 请求!!! ")
} else if r.Method == http.MethodPost {
fmt.Fprintf(w, "这是一个 POST 请求!!! ")
} else {
fmt.Fprintf(w, "这是一个其他类型的请求:%s!!! ", r.Method)
}
for _, group := range e.routerGroups {
routerName := SubStringLast(r.RequestURI, "/"+group.name)
if node := group.TreeNode.Get(routerName); node != nil && node.isEnd {
ctx := &Context{W: w, R: r}
if handle, exist := group.handleFuncMap[node.routerName][ANY]; exist {
group.methodHandle(handle, ctx)
return
}
if handle, exist := group.handleFuncMap[node.routerName][r.Method]; exist {
group.methodHandle(handle, ctx)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "%s %s not allowed!!!\n", r.Method, r.RequestURI)
return
}
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "%s %s not found!!!\n", r.Method, r.RequestURI)
return
}
type Engine struct {
router
}
func New() *Engine {
return &Engine{
router: router{},
}
}
func (e *Engine) Run() {
http.Handle("/", e)
err := http.ListenAndServe(":3986", nil)
if err != nil {
log.Fatal(err)
}
}
package main
import (
"fmt"
"net/http"
"github.com/ErizJ/ZJGo/zjgo"
)
func main() {
fmt.Println("Hello World!")
engine := zjgo.New()
g1 := engine.Group("user")
g1.UseMiddleware(func(handleFunc zjgo.HandleFunc) zjgo.HandleFunc {
return func(ctx *zjgo.Context) {
fmt.Println("Pre Middleware ON!!!")
handleFunc(ctx)
fmt.Println("Post Middleware ON!!!")
}
})
g1.Get("/hello", func(ctx *zjgo.Context) {
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/hello")
})
g1.Post("/info", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodPost+" Hello Go!——user/info——POST")
})
g1.Get("/info", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/info——GET")
})
g1.Get("/get/:id", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/get/:id——GET")
})
g1.Get("/isEnd/get", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/isEnd/get——GET")
})
g1.Any("/any", func(ctx *zjgo.Context) {
fmt.Fprintf(ctx.W, " Hello Go!——user/any")
})
fmt.Println("Starting...")
engine.Run()
}
路由级别中间件
package zjgo
import (
"fmt"
"log"
"net/http"
)
const ANY = "ANY"
type HandleFunc func(ctx *Context)
type MiddlewareFunc func(handleFunc HandleFunc) HandleFunc
type routerGroup struct {
name string
handleFuncMap map[string]map[string]HandleFunc
handlerMethodMap map[string][]string
middlewaresFuncMap map[string]map[string][]MiddlewareFunc
TreeNode *TreeNode
middlewares []MiddlewareFunc
}
func (routerGroup *routerGroup) UseMiddleware(middlewareFunc ...MiddlewareFunc) {
routerGroup.middlewares = append(routerGroup.middlewares, middlewareFunc...)
}
func (routerGroup *routerGroup) methodHandle(name string, method string, handle HandleFunc, ctx *Context) {
if routerGroup.middlewares != nil {
for _, middlewareFunc := range routerGroup.middlewares {
handle = middlewareFunc(handle)
}
}
if _, exist := routerGroup.middlewaresFuncMap[name][method]; exist {
for _, middlewareFunc := range routerGroup.middlewaresFuncMap[name][method] {
handle = middlewareFunc(handle)
}
}
handle(ctx)
}
type router struct {
routerGroups []*routerGroup
}
func (r *router) Group(name string) *routerGroup {
routerGroup := &routerGroup{
name: name,
handleFuncMap: make(map[string]map[string]HandleFunc),
handlerMethodMap: make(map[string][]string),
middlewaresFuncMap: make(map[string]map[string][]MiddlewareFunc, 0),
TreeNode: &TreeNode{name: "/", children: make([]*TreeNode, 0)},
}
r.routerGroups = append(r.routerGroups, routerGroup)
return routerGroup
}
func (routerGroup *routerGroup) registerRoute(name string, method string, handleFunc HandleFunc, middlewareFunc ...MiddlewareFunc) {
if _, exist := routerGroup.handleFuncMap[name]; !exist {
routerGroup.handleFuncMap[name] = make(map[string]HandleFunc)
routerGroup.middlewaresFuncMap[name] = make(map[string][]MiddlewareFunc)
}
if _, exist := routerGroup.handleFuncMap[name][method]; !exist {
routerGroup.handleFuncMap[name][method] = handleFunc
routerGroup.handlerMethodMap[method] = append(routerGroup.handlerMethodMap[method], name)
routerGroup.middlewaresFuncMap[name][method] = append(routerGroup.middlewaresFuncMap[name][method], middlewareFunc...)
} else {
panic("Under the same route, duplication is not allowed!!!")
}
routerGroup.TreeNode.Put(name)
}
func (routerGroup *routerGroup) Any(name string, handleFunc HandleFunc, middlewareFunc ...MiddlewareFunc) {
routerGroup.registerRoute(name, ANY, handleFunc, middlewareFunc...)
}
func (routerGroup *routerGroup) Post(name string, handleFunc HandleFunc, middlewareFunc ...MiddlewareFunc) {
routerGroup.registerRoute(name, http.MethodPost, handleFunc, middlewareFunc...)
}
func (routerGroup *routerGroup) Get(name string, handleFunc HandleFunc, middlewareFunc ...MiddlewareFunc) {
routerGroup.registerRoute(name, http.MethodGet, handleFunc, middlewareFunc...)
}
func (routerGroup *routerGroup) Delete(name string, handleFunc HandleFunc, middlewareFunc ...MiddlewareFunc) {
routerGroup.registerRoute(name, http.MethodDelete, handleFunc, middlewareFunc...)
}
func (routerGroup *routerGroup) Put(name string, handleFunc HandleFunc, middlewareFunc ...MiddlewareFunc) {
routerGroup.registerRoute(name, http.MethodPut, handleFunc, middlewareFunc...)
}
func (routerGroup *routerGroup) Patch(name string, handleFunc HandleFunc, middlewareFunc ...MiddlewareFunc) {
routerGroup.registerRoute(name, http.MethodPatch, handleFunc, middlewareFunc...)
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.httpRequestHandle(w, r)
}
func (e *Engine) httpRequestHandle(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
fmt.Fprintf(w, "这是一个 GET 请求!!! ")
} else if r.Method == http.MethodPost {
fmt.Fprintf(w, "这是一个 POST 请求!!! ")
} else {
fmt.Fprintf(w, "这是一个其他类型的请求:%s!!! ", r.Method)
}
for _, group := range e.routerGroups {
routerName := SubStringLast(r.RequestURI, "/"+group.name)
if node := group.TreeNode.Get(routerName); node != nil && node.isEnd {
ctx := &Context{W: w, R: r}
if handle, exist := group.handleFuncMap[node.routerName][ANY]; exist {
group.methodHandle(node.routerName, ANY, handle, ctx)
return
}
if handle, exist := group.handleFuncMap[node.routerName][r.Method]; exist {
group.methodHandle(node.routerName, r.Method, handle, ctx)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "%s %s not allowed!!!\n", r.Method, r.RequestURI)
return
}
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "%s %s not found!!!\n", r.Method, r.RequestURI)
return
}
type Engine struct {
router
}
func New() *Engine {
return &Engine{
router: router{},
}
}
func (e *Engine) Run() {
http.Handle("/", e)
err := http.ListenAndServe(":3986", nil)
if err != nil {
log.Fatal(err)
}
}
package main
import (
"fmt"
"net/http"
"github.com/ErizJ/ZJGo/zjgo"
)
func Log(handleFunc zjgo.HandleFunc) zjgo.HandleFunc {
return func(ctx *zjgo.Context) {
fmt.Println("[LOG] Middleware START")
handleFunc(ctx)
fmt.Println("[LOG] Middleware END")
}
}
func main() {
fmt.Println("Hello World!")
engine := zjgo.New()
g1 := engine.Group("user")
g1.UseMiddleware(func(handleFunc zjgo.HandleFunc) zjgo.HandleFunc {
return func(ctx *zjgo.Context) {
fmt.Println("Pre Middleware ON!!!")
handleFunc(ctx)
fmt.Println("Post Middleware ON!!!")
}
})
g1.Get("/hello", func(ctx *zjgo.Context) {
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/hello")
})
g1.Post("/info", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodPost+" Hello Go!——user/info——POST")
})
g1.Get("/info", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/info——GET")
}, Log)
g1.Get("/get/:id", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/get/:id——GET")
})
g1.Get("/isEnd/get", func(ctx *zjgo.Context) {
fmt.Println("HandleFunc ON!!!")
fmt.Fprintf(ctx.W, http.MethodGet+" Hello Go!——user/isEnd/get——GET")
})
g1.Any("/any", func(ctx *zjgo.Context) {
fmt.Fprintf(ctx.W, " Hello Go!——user/any")
})
fmt.Println("Starting...")
engine.Run()
}
[LOG] Middleware START
Pre Middleware ON!!!
HandleFunc ON!!!
Post Middleware ON!!!
[LOG] Middleware END