This commit is contained in:
yusing
2026-02-16 08:59:01 +08:00
parent 15b9635ee1
commit e4e6f6b3e8
242 changed files with 3953 additions and 3502 deletions

View File

@@ -10,6 +10,7 @@ import (
"strings"
"github.com/rs/zerolog"
entrypoint "github.com/yusing/godoxy/internal/entrypoint/types"
"github.com/yusing/godoxy/internal/logging"
gphttp "github.com/yusing/godoxy/internal/net/gphttp"
nettypes "github.com/yusing/godoxy/internal/net/types"
@@ -71,10 +72,11 @@ var commands = map[string]struct {
description: makeLines("Require HTTP authentication for incoming requests"),
args: map[string]string{},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
if len(args) != 0 {
return nil, ErrExpectNoArg
}
//nolint:nilnil
return nil, nil
},
build: func(args any) CommandHandler {
@@ -102,17 +104,17 @@ var commands = map[string]struct {
"to": "the path to rewrite to, must start with /",
},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
if len(args) != 2 {
return nil, ErrExpectTwoArgs
}
path1, err1 := validateURLPath(args[:1])
path2, err2 := validateURLPath(args[1:])
if err1 != nil {
err1 = gperr.Errorf("from: %w", err1)
err1 = gperr.PrependSubject(err1, "from")
}
if err2 != nil {
err2 = gperr.Errorf("to: %w", err2)
err2 = gperr.PrependSubject(err2, "to")
}
if err1 != nil || err2 != nil {
return nil, gperr.Join(err1, err2)
@@ -188,7 +190,7 @@ var commands = map[string]struct {
"route": "the route to route to",
},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
if len(args) != 1 {
return nil, ErrExpectOneArg
}
@@ -197,9 +199,10 @@ var commands = map[string]struct {
build: func(args any) CommandHandler {
route := args.(string)
return TerminatingCommand(func(w http.ResponseWriter, req *http.Request) error {
r, ok := routes.HTTP.Get(route)
ep := entrypoint.FromCtx(req.Context())
r, ok := ep.HTTPRoutes().Get(route)
if !ok {
excluded, has := routes.Excluded.Get(route)
excluded, has := ep.ExcludedRoutes().Get(route)
if has {
r, ok = excluded.(types.HTTPRoute)
}
@@ -225,7 +228,7 @@ var commands = map[string]struct {
"text": "the error message to return",
},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
if len(args) != 2 {
return nil, ErrExpectTwoArgs
}
@@ -265,7 +268,7 @@ var commands = map[string]struct {
"realm": "the authentication realm",
},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
if len(args) == 1 {
return args[0], nil
}
@@ -327,12 +330,12 @@ var commands = map[string]struct {
helpExample(CommandSet, "header", "User-Agent", "godoxy"),
),
args: map[string]string{
"target": fmt.Sprintf("the target to set, can be %s", strings.Join(AllFields, ", ")),
"target": "the target to set, can be " + strings.Join(AllFields, ", "),
"field": "the field to set",
"value": "the value to set",
},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
return validateModField(ModFieldSet, args)
},
build: func(args any) CommandHandler {
@@ -347,12 +350,12 @@ var commands = map[string]struct {
helpExample(CommandAdd, "header", "X-Foo", "bar"),
),
args: map[string]string{
"target": fmt.Sprintf("the target to add, can be %s", strings.Join(AllFields, ", ")),
"target": "the target to add, can be " + strings.Join(AllFields, ", "),
"field": "the field to add",
"value": "the value to add",
},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
return validateModField(ModFieldAdd, args)
},
build: func(args any) CommandHandler {
@@ -367,11 +370,11 @@ var commands = map[string]struct {
helpExample(CommandRemove, "header", "User-Agent"),
),
args: map[string]string{
"target": fmt.Sprintf("the target to remove, can be %s", strings.Join(AllFields, ", ")),
"target": "the target to remove, can be " + strings.Join(AllFields, ", "),
"field": "the field to remove",
},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
return validateModField(ModFieldRemove, args)
},
build: func(args any) CommandHandler {
@@ -396,7 +399,7 @@ var commands = map[string]struct {
"template": "the template to log",
},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
if len(args) != 3 {
return nil, ErrExpectThreeArgs
}
@@ -453,7 +456,7 @@ var commands = map[string]struct {
"body": "the body of the notification",
},
},
validate: func(args []string) (any, gperr.Error) {
validate: func(args []string) (any, error) {
if len(args) != 4 {
return nil, ErrExpectFourArgs
}
@@ -509,8 +512,10 @@ var commands = map[string]struct {
},
}
type onLogArgs = Tuple3[zerolog.Level, io.WriteCloser, templateString]
type onNotifyArgs = Tuple4[zerolog.Level, string, templateString, templateString]
type (
onLogArgs = Tuple3[zerolog.Level, io.WriteCloser, templateString]
onNotifyArgs = Tuple4[zerolog.Level, string, templateString, templateString]
)
// Parse implements strutils.Parser.
func (cmd *Command) Parse(v string) error {
@@ -541,7 +546,7 @@ func (cmd *Command) Parse(v string) error {
validArgs, err := builder.validate(args)
if err != nil {
// Only attach help for the directive that failed, avoid bringing in unrelated KV errors
return err.Subject(directive).With(builder.help.Error())
return gperr.PrependSubject(err, directive).With(builder.help.Error())
}
handler := builder.build(validArgs)