# Route Rules Implements a rule engine for HTTP request/response processing, enabling conditional routing, header manipulation, authentication, and more. ## Overview The `internal/route/rules` package provides a powerful rule engine for GoDoxy. Rules allow conditional processing of HTTP requests and responses based on various matchers (headers, path, method, IP, etc.). Matching rules can modify requests, route to different backends, or terminate processing. ### Primary Consumers - **Route layer**: Applies rules during request processing - **Configuration system**: Parses rule YAML - **Middleware integration**: Extends rule capabilities ### Non-goals - Does not implement proxy transport (delegates to reverse proxy) - Does not handle TLS/SSL (handled at entrypoint) - Does not perform health checking ### Stability Internal package with stable YAML schema. Backward-compatible additions to rule types are allowed. ## Public API ### Exported Types ```go type Rules []Rule type Rule struct { Name string // Rule identifier for debugging On RuleOn // Condition matcher Do Command // Action to execute } type RuleOn struct { raw string checker Checker isResponseChecker bool } type Command struct { raw string exec CommandHandler isResponseHandler bool } ``` ### Exported Functions ```go // BuildHandler converts rules to an HTTP handler func (rules Rules) BuildHandler(up http.HandlerFunc) http.HandlerFunc // ParseRules parses rule configuration func ParseRules(config string) (Rules, error) // ValidateRules validates rule syntax func ValidateRules(config string) error ``` ## Architecture ### Core Components ```mermaid classDiagram class Rules { +BuildHandler(up) http.HandlerFunc } class Rule { +Name string +On RuleOn +Do Command +IsResponseRule() bool } class RuleOn { +raw string +checker Checker +isResponseChecker bool } class Command { +raw string +exec CommandHandler +isResponseHandler bool } class Checker { <> +Check(r *http.Request) bool +CheckResponse(w ResponseWriter, r *http.Request) bool } class CommandHandler { <> +Execute(w ResponseWriter, r *http.Request, rm *ResponseModifier) gperr.Error } Rules --> Rule Rule --> RuleOn Rule --> Command RuleOn --> Checker Command --> CommandHandler ``` ### Request Processing Flow ```mermaid sequenceDiagram participant Req as Request participant Pre as Pre Rules participant Proxy as Upstream participant Post as Post Rules Req->>Pre: Check pre-rules alt Rule matches Pre->>Pre: Execute handler alt Terminating action Pre-->>Req: Response Return-->>Req: Return immediately end end Req->>Proxy: Forward request Proxy-->>Req: Response Req->>Post: Check post-rules Post->>Post: Execute handlers Post-->>Req: Modified response ``` ### Condition Matchers | Matcher | Type | Description | | ------------- | -------- | ---------------------------- | | `header` | Request | Match request header value | | `query` | Request | Match query parameter | | `cookie` | Request | Match cookie value | | `form` | Request | Match form field | | `method` | Request | Match HTTP method | | `host` | Request | Match virtual host | | `path` | Request | Match request path | | `proto` | Request | Match protocol (http/https) | | `remote` | Request | Match remote IP/CIDR | | `basic_auth` | Request | Match basic auth credentials | | `route` | Request | Match route name | | `resp_header` | Response | Match response header | | `status` | Response | Match status code range | ### Matcher Types ```sh # String: exact match (default) # Glob: shell-style wildcards (*, ?) # Regex: regular expressions path /api/users // exact match path glob("/api/*") // glob pattern path regex("/api/v[0-9]+/.*") // regex pattern ``` ### Actions **Terminating Actions** (stop processing): | Command | Description | | ------------------------ | ---------------------- | | `error ` | Return HTTP error | | `redirect ` | Redirect to URL | | `serve ` | Serve local files | | `route ` | Route to another route | | `proxy ` | Proxy to upstream | **Non-Terminating Actions** (modify and continue): | Command | Description | | ------------------------------ | ---------------------- | | `pass` / `bypass` | Pass through unchanged | | `rewrite ` | Rewrite request path | | `require_auth` | Require authentication | | `require_basic_auth ` | Basic auth challenge | | `set ` | Set header/variable | | `add ` | Add header/variable | | `remove ` | Remove header/variable | **Response Actions**: | Command | Description | | ------------------------------------------ | ----------------- | | `log