mirror of
https://github.com/eliasstepanik/caddy-ingess.git
synced 2026-01-11 04:28:28 +00:00
* feat: Add plugin system to controller * add priority system and default empty tls connection policy
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package ingress
|
|
|
|
import (
|
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
|
|
"github.com/caddyserver/ingress/pkg/converter"
|
|
)
|
|
|
|
type RewritePlugin struct{}
|
|
|
|
func (p RewritePlugin) IngressPlugin() converter.PluginInfo {
|
|
return converter.PluginInfo{
|
|
Name: "ingress.rewrite",
|
|
Priority: 10,
|
|
New: func() converter.Plugin { return new(RewritePlugin) },
|
|
}
|
|
}
|
|
|
|
// IngressHandler Converts rewrite annotations to rewrite handler
|
|
func (p RewritePlugin) IngressHandler(input converter.IngressMiddlewareInput) (*caddyhttp.Route, error) {
|
|
ing := input.Ingress
|
|
|
|
rewriteTo := getAnnotation(ing, rewriteToAnnotation)
|
|
if rewriteTo != "" {
|
|
handler := caddyconfig.JSONModuleObject(
|
|
rewrite.Rewrite{URI: rewriteTo},
|
|
"handler", "rewrite", nil,
|
|
)
|
|
|
|
input.Route.HandlersRaw = append(input.Route.HandlersRaw, handler)
|
|
}
|
|
|
|
rewriteStripPrefix := getAnnotation(ing, rewriteStripPrefixAnnotation)
|
|
if rewriteStripPrefix != "" {
|
|
handler := caddyconfig.JSONModuleObject(
|
|
rewrite.Rewrite{StripPathPrefix: rewriteStripPrefix},
|
|
"handler", "rewrite", nil,
|
|
)
|
|
|
|
input.Route.HandlersRaw = append(input.Route.HandlersRaw, handler)
|
|
}
|
|
return input.Route, nil
|
|
}
|
|
|
|
func init() {
|
|
converter.RegisterPlugin(RewritePlugin{})
|
|
}
|
|
|
|
// Interface guards
|
|
var (
|
|
_ = converter.IngressMiddleware(RewritePlugin{})
|
|
)
|