add basic middleware handling
This is an initial spike on how the middleware might be included into the system. It is inspired/copied from GIN, so I still have to work out, how to make it nicer than it is at the moment. What is currently missing is Aborting and the addRoute function looks very ugly.
This commit is contained in:
parent
7a5cbc3c7a
commit
ce036f428d
|
@ -6,23 +6,39 @@ import (
|
|||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// the following is inspired by the gin framework
|
||||
|
||||
// this router defines each action to take for an URI including the middlewares
|
||||
type Router struct {
|
||||
// path that this engine takes care of relative to the parent
|
||||
path string
|
||||
// the list of functions to run on a request
|
||||
funcList []ContextFunc
|
||||
// the router to use as the main entity
|
||||
router *httprouter.Router
|
||||
}
|
||||
|
||||
// Bundle all parameters into the context to make it easier to push important
|
||||
// data into functions.
|
||||
type Context struct {
|
||||
// the current request
|
||||
Request *http.Request
|
||||
// the response
|
||||
Response http.ResponseWriter
|
||||
params httprouter.Params
|
||||
// parameters provided by the router
|
||||
Params httprouter.Params
|
||||
|
||||
// the list of functions to run
|
||||
funcList []ContextFunc
|
||||
// the current position in the function list
|
||||
current int
|
||||
}
|
||||
|
||||
// the func type for internal routes
|
||||
type ContextFunc func(*Context) error
|
||||
|
||||
type Router struct {
|
||||
router *httprouter.Router
|
||||
}
|
||||
|
||||
func NewRouter() *Router {
|
||||
return &Router{httprouter.New()}
|
||||
return &Router{"", make([]ContextFunc, 0), httprouter.New()}
|
||||
}
|
||||
|
||||
func (r *Router) Get(path string, target ContextFunc) {
|
||||
|
@ -41,11 +57,26 @@ func (r *Router) Delete(path string, target ContextFunc) {
|
|||
r.addRoute("DELETE", path, target)
|
||||
}
|
||||
|
||||
func (r *Router) addRoute(method, path string, target ContextFunc) {
|
||||
r.router.Handle(method, path, func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
log.Println("Handling Request for ", path)
|
||||
if err := target(&Context{r, w, p}); err != nil {
|
||||
log.Fatal(err)
|
||||
func (r *Router) Use(middleware ContextFunc) {
|
||||
r.funcList = addToFuncList(r.funcList, middleware)
|
||||
}
|
||||
|
||||
func (router *Router) addRoute(method, path string, target ContextFunc) {
|
||||
router.router.Handle(
|
||||
method,
|
||||
router.path + path,
|
||||
func(response http.ResponseWriter,
|
||||
request *http.Request,
|
||||
params httprouter.Params) {
|
||||
ctx := &Context{
|
||||
request,
|
||||
response,
|
||||
params,
|
||||
addToFuncList(router.funcList, target),
|
||||
0,
|
||||
}
|
||||
for i := 0; i < len(ctx.funcList); i++ {
|
||||
ctx.funcList[i](ctx)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -54,3 +85,10 @@ func (r *Router) Start() {
|
|||
log.Print("Starting to listen for incoming requests ...")
|
||||
log.Fatal(http.ListenAndServe(":9292", r.router))
|
||||
}
|
||||
|
||||
func addToFuncList(list []ContextFunc, element ContextFunc) []ContextFunc {
|
||||
new_list := make([]ContextFunc, len(list) + 1)
|
||||
copy(new_list, list)
|
||||
new_list[len(list)] = element
|
||||
return new_list
|
||||
}
|
||||
|
|
5
main.go
5
main.go
|
@ -1,12 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gibheer/zero-blog/lib"
|
||||
"github.com/gibheer/zero-blog/controller"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := lib.NewRouter()
|
||||
router.Use(func(c *lib.Context) error {
|
||||
fmt.Fprint(c.Response, "Hello says the middleware!")
|
||||
return nil
|
||||
})
|
||||
controller.DefineRoutes(router)
|
||||
router.Start()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue