context: Get params from url

This commit is contained in:
Muyao CHEN 2024-09-26 19:01:34 +02:00
parent cb1ea9f701
commit 0786a97a77
5 changed files with 44 additions and 11 deletions

View File

@ -19,7 +19,7 @@ type Context struct {
// current handler index // current handler index
index int index int
params map[string]any params map[string]string
hasTimeout bool hasTimeout bool
writerMux *sync.Mutex writerMux *sync.Mutex
@ -117,4 +117,8 @@ func (ctx *Context) SetHandlers(handlers []ControllerHandler) {
ctx.handlers = handlers 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 // 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) upperMethod := strings.ToUpper(r.Method)
mapper, ok := c.router[upperMethod] mapper, ok := c.router[upperMethod]
@ -75,13 +75,13 @@ func (c *Core) FindRouteByRequest(r *http.Request) []ControllerHandler {
return nil return nil
} }
controllers := mapper.FindRoute(r.URL.Path) node := mapper.FindRoute(r.URL.Path)
if controllers == nil { if node == 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 controllers return node
} }
func (c *Core) Group(prefix string) IGroup { 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) ctx := NewContext(w, r)
handlers := c.FindRouteByRequest(r) node := c.FindRouteByRequest(r)
if handlers == nil { if node == nil {
ctx.WriteJSON(http.StatusNotFound, "Request not found") ctx.WriteJSON(http.StatusNotFound, "Request not found")
return return
} }
ctx.SetHandlers(handlers) params := node.ParseParamsFromEndNode(r.URL.Path)
ctx.SetParams(params)
ctx.SetHandlers(node.handlers)
if err := ctx.Next(); err != nil { if err := ctx.Next(); err != nil {
ctx.WriteJSON(http.StatusInternalServerError, "Internal error") ctx.WriteJSON(http.StatusInternalServerError, "Internal error")

View File

@ -14,11 +14,11 @@ func NewTrie() *Trie {
return &Trie{root: newNode("")} return &Trie{root: newNode("")}
} }
func (t *Trie) FindRoute(uri string) []ControllerHandler { func (t *Trie) FindRoute(uri string) *node {
uri = strings.ToUpper(uri) uri = strings.ToUpper(uri)
uri = strings.TrimPrefix(uri, "/") uri = strings.TrimPrefix(uri, "/")
if uri == "" { if uri == "" {
return t.root.handlers return t.root
} }
found := t.root.findRoute(uri) found := t.root.findRoute(uri)
@ -26,7 +26,7 @@ func (t *Trie) FindRoute(uri string) []ControllerHandler {
return nil return nil
} }
return found.handlers return found
} }
func (t *Trie) AddRouter(uri string, handlers []ControllerHandler) error { func (t *Trie) AddRouter(uri string, handlers []ControllerHandler) error {
@ -58,6 +58,29 @@ type node struct {
segment string segment string
handlers []ControllerHandler handlers []ControllerHandler
children []*node 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 { func newNode(segment string) *node {
@ -138,6 +161,7 @@ func (n *node) addRoute(uri string, handlers []ControllerHandler) error {
// create a new node // create a new node
new := newNode(splitted[0]) new := newNode(splitted[0])
new.parent = n
if isLast { if isLast {
// this is the end // this is the end
new.handlers = append(new.handlers, handlers...) new.handlers = append(new.handlers, handlers...)

View File

@ -74,6 +74,7 @@ func SubjectUpdateController(ctx *framework.Context) error {
func SubjectGetController(ctx *framework.Context) error { func SubjectGetController(ctx *framework.Context) error {
ctx.WriteJSON(http.StatusAccepted, "got") ctx.WriteJSON(http.StatusAccepted, "got")
log.Println(ctx.ParamInt("ID", 0))
return nil return nil
} }

View File

@ -15,6 +15,7 @@ func registerRouter(core *framework.Core) {
subjectApi.Delete("/:id", SubjectDelController) subjectApi.Delete("/:id", SubjectDelController)
subjectApi.Put("/:id", SubjectUpdateController) subjectApi.Put("/:id", SubjectUpdateController)
subjectApi.Get("/:id", SubjectGetController) subjectApi.Get("/:id", SubjectGetController)
subjectApi.Get("/:id/test", SubjectGetController)
subjectApi.Get("/list/all", SubjectListController) subjectApi.Get("/list/all", SubjectListController)
} }
} }