routing.gno

3.60 Kb ยท 111 lines
  1package routing
  2
  3import (
  4	"strings"
  5
  6	"gno.land/p/demo/mux"
  7	"gno.land/p/demo/ufmt"
  8	"gno.land/r/sys/users"
  9)
 10
 11func Render(path string) string {
 12	// Initialize the router
 13	router := mux.NewRouter()
 14
 15	// Then, pass specific path patterns and their function handlers
 16	// The handler functions need to have a specific signature:
 17	// func(*mux.ResponseWriter, *mux.Request)
 18
 19	// Below are some examples for specific path patterns and their handlers
 20
 21	// When no render path is passed, ie the root render
 22	router.HandleFunc("", homeHandler)
 23
 24	// When a specific render path is passed
 25	router.HandleFunc("staticpage", staticpageHandler)
 26
 27	// When a render path with a variable is passed, ie `addr`
 28	router.HandleFunc("user/{name}", profileHandler)
 29
 30	// When a wildcard path is passed
 31	router.HandleFunc("wildcard/*/", wildcardHandler)
 32
 33	// Finally, Pass the render path to the router
 34	return router.Render(path)
 35}
 36
 37func homeHandler(res *mux.ResponseWriter, _ *mux.Request) {
 38	out := "# Routing\n\n"
 39
 40	out += `This short example showcases how the [p/demo/mux](/p/demo/mux) package works.
 41This pure package aims to offer similar functionality to [**http.ServeMux**](https://pkg.go.dev/net/http#ServeMux) in Go, but for Gno's **Render()** requests.
 42
 43This home page is handled by the homeHandler function. Check out the examples below for more ways
 44to use this package:
 45- [A static page](/r/docs/routing:staticpage)
 46- [A path handling path variables, such as user pages](/r/docs/routing:user/test1)
 47- [A path handling wildcard](/r/docs/routing:wildcard/wildcardhelp/whatever)
 48`
 49
 50	// Write to the result at the end
 51	res.Write(out)
 52}
 53
 54func staticpageHandler(res *mux.ResponseWriter, req *mux.Request) {
 55	out := "# Welcome to the static page!\n\n"
 56	out += "There isn't much on this page, but it's cool because shows you that using query parameters is also possible!\n\n"
 57	out += "Try adding [`?gno_is=value`](/r/docs/routing:staticpage?gno_is=cool) to the URL.\n\n"
 58
 59	// We can use the Query API to get the values of query parameters
 60	val := req.Query.Get("gno_is")
 61	if val != "" {
 62		out += ufmt.Sprintf("You inputted a value for the parameter `gno_is`: **%s**\n\n", val)
 63
 64	}
 65
 66	out += "### Multiple values for the same parameter\n\n"
 67	out += "The same parameter can have [more than one value](/r/docs/routing:staticpage?gno_is=cool&gno_is=transparent).\n\n"
 68	out += "We can access it via the `*mux.Request.Query` map.\n\n"
 69
 70	res.Write(out)
 71}
 72
 73func profileHandler(res *mux.ResponseWriter, req *mux.Request) {
 74	out := "# User display page\n\n"
 75
 76	// Integrate with r/sys/users to get user data
 77	name := req.GetVar("name")
 78	userData, _ := users.ResolveName(name)
 79	if userData == nil {
 80		out += "This name does not exist in the [User Registry](/r/sys/users)!"
 81		res.Write(out)
 82		return
 83	}
 84
 85	out += "Found an address matching the name:\n\n"
 86	out += userData.RenderLink("")
 87	res.Write(out)
 88}
 89
 90func wildcardHandler(res *mux.ResponseWriter, req *mux.Request) {
 91	out := "# Wildcard handler\n\n"
 92
 93	// Use GetVar("*") to access the wildcard part of the route
 94	wild := req.GetVar("*")
 95	out += ufmt.Sprintf("This page matched a wildcard route.\n\n")
 96	out += ufmt.Sprintf("**Wildcard path:** `%s`\n\n", wild)
 97
 98	out += "You can use this to match and handle arbitrarily nested paths.\n\n"
 99	out += "Try visiting:\n"
100	out += "- [/r/docs/routing:wildcard/foo/bar](/r/docs/routing:wildcard/foo/bar)\n"
101	out += "- [/r/docs/routing:wildcard/a/b/c](/r/docs/routing:wildcard/a/b/c)\n"
102
103	// Optionally split the wildcard into segments
104	segments := strings.Split(wild, "/")
105	out += "\n### Wildcard segments:\n"
106	for i, seg := range segments {
107		out += ufmt.Sprintf("- Segment %d: `%s`\n", i+1, seg)
108	}
109
110	res.Write(out)
111}