doc.gno
1.94 Kb · 40 lines
1// v0 - Unaudited: This is an initial version that has not yet been formally audited.
2// A fully audited version will be published as a subsequent release.
3// Use in production at your own risk.
4//
5// Package mux provides a simple routing and rendering library for handling dynamic path-based requests in Gno contracts.
6//
7// The `mux` package aims to offer similar functionality to `http.ServeMux` in Go, but for Gno's Render() requests.
8// It allows you to define routes with dynamic parts and associate them with corresponding handler functions for rendering outputs.
9//
10// Usage:
11// 1. Create a new Router instance using `NewRouter()` to handle routing and rendering logic.
12// 2. Register routes and their associated handler functions using the `Handle(route, handler)` method.
13// 3. Implement the rendering logic within the handler functions, utilizing the `Request` and `ResponseWriter` types.
14// 4. Use the `Render(path)` method to process a given path and execute the corresponding handler function to obtain the rendered output.
15//
16// Route Patterns:
17// Routes can include dynamic parts enclosed in braces, such as "users/{id}" or "hello/{name}". The `Request` object's `GetVar(key)`
18// method allows you to extract the value of a specific variable from the path based on routing rules.
19//
20// Example:
21//
22// router := mux.NewRouter()
23//
24// // Define a route with a variable and associated handler function
25// router.HandleFunc("hello/{name}", func(res *mux.ResponseWriter, req *mux.Request) {
26// name := req.GetVar("name")
27// if name != "" {
28// res.Write("Hello, " + name + "!")
29// } else {
30// res.Write("Hello, world!")
31// }
32// })
33//
34// // Render the output for the "/hello/Alice" path
35// output := router.Render("hello/Alice")
36// // Output: "Hello, Alice!"
37//
38// Note: The `mux` package provides a basic routing and rendering mechanism for simple use cases. For more advanced routing features,
39// consider using more specialized libraries or frameworks.
40package mux