mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 01:38:30 +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:
48
internal/route/rules/presets/embed.go
Normal file
48
internal/route/rules/presets/embed.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package rulepresets
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/yusing/godoxy/internal/route/rules"
|
||||
"github.com/yusing/godoxy/internal/serialization"
|
||||
)
|
||||
|
||||
//go:embed *.yml
|
||||
var fs embed.FS
|
||||
|
||||
var rulePresets = make(map[string]rules.Rules)
|
||||
|
||||
var once sync.Once
|
||||
|
||||
func GetRulePreset(name string) (rules.Rules, bool) {
|
||||
once.Do(initPresets)
|
||||
rules, ok := rulePresets[name]
|
||||
return rules, ok
|
||||
}
|
||||
|
||||
// init all rule presetsl lazily
|
||||
func initPresets() {
|
||||
files, err := fs.ReadDir(".")
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to read rule presets")
|
||||
return
|
||||
}
|
||||
for _, file := range files {
|
||||
var rules rules.Rules
|
||||
content, err := fs.ReadFile(file.Name())
|
||||
if err != nil {
|
||||
log.Error().Str("name", file.Name()).Err(err).Msg("failed to read rule preset")
|
||||
continue
|
||||
}
|
||||
_, err = serialization.ConvertString(string(content), reflect.ValueOf(&rules))
|
||||
if err != nil {
|
||||
log.Error().Str("name", file.Name()).Err(err).Msg("failed to unmarshal rule preset")
|
||||
continue
|
||||
}
|
||||
rulePresets[file.Name()] = rules
|
||||
log.Debug().Str("name", file.Name()).Msg("loaded rule preset")
|
||||
}
|
||||
}
|
||||
17
internal/route/rules/presets/webui.yml
Normal file
17
internal/route/rules/presets/webui.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
- name: login page
|
||||
on: path /login
|
||||
do: pass
|
||||
- name: protected
|
||||
on: |
|
||||
!path regex("(_next/static|_next/image|favicon.ico).*")
|
||||
!path glob("/api/v1/auth/*")
|
||||
!path /api/v1/version
|
||||
do: require_auth
|
||||
- name: proxy to backend
|
||||
on: path glob("/api/v1/*")
|
||||
do: proxy http://${API_ADDR}/
|
||||
- name: proxy to auth api
|
||||
on: path glob("/auth/*")
|
||||
do: |
|
||||
rewrite /auth /api/v1/auth
|
||||
proxy http://${API_ADDR}/
|
||||
Reference in New Issue
Block a user