3b25e0317f
Add the first implementation of core/rewriter.Rewriter as a plugin-only component following the bypass/admission pattern (single-value, no RewriterObject). Includes: - plugin/rewriter/proto/: protobuf definition with Rewrite RPC returning transformed data in the reply - x/rewriter/plugin/: gRPC and HTTP plugin clients - x/registry/rewriter.go: hot-reload-safe RewriterRegistry with wrapper - x/config/config.go: RewriterConfig, Config.Rewriters, ServiceConfig.Rewriter - x/config/parsing/rewriter/parse.go: config parser (plugin backends only) - core/handler/option.go: Rewriter field + RewriterOption on handler.Options - x/config/loader/loader.go: rewriter registration during startup - x/config/parsing/service/parse.go: inject rewriter into handler options
44 lines
1020 B
Go
44 lines
1020 B
Go
package registry
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-gost/core/rewriter"
|
|
)
|
|
|
|
// rewriterRegistry implements a hot-reload-safe registry for rewriter.Rewriter.
|
|
type rewriterRegistry struct {
|
|
registry[rewriter.Rewriter]
|
|
}
|
|
|
|
// Register stores a Rewriter under the given name.
|
|
func (r *rewriterRegistry) Register(name string, v rewriter.Rewriter) error {
|
|
return r.registry.Register(name, v)
|
|
}
|
|
|
|
// Get returns a wrapper that delegates to the currently registered Rewriter.
|
|
// Returns nil if name is empty.
|
|
func (r *rewriterRegistry) Get(name string) rewriter.Rewriter {
|
|
if name != "" {
|
|
return &rewriterWrapper{name: name, r: r}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *rewriterRegistry) get(name string) rewriter.Rewriter {
|
|
return r.registry.Get(name)
|
|
}
|
|
|
|
type rewriterWrapper struct {
|
|
name string
|
|
r *rewriterRegistry
|
|
}
|
|
|
|
func (w *rewriterWrapper) Rewrite(ctx context.Context, b []byte, opts ...rewriter.RewriteOption) ([]byte, error) {
|
|
v := w.r.get(w.name)
|
|
if v == nil {
|
|
return b, nil
|
|
}
|
|
return v.Rewrite(ctx, b, opts...)
|
|
}
|