Skip to content

Commit

Permalink
fix: refactor router wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
x1unix committed Nov 27, 2024
1 parent 767db57 commit 1acef25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
16 changes: 8 additions & 8 deletions examples/gno.land/r/demo/boards2/render.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
51 changes: 22 additions & 29 deletions examples/gno.land/r/demo/boards2/router.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
}
}

Expand All @@ -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,
})
})
}
Expand All @@ -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,
}
}

0 comments on commit 1acef25

Please sign in to comment.