README.md
3.79 Kb · 109 lines
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.
mux - Path router for Render
Simple routing and rendering library for Render(path) requests in Gno realms. Similar in spirit to http.ServeMux, with support for path variables ({name}), wildcards (*), and query strings.
Usage
1package myrealm
2
3import "gno.land/p/nt/mux/v0"
4
5var router *mux.Router
6
7func init() {
8 router = mux.NewRouter()
9
10 // Static route.
11 router.HandleFunc("", func(res *mux.ResponseWriter, req *mux.Request) {
12 res.Write("# Home\n")
13 })
14
15 // Named parameter.
16 router.HandleFunc("hello/{name}", func(res *mux.ResponseWriter, req *mux.Request) {
17 name := req.GetVar("name")
18 res.Write("Hello, " + name + "!")
19 })
20
21 // Query string.
22 router.HandleFunc("search", func(res *mux.ResponseWriter, req *mux.Request) {
23 q := req.Query.Get("q")
24 res.Write("Searching for: " + q)
25 })
26
27 // Wildcard - matches the rest of the path.
28 router.HandleFunc("files/*", func(res *mux.ResponseWriter, req *mux.Request) {
29 res.Write("File path: " + req.GetVar("*"))
30 })
31}
32
33// Realm entry point.
34func Render(path string) string {
35 return router.Render(path)
36}
API
1type Router struct {
2 NotFoundHandler NotFoundHandler
3 // unexported
4}
5
6func NewRouter() *Router
7
8func (r *Router) HandleFunc(pattern string, fn HandlerFunc)
9func (r *Router) HandleFuncRlm(pattern string, fn HandlerFuncRlm) // rlm-aware handler
10func (r *Router) HandleErrFunc(pattern string, fn ErrHandlerFunc)
11func (r *Router) SetNotFoundHandler(handler NotFoundHandler)
12func (r *Router) Render(reqPath string) string
13func (r *Router) RenderRlm(_ int, rlm realm, reqPath string) string // dispatches rlm-aware routes
14
15type Request struct {
16 Path string // path without query string
17 RawPath string // path including "?..." query string
18 HandlerPath string // pattern that matched this request
19 Query url.Values // parsed query parameters
20}
21
22func (r *Request) GetVar(key string) string
23
24type ResponseWriter struct{ /* unexported */ }
25
26func (rw *ResponseWriter) Write(data string)
27func (rw *ResponseWriter) Output() string
28
29type Handler struct {
30 Pattern string
31 Fn HandlerFunc // set by HandleFunc
32 FnRlm HandlerFuncRlm // set by HandleFuncRlm
33}
34
35type HandlerFunc func(*ResponseWriter, *Request)
36type HandlerFuncRlm func(_ int, rlm realm, res *ResponseWriter, req *Request)
37type ErrHandlerFunc func(*ResponseWriter, *Request) error
38type NotFoundHandler func(*ResponseWriter, *Request)
Route patterns
users- static, matches exactlyusers.users/{id}- named parameter, extracted withreq.GetVar("id").files/*- wildcard, captures all remaining segments. Extract withreq.GetVar("*").
Routes are matched in registration order; the first match wins. If no route matches, NotFoundHandler runs (default writes "404").
Notes
HandleErrFuncwraps an error-returning handler: a non-nil error is written as"Error: " + err.Error()to the response.- Query strings are parsed off
reqPath(?foo=bar); access viareq.Query(anet/url.Values). req.RawPathkeeps the original path including the query string;req.Pathstrips it.req.GetVar(...)andreq.Query.Get(...)return attacker-controlled path/query input. Wrap it withsanitize.InlineTextfromgno.land/p/nt/markdown/sanitize/v0before writing it into the response, or user input can inject Markdown structure.- Register realm-aware handlers with
HandleFuncRlmand dispatch them withRenderRlm(0, cur, path). The plainRenderpath only invokes non-rlmFnhandlers.