context: implement handler chain

This commit is contained in:
Muyao CHEN 2024-09-24 23:33:33 +02:00
parent e8c4cbaa89
commit 1aa9b78bdc
3 changed files with 35 additions and 17 deletions

View File

@ -19,7 +19,10 @@ type Context struct {
ctx context.Context ctx context.Context
request *http.Request request *http.Request
responseWriter http.ResponseWriter responseWriter http.ResponseWriter
handler ControllerHandler
handlers []ControllerHandler
// current handler index
index int
hasTimeout bool hasTimeout bool
writerMux *sync.Mutex writerMux *sync.Mutex
@ -32,6 +35,7 @@ func NewContext(w http.ResponseWriter, r *http.Request) *Context {
request: r, request: r,
responseWriter: w, responseWriter: w,
writerMux: &sync.Mutex{}, writerMux: &sync.Mutex{},
index: -1, // will be set to 0 when at the beginning
} }
} }
@ -99,8 +103,21 @@ func (ctx *Context) Value(key any) any {
// Next runs the next function in the function chain // Next runs the next function in the function chain
func (ctx *Context) Next() error { func (ctx *Context) Next() error {
// TODO ctx.index++
if ctx.index >= len(ctx.handlers) {
// This is the end of the chain
return nil return nil
}
// Run this handler
if err := ctx.handlers[ctx.index](ctx); err != nil {
return err
}
return nil
}
// SetHandlers sets handlers for context
func (ctx *Context) SetHandlers(handlers []ControllerHandler) {
ctx.handlers = handlers
} }
// }}} // }}}

View File

@ -60,7 +60,7 @@ func (c *Core) Delete(url string, handler ControllerHandler) {
} }
// 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) upperUri := strings.ToUpper(r.URL.Path)
upperMethod := strings.ToUpper(r.Method) upperMethod := strings.ToUpper(r.Method)
@ -70,13 +70,13 @@ func (c *Core) FindRouteByRequest(r *http.Request) ControllerHandler {
return nil return nil
} }
controller := mapper.FindRoute(upperUri) controllers := mapper.FindRoute(upperUri)
if controller == 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
} }
return controller return controllers
} }
func (c *Core) Group(prefix string) IGroup { func (c *Core) Group(prefix string) IGroup {
@ -92,14 +92,15 @@ func (c *Core) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(w, r) ctx := NewContext(w, r)
router := c.FindRouteByRequest(r) handlers := c.FindRouteByRequest(r)
if router == nil { if handlers == nil {
ctx.WriteJSON(http.StatusNotFound, "Request not found") ctx.WriteJSON(http.StatusNotFound, "Request not found")
return return
} }
err := router(ctx) ctx.SetHandlers(handlers)
if err != nil {
if err := ctx.Next(); err != nil {
ctx.WriteJSON(http.StatusInternalServerError, "Internal error") ctx.WriteJSON(http.StatusInternalServerError, "Internal error")
return return
} }

View File

@ -14,10 +14,10 @@ func NewTrie() *Trie {
return &Trie{root: newNode("")} return &Trie{root: newNode("")}
} }
func (t *Trie) FindRoute(uri string) ControllerHandler { func (t *Trie) FindRoute(uri string) []ControllerHandler {
uri = strings.TrimPrefix(uri, "/") uri = strings.TrimPrefix(uri, "/")
if uri == "" { if uri == "" {
return t.root.handler return t.root.handlers
} }
found := t.root.findRoute(uri) found := t.root.findRoute(uri)
@ -25,14 +25,14 @@ func (t *Trie) FindRoute(uri string) ControllerHandler {
return nil return nil
} }
return found.handler return found.handlers
} }
func (t *Trie) AddRouter(uri string, handler ControllerHandler) error { func (t *Trie) AddRouter(uri string, handler ControllerHandler) error {
uri = strings.TrimPrefix(uri, "/") uri = strings.TrimPrefix(uri, "/")
if uri == "" { if uri == "" {
t.root.isLast = true t.root.isLast = true
t.root.handler = handler t.root.handlers = append(t.root.handlers, handler)
return nil return nil
} }
@ -54,7 +54,7 @@ func (t *Trie) AddRouter(uri string, handler ControllerHandler) error {
type node struct { type node struct {
isLast bool isLast bool
segment string segment string
handler ControllerHandler handlers []ControllerHandler
children []*node children []*node
} }
@ -125,7 +125,7 @@ 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.handler = handler child.handlers = append(child.handlers, handler)
return nil return nil
} }
} }
@ -138,7 +138,7 @@ 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.handler = handler new.handlers = append(new.handlers, handler)
new.isLast = true new.isLast = true
n.children = append(n.children, new) n.children = append(n.children, new)
return nil return nil