From 1acef25987d2dff002253c02b028b4f2ecb44c84 Mon Sep 17 00:00:00 2001 From: x1unix Date: Wed, 27 Nov 2024 11:50:25 -0500 Subject: [PATCH] fix: refactor router wrapper --- examples/gno.land/r/demo/boards2/render.gno | 16 +++---- examples/gno.land/r/demo/boards2/router.gno | 51 +++++++++------------ 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/examples/gno.land/r/demo/boards2/render.gno b/examples/gno.land/r/demo/boards2/render.gno index d2acad6aace..3e3083d21ce 100644 --- a/examples/gno.land/r/demo/boards2/render.gno +++ b/examples/gno.land/r/demo/boards2/render.gno @@ -14,7 +14,7 @@ const ( func Render(path string) string { // TODO: remove this when query strings PR will be merged: https://github.com/gnolang/gno/pull/2594 - router := newWrappedRouter() + router := newWrappedRouter(mux.NewRouter()) router.HandleFunc("", renderBoardsList) router.HandleFunc("{board}", renderBoard) router.HandleFunc("{board}/{thread}", renderThread) @@ -27,11 +27,11 @@ func Render(path string) string { return router.Render(path) } -func renderBoardsList(res *mux.ResponseWriter, r request) { +func renderBoardsList(res *mux.ResponseWriter, r *mux.Request) { res.Write("These are all the boards of this realm:\n\n") pg := pager.NewPager(&gBoardsByID, defaultPageSize) - pageNum, limit, err := pg.ParseQuery(r.requestURI) + pageNum, limit, err := pg.ParseQuery(r.Path) if err != nil { panic(err) } @@ -49,7 +49,7 @@ func renderBoardsList(res *mux.ResponseWriter, r request) { } } -func renderBoard(res *mux.ResponseWriter, req request) { +func renderBoard(res *mux.ResponseWriter, req *mux.Request) { name := req.GetVar("board") v, found := gBoardsByName.Get(name) if !found { @@ -58,11 +58,11 @@ func renderBoard(res *mux.ResponseWriter, req request) { } board := v.(*Board) - p := req.pagination(defaultPageSize) + p := getPagination(req, defaultPageSize) res.Write(board.Render(p)) } -func renderThread(res *mux.ResponseWriter, req request) { +func renderThread(res *mux.ResponseWriter, req *mux.Request) { name := req.GetVar("board") v, found := gBoardsByName.Get(name) @@ -85,11 +85,11 @@ func renderThread(res *mux.ResponseWriter, req request) { return } - p := req.pagination(defaultPageSize) + p := getPagination(req, defaultPageSize) res.Write(thread.Render("", threadRenderDepth, &p)) } -func renderReply(res *mux.ResponseWriter, req request) { +func renderReply(res *mux.ResponseWriter, req *mux.Request) { name := req.GetVar("board") v, found := gBoardsByName.Get(name) if !found { diff --git a/examples/gno.land/r/demo/boards2/router.gno b/examples/gno.land/r/demo/boards2/router.gno index 4231a8847a5..21b5b7d50e6 100644 --- a/examples/gno.land/r/demo/boards2/router.gno +++ b/examples/gno.land/r/demo/boards2/router.gno @@ -7,29 +7,6 @@ import ( "gno.land/p/demo/mux" ) -type handleFunc = func(*mux.ResponseWriter, request) - -type request struct { - *mux.Request - - requestURI string -} - -func (r request) pagination(defaultPageSize int) Pagination { - // reuse query parameter extract logic from pager. - pg := pager.NewPager(nil, defaultPageSize) - pg.SizeQueryParam = "" - pageNum, limit, err := pg.ParseQuery(r.requestURI) - if err != nil { - panic(err) - } - - return Pagination{ - PageNumber: pageNum, - PageSize: limit, - } -} - type wrappedRouter struct { *mux.Router @@ -39,9 +16,9 @@ type wrappedRouter struct { // newWrappedRouter returns a wrapped mux router that supports query strings. // // TODO: remove this when query strings PR will be merged: https://github.com/gnolang/gno/pull/2594 -func newWrappedRouter() *wrappedRouter { +func newWrappedRouter(r *mux.Router) *wrappedRouter { return &wrappedRouter{ - Router: mux.NewRouter(), + Router: r, } } @@ -51,11 +28,11 @@ func (r *wrappedRouter) Render(path string) string { return r.Router.Render(reqPath) } -func (r *wrappedRouter) HandleFunc(pattern string, fn handleFunc) { +func (r *wrappedRouter) HandleFunc(pattern string, fn mux.HandlerFunc) { r.Router.HandleFunc(pattern, func(rw *mux.ResponseWriter, req *mux.Request) { - fn(rw, request{ - Request: req, - requestURI: r.requestURI, + fn(rw, &mux.Request{ + Path: r.requestURI, + HandlerPath: req.HandlerPath, }) }) } @@ -69,3 +46,19 @@ func trimRequestPath(path string) string { return path[:i] } + +func getPagination(r *mux.Request, defaultPageSize int) Pagination { + // reuse query parameter extract logic from pager. + pg := pager.NewPager(nil, defaultPageSize) + pg.SizeQueryParam = "" + pageNum, limit, err := pg.ParseQuery(r.Path) + if err != nil { + panic(err) + } + + return Pagination{ + PageNumber: pageNum, + PageSize: limit, + } +} +