diff --git a/config/application.yaml b/config/application.yaml index 9c8b56e..87ea8d6 100644 --- a/config/application.yaml +++ b/config/application.yaml @@ -2,12 +2,15 @@ server: port: 8090 routes: - id: wry - uri: https://chat18.aichatos.xyz/ + uri: http://127.0.0.1:8080 order: 1 predicates: - Method=GET,POST - After=2023-06-26T14:30:00.000+08:00[Asia/Shanghai] filters: + - name: StripPrefix + args: + path: /name - name: AddRequestHeader args: X-Token: sss diff --git a/internal/filter/gateway/strip_prefix.go b/internal/filter/gateway/strip_prefix.go new file mode 100644 index 0000000..0a94139 --- /dev/null +++ b/internal/filter/gateway/strip_prefix.go @@ -0,0 +1,40 @@ +package gateway + +import ( + "gateway/internal/config" + "gateway/internal/filter" + "gateway/internal/web" + "gateway/logger" + "net/url" + "strings" +) + +type StripPrefixFactory struct { +} + +func (a *StripPrefixFactory) Apply(configuration *config.FilterConfiguration) filter.Filter { + path := configuration.Args["path"] + if path == nil { + logger.Logger.TagLogger("stripPrefix").Fatalf("config path is null ") + } + prefix, ok := path.(string) + if !ok { + logger.Logger.TagLogger("stripPrefix").Fatalf("config path is not string type") + } + return filter.Constructor(func(exchange *web.ServerWebExchange, chain filter.Chain) { + + r := exchange.Request + p := strings.TrimPrefix(r.URL.Path, prefix) + rp := strings.TrimPrefix(r.URL.RawPath, prefix) + if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) { + r.URL = new(url.URL) + r.URL.Path = p + r.URL.RawPath = rp + } + chain.Filter(exchange) + }) +} + +func init() { + AddFactory("StripPrefix", &StripPrefixFactory{}) +}