RoadSign/pkg/sign/transformer.go

60 lines
1.4 KiB
Go
Raw Normal View History

2023-11-17 17:42:04 +00:00
package sign
2023-11-17 16:23:40 +00:00
import (
"regexp"
"strings"
2023-12-10 03:30:31 +00:00
"github.com/gofiber/fiber/v2"
2023-11-17 16:23:40 +00:00
)
type RequestTransformer struct {
ModifyRequest func(options any, ctx *fiber.Ctx)
ModifyResponse func(options any, ctx *fiber.Ctx)
}
type RequestTransformerConfig struct {
2023-12-10 03:30:31 +00:00
Type string `json:"type" yaml:"type"`
Options any `json:"options" yaml:"options"`
2023-11-17 16:23:40 +00:00
}
func (v *RequestTransformerConfig) TransformRequest(ctx *fiber.Ctx) {
for k, f := range Transformers {
if k == v.Type {
if f.ModifyRequest != nil {
f.ModifyRequest(v.Options, ctx)
}
break
}
}
}
func (v *RequestTransformerConfig) TransformResponse(ctx *fiber.Ctx) {
for k, f := range Transformers {
if k == v.Type {
if f.ModifyResponse != nil {
f.ModifyResponse(v.Options, ctx)
}
break
}
}
}
var Transformers = map[string]RequestTransformer{
"replacePath": {
ModifyRequest: func(options any, ctx *fiber.Ctx) {
opts := DeserializeOptions[struct {
Pattern string `json:"pattern"`
Value string `json:"value"`
Repl string `json:"repl"` // Use when complex mode(regexp) enabled
Complex bool `json:"complex"`
}](options)
path := string(ctx.Request().URI().Path())
if !opts.Complex {
ctx.Path(strings.ReplaceAll(path, opts.Pattern, opts.Value))
} else if ex := regexp.MustCompile(opts.Pattern); ex != nil {
ctx.Path(ex.ReplaceAllString(path, opts.Repl))
}
},
},
}