feat: register routes with name

This commit is contained in:
juancwu 2026-04-11 15:17:25 +00:00
commit 92db29278d
5 changed files with 152 additions and 40 deletions

View file

@ -9,10 +9,13 @@ import (
"git.juancwu.dev/juancwu/budgit/internal/app"
"git.juancwu.dev/juancwu/budgit/internal/handler"
"git.juancwu.dev/juancwu/budgit/internal/middleware"
"git.juancwu.dev/juancwu/budgit/internal/routeurl"
"git.juancwu.dev/juancwu/budgit/internal/router"
)
func SetupRoutes(a *app.App) http.Handler {
routeurl.Reset()
authH := handler.NewAuthHandler(a.AuthService, a.InviteService, a.SpaceService)
homeH := handler.NewHomeHandler()
settingsH := handler.NewSettingsHandler(a.AuthService, a.UserService)
@ -45,11 +48,11 @@ func SetupRoutes(a *app.App) http.Handler {
)
// Public pages
r.Get("/{$}", homeH.HomePage)
r.Get("/forbidden", homeH.ForbiddenPage)
r.Get("/privacy", homeH.PrivacyPage)
r.Get("/terms", homeH.TermsPage)
r.Get("/join/{token}", authH.JoinSpace)
r.Get("/{$}", homeH.HomePage).Name("page.public.home")
r.Get("/forbidden", homeH.ForbiddenPage).Name("page.public.forbidden")
r.Get("/privacy", homeH.PrivacyPage).Name("page.public.privacy")
r.Get("/terms", homeH.TermsPage).Name("page.public.terms")
r.Get("/join/{token}", authH.JoinSpace).Name("page.public.join-space")
// Permanent redirects
r.Get("/app/dashboard", redirectH.Spaces)
@ -57,39 +60,39 @@ func SetupRoutes(a *app.App) http.Handler {
// Auth - guest routes
r.Group("/auth", func(g *router.Group) {
g.Use(middleware.RequireGuest)
g.Get("", authH.AuthPage)
g.Get("/password", authH.PasswordPage)
g.Get("/magic-link/{token}", authH.VerifyMagicLink)
g.Get("", authH.AuthPage).Name("page.auth.index")
g.Get("/password", authH.PasswordPage).Name("page.auth.password")
g.Get("/magic-link/{token}", authH.VerifyMagicLink).Name("page.auth.magic-link.verify")
g.SubGroup("", func(g *router.Group) {
g.RateLimit(5, 15*time.Minute)
g.Post("/magic-link", authH.SendMagicLink)
g.Post("/password", authH.LoginWithPassword)
g.Post("/magic-link", authH.SendMagicLink).Name("action.auth.magic-link.send")
g.Post("/password", authH.LoginWithPassword).Name("action.auth.password.login")
})
})
// Auth - authenticated routes
r.Group("/auth", func(g *router.Group) {
g.Use(middleware.RequireAuth)
g.Get("/onboarding", authH.OnboardingPage)
g.Post("/onboarding", authH.CompleteOnboarding)
g.Get("/onboarding", authH.OnboardingPage).Name("page.auth.onboarding")
g.Post("/onboarding", authH.CompleteOnboarding).Name("action.auth.onboarding.complete")
})
r.Post("/auth/logout", authH.Logout)
r.Post("/auth/logout", authH.Logout).Name("action.auth.logout")
// App routes
r.Group("/app", func(g *router.Group) {
g.Use(middleware.RequireAuth)
g.SubGroup("/spaces", func(g *router.Group) {
g.Get("", spaceH.SpacesPage)
g.Get("", spaceH.SpacesPage).Name("page.app.spaces")
})
g.SubGroup("/settings", func(g *router.Group) {
g.Get("", settingsH.SettingsPage)
g.Get("", settingsH.SettingsPage).Name("page.app.settings")
g.SubGroup("", func(g *router.Group) {
g.RateLimit(5, 15*time.Minute)
g.Post("/password", settingsH.SetPassword)
g.Post("/password", settingsH.SetPassword).Name("action.app.settings.password.set")
})
})
})

View file

@ -8,6 +8,7 @@ import (
"git.juancwu.dev/juancwu/budgit/internal/app"
"git.juancwu.dev/juancwu/budgit/internal/model"
"git.juancwu.dev/juancwu/budgit/internal/repository"
"git.juancwu.dev/juancwu/budgit/internal/routeurl"
"git.juancwu.dev/juancwu/budgit/internal/service"
"git.juancwu.dev/juancwu/budgit/internal/testutil"
"github.com/stretchr/testify/assert"
@ -217,6 +218,18 @@ func TestSetupRoutes_NotFound(t *testing.T) {
})
}
func TestURL_ResolvesNamedRoute(t *testing.T) {
testutil.ForEachDB(t, func(t *testing.T, dbi testutil.DBInfo) {
a := newTestApp(dbi)
SetupRoutes(a)
assert.Equal(t, "/privacy", routeurl.URL("page.public.privacy"))
assert.Equal(t, "/app/spaces", routeurl.URL("page.app.spaces"))
assert.Equal(t, "/join/abc123", routeurl.URL("page.public.join-space", "token", "abc123"))
assert.Equal(t, "#", routeurl.URL("does.not.exist"))
})
}
func TestSetupRoutes_StaticAssets(t *testing.T) {
testutil.ForEachDB(t, func(t *testing.T, dbi testutil.DBInfo) {
a := newTestApp(dbi)