From 197832931414cbad9dfeda9048345f1e5b424f99 Mon Sep 17 00:00:00 2001 From: yusing Date: Tue, 30 Dec 2025 21:49:47 +0800 Subject: [PATCH] feat(route): add CommandRoute for routing requests to specified routes - Introduced CommandRoute to handle routing requests to other defined routes. - Added validation to ensure a single argument is provided for the route. - Implemented command handler to serve the specified route or return a 404 error if not found. --- internal/route/rules/do.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/internal/route/rules/do.go b/internal/route/rules/do.go index 3189f586..4a4a17d4 100644 --- a/internal/route/rules/do.go +++ b/internal/route/rules/do.go @@ -15,6 +15,7 @@ import ( nettypes "github.com/yusing/godoxy/internal/net/types" "github.com/yusing/godoxy/internal/notif" "github.com/yusing/godoxy/internal/route/routes" + "github.com/yusing/godoxy/internal/types" gperr "github.com/yusing/goutils/errs" httputils "github.com/yusing/goutils/http" "github.com/yusing/goutils/http/reverseproxy" @@ -38,6 +39,7 @@ const ( CommandServe = "serve" CommandProxy = "proxy" CommandRedirect = "redirect" + CommandRoute = "route" CommandError = "error" CommandRequireBasicAuth = "require_basic_auth" CommandSet = "set" @@ -171,6 +173,42 @@ var commands = map[string]struct { }) }, }, + CommandRoute: { + help: Help{ + command: CommandRoute, + description: makeLines( + "Route the request to another route, e.g.:", + helpExample(CommandRoute, "route1"), + ), + args: map[string]string{ + "route": "the route to route to", + }, + }, + validate: func(args []string) (any, gperr.Error) { + if len(args) != 1 { + return nil, ErrExpectOneArg + } + return args[0], nil + }, + build: func(args any) CommandHandler { + route := args.(string) + return TerminatingCommand(func(w http.ResponseWriter, req *http.Request) error { + r, ok := routes.HTTP.Get(route) + if !ok { + excluded, has := routes.Excluded.Get(route) + if has { + r, ok = excluded.(types.HTTPRoute) + } + } + if ok { + r.ServeHTTP(w, req) + } else { + http.Error(w, fmt.Sprintf("Route %q not found", route), http.StatusNotFound) + } + return nil + }) + }, + }, CommandError: { help: Help{ command: CommandError,