Skip to content

Commit

Permalink
feat(core): StripPrefixFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
BadKid90s committed Jan 22, 2024
1 parent 3e9e575 commit 1c55e18
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion config/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions internal/filter/gateway/strip_prefix.go
Original file line number Diff line number Diff line change
@@ -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{})
}

0 comments on commit 1c55e18

Please sign in to comment.