feat: register routes with name
This commit is contained in:
parent
f25244b016
commit
92db29278d
5 changed files with 152 additions and 40 deletions
54
internal/routeurl/routeurl.go
Normal file
54
internal/routeurl/routeurl.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
// Package routeurl resolves named routes to concrete URL paths.
|
||||
// It is intentionally a dependency-free leaf package so templates in
|
||||
// internal/ui/... can import it without creating a cycle through the
|
||||
// routes/router/middleware/handler graph.
|
||||
package routeurl
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
mu sync.RWMutex
|
||||
registry = map[string]string{}
|
||||
)
|
||||
|
||||
// Register associates a name with a pattern path (e.g. "/join/{token}").
|
||||
// Called by router.Route.Name at route-registration time.
|
||||
func Register(name, path string) {
|
||||
mu.Lock()
|
||||
registry[name] = path
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
// Reset clears the registry. Intended for tests that want isolation
|
||||
// between SetupRoutes calls.
|
||||
func Reset() {
|
||||
mu.Lock()
|
||||
registry = map[string]string{}
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
// URL resolves a named route, substituting named wildcard segments from
|
||||
// key/value pairs. Unknown names return "#" so templates degrade gracefully
|
||||
// instead of panicking.
|
||||
//
|
||||
// Each key is replaced exactly once — net/http.ServeMux forbids duplicate
|
||||
// wildcard names in a single pattern, so this is safe by construction.
|
||||
//
|
||||
// routeurl.URL("page.public.join-space", "token", tok) // "/join/<tok>"
|
||||
func URL(name string, kv ...string) string {
|
||||
mu.RLock()
|
||||
path, ok := registry[name]
|
||||
mu.RUnlock()
|
||||
if !ok {
|
||||
return "#"
|
||||
}
|
||||
for i := 0; i+1 < len(kv); i += 2 {
|
||||
key, val := kv[i], kv[i+1]
|
||||
path = strings.Replace(path, "{"+key+"...}", val, 1)
|
||||
path = strings.Replace(path, "{"+key+"}", val, 1)
|
||||
}
|
||||
return path
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue