mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 01:08:31 +02:00
feat(rules): add post-request rules system with response manipulation (#160)
* Add comprehensive post-request rules support for response phase * Enable response body, status, and header manipulation via set commands * Refactor command handlers to support both request and response phases * Implement response modifier system for post-request template execution * Support response-based rule matching with status and header checks * Add comprehensive benchmarks for matcher performance * Refactor authentication and proxying commands for unified error handling * Support negated conditions with ! * Enhance error handling, error formatting and validation * Routes: add `rule_file` field with rule preset support * Environment variable substitution: now supports variables without `GODOXY_` prefix * new conditions: * `on resp_header <key> [<value>]` * `on status <status>` * new commands: * `require_auth` * `set resp_header <key> <template>` * `set resp_body <template>` * `set status <code>` * `log <level> <path> <template>` * `notify <level> <provider> <title_template> <body_template>`
This commit is contained in:
@@ -3,9 +3,9 @@ package rules
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"unicode"
|
||||
|
||||
"github.com/yusing/goutils/env"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
@@ -33,7 +33,7 @@ func parse(v string) (subject string, args []string, err gperr.Error) {
|
||||
brackets := 0
|
||||
|
||||
var envVar bytes.Buffer
|
||||
var missingEnvVars bytes.Buffer
|
||||
var missingEnvVars []string
|
||||
inEnvVar := false
|
||||
expectingBrace := false
|
||||
|
||||
@@ -70,6 +70,10 @@ func parse(v string) (subject string, args []string, err gperr.Error) {
|
||||
escaped = false
|
||||
continue
|
||||
}
|
||||
if expectingBrace && r != '{' && r != '$' { // not escaped and not env var
|
||||
buf.WriteRune('$')
|
||||
expectingBrace = false
|
||||
}
|
||||
switch r {
|
||||
case '\\':
|
||||
escaped = true
|
||||
@@ -90,9 +94,11 @@ func parse(v string) (subject string, args []string, err gperr.Error) {
|
||||
}
|
||||
case '}':
|
||||
if inEnvVar {
|
||||
envValue, ok := os.LookupEnv(envVar.String())
|
||||
// NOTE: use env.LookupEnv instead of os.LookupEnv to support environment variable prefixes
|
||||
// like ${API_ADDR} will lookup for GODOXY_API_ADDR, GOPROXY_API_ADDR and API_ADDR.
|
||||
envValue, ok := env.LookupEnv(envVar.String())
|
||||
if !ok {
|
||||
fmt.Fprintf(&missingEnvVars, "%q, ", envVar.String())
|
||||
missingEnvVars = append(missingEnvVars, envVar.String())
|
||||
} else {
|
||||
buf.WriteString(envValue)
|
||||
}
|
||||
@@ -140,15 +146,21 @@ func parse(v string) (subject string, args []string, err gperr.Error) {
|
||||
}
|
||||
}
|
||||
|
||||
if expectingBrace {
|
||||
buf.WriteRune('$')
|
||||
}
|
||||
|
||||
if quote != 0 {
|
||||
err = ErrUnterminatedQuotes
|
||||
} else if brackets != 0 {
|
||||
err = ErrUnterminatedBrackets
|
||||
} else if inEnvVar {
|
||||
err = ErrUnterminatedEnvVar
|
||||
} else {
|
||||
flush(false)
|
||||
}
|
||||
if missingEnvVars.Len() > 0 {
|
||||
err = gperr.Join(err, ErrEnvVarNotFound.Subject(missingEnvVars.String()))
|
||||
if len(missingEnvVars) > 0 {
|
||||
err = gperr.Join(err, ErrEnvVarNotFound.With(gperr.Multiline().AddStrings(missingEnvVars...)))
|
||||
}
|
||||
return subject, args, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user