func NewRouter
NewRouter creates a new Router instance.
v0 - Unaudited: This is an initial version that has not yet been formally audited. A fully audited version will be pu...
v0 - Unaudited This is an initial version of this package that has not yet been formally audited. A fully audited version will be published as a subsequent release. Use in production at your own risk.
Package mux provides a simple routing and rendering library for handling dynamic path-based requests in Gno contracts, similar to http.ServeMux in Go.
v0 - Unaudited: This is an initial version that has not yet been formally audited. A fully audited version will be published as a subsequent release. Use in production at your own risk.
Package mux provides a simple routing and rendering library for handling dynamic path-based requests in Gno contracts.
The `mux` package aims to offer similar functionality to `http.ServeMux` in Go, but for Gno's Render() requests. It allows you to define routes with dynamic parts and associate them with corresponding handler functions for rendering outputs.
Usage: 1. Create a new Router instance using `NewRouter()` to handle routing and rendering logic. 2. Register routes and their associated handler functions using the `Handle(route, handler)` method. 3. Implement the rendering logic within the handler functions, utilizing the `Request` and `ResponseWriter` types. 4. Use the `Render(path)` method to process a given path and execute the corresponding handler function to obtain the rendered output.
Route Patterns: Routes can include dynamic parts enclosed in braces, such as "users/{id}" or "hello/{name}". The `Request` object's `GetVar(key)` method allows you to extract the value of a specific variable from the path based on routing rules.
Example:
1router := mux.NewRouter()
2
3// Define a route with a variable and associated handler function
4router.HandleFunc("hello/{name}", func(res *mux.ResponseWriter, req *mux.Request) {
5 name := req.GetVar("name")
6 if name != "" {
7 res.Write("Hello, " + name + "!")
8 } else {
9 res.Write("Hello, world!")
10 }
11})
12
13// Render the output for the "/hello/Alice" path
14output := router.Render("hello/Alice")
15// Output: "Hello, Alice!"
Note: The `mux` package provides a basic routing and rendering mechanism for simple use cases. For more advanced routing features, consider using more specialized libraries or frameworks.
Handler stores a route pattern with one of two handler shapes. Fn (HandlerFunc, no rlm) is set by HandleFunc; FnRlm (HandlerFuncRlm, rlm-aware non-crossing) is set by HandleFuncRlm. Exactly one is set per route. RenderRlm dispatches FnRlm with the supplied rlm; Render dispatches Fn and panics if the matched route was registered with HandleFuncRlm (caller used the wrong dispatch method).
HandlerFuncRlm is the rlm-aware handler shape — non-crossing (`_ int, rlm realm` first params) so callers thread cur as data for the handler to forward to downstream crossing functions.
1type Request struct {
2 // Path is request path name.
3 //
4 // Note: use RawPath to obtain a raw path with query string.
5 Path string
6
7 // RawPath contains a whole request path, including query string.
8 RawPath string
9
10 // HandlerPath is handler rule that matches a request.
11 HandlerPath string
12
13 // Query contains the parsed URL query parameters.
14 Query url.Values
15}Request represents an incoming request.
ResponseWriter represents the response writer.
Router handles the routing and rendering logic.
HandleErrFunc registers a route and its error handler function.
HandleFunc registers a route and its handler function.
HandleFuncRlm registers a route with a rlm-aware handler. Dispatch must use Router.RenderRlm — calling Router.Render on a route registered via HandleFuncRlm panics (no rlm to supply).
Render renders the output for the given path using the registered route handler.
RenderRlm is the rlm-aware counterpart of Render. Dispatches matched routes registered via HandleFuncRlm with the supplied rlm; routes registered via the legacy HandleFunc still work — rlm is ignored. Use this when the router carries any rlm-aware handlers.
SetNotFoundHandler sets custom message for 404 defaultNotFoundHandler.