context: Get params from url

This commit is contained in:
Muyao CHEN 2024-09-26 19:01:34 +02:00
parent cb1ea9f701
commit 7be01627fa
3 changed files with 42 additions and 11 deletions

View File

@ -19,7 +19,7 @@ type Context struct {
// current handler index
index int
params map[string]any
params map[string]string
hasTimeout bool
writerMux *sync.Mutex
@ -117,4 +117,8 @@ func (ctx *Context) SetHandlers(handlers []ControllerHandler) {
ctx.handlers = handlers
}
func (ctx *Context) SetParams(params map[string]string) {
ctx.params = params
}
// }}}

View File

@ -66,7 +66,7 @@ func (c *Core) Use(middlewares ...ControllerHandler) {
}
// FindRouteByRequest finds route using the request
func (c *Core) FindRouteByRequest(r *http.Request) []ControllerHandler {
func (c *Core) FindRouteByRequest(r *http.Request) *node {
upperMethod := strings.ToUpper(r.Method)
mapper, ok := c.router[upperMethod]
@ -75,13 +75,13 @@ func (c *Core) FindRouteByRequest(r *http.Request) []ControllerHandler {
return nil
}
controllers := mapper.FindRoute(r.URL.Path)
if controllers == nil {
node := mapper.FindRoute(r.URL.Path)
if node == nil {
log.Printf("URI %q is not recognized\n", r.URL.Path)
return nil
}
return controllers
return node
}
func (c *Core) Group(prefix string) IGroup {
@ -97,13 +97,16 @@ func (c *Core) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(w, r)
handlers := c.FindRouteByRequest(r)
if handlers == nil {
node := c.FindRouteByRequest(r)
if node == nil {
ctx.WriteJSON(http.StatusNotFound, "Request not found")
return
}
ctx.SetHandlers(handlers)
params := node.ParseParamsFromEndNode(r.URL.Path)
ctx.SetParams(params)
ctx.SetHandlers(node.handlers)
if err := ctx.Next(); err != nil {
ctx.WriteJSON(http.StatusInternalServerError, "Internal error")

View File

@ -14,11 +14,11 @@ func NewTrie() *Trie {
return &Trie{root: newNode("")}
}
func (t *Trie) FindRoute(uri string) []ControllerHandler {
func (t *Trie) FindRoute(uri string) *node {
uri = strings.ToUpper(uri)
uri = strings.TrimPrefix(uri, "/")
if uri == "" {
return t.root.handlers
return t.root
}
found := t.root.findRoute(uri)
@ -26,7 +26,7 @@ func (t *Trie) FindRoute(uri string) []ControllerHandler {
return nil
}
return found.handlers
return found
}
func (t *Trie) AddRouter(uri string, handlers []ControllerHandler) error {
@ -58,6 +58,29 @@ type node struct {
segment string
handlers []ControllerHandler
children []*node
parent *node
}
func (n *node) ParseParamsFromEndNode(uri string) map[string]string {
ret := map[string]string{}
uri = strings.ToUpper(uri)
uri = strings.TrimPrefix(uri, "/")
if uri == "" {
// root
return ret
}
segments := strings.Split(uri, "/")
cnt := len(segments)
cur := n
for i := cnt - 1; i >= 0; i-- {
if isWildcard(cur.segment) {
// set params
ret[cur.segment[1:]] = segments[i]
}
cur = cur.parent
}
return ret
}
func newNode(segment string) *node {
@ -138,6 +161,7 @@ func (n *node) addRoute(uri string, handlers []ControllerHandler) error {
// create a new node
new := newNode(splitted[0])
new.parent = n
if isLast {
// this is the end
new.handlers = append(new.handlers, handlers...)