♻️ Move transformer to an isolated module
All checks were successful
release-nightly / build-docker (push) Successful in 1m3s
All checks were successful
release-nightly / build-docker (push) Successful in 1m3s
This commit is contained in:
parent
ed1b20873d
commit
ae165e0f12
@ -1,10 +0,0 @@
|
||||
package sign
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func DeserializeOptions[T any](data any) T {
|
||||
var out T
|
||||
raw, _ := json.Marshal(data)
|
||||
_ = json.Unmarshal(raw, &out)
|
||||
return out
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package sign
|
||||
|
||||
import (
|
||||
"code.smartsheep.studio/goatworks/roadsign/pkg/sign/transformers"
|
||||
"errors"
|
||||
"math/rand"
|
||||
|
||||
@ -39,6 +40,8 @@ func (v *RoadApp) Forward(ctx *fiber.Ctx, site *SiteConfig) error {
|
||||
}
|
||||
}
|
||||
|
||||
type RequestTransformerConfig = transformers.RequestTransformerConfig
|
||||
|
||||
type SiteConfig struct {
|
||||
ID string `json:"id"`
|
||||
Rules []*RouterRuleConfig `json:"rules" yaml:"rules"`
|
||||
|
@ -1,12 +1,12 @@
|
||||
package sign
|
||||
package transformers
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"encoding/json"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// Definitions
|
||||
|
||||
type RequestTransformer struct {
|
||||
ModifyRequest func(options any, ctx *fiber.Ctx)
|
||||
ModifyResponse func(options any, ctx *fiber.Ctx)
|
||||
@ -39,21 +39,18 @@ func (v *RequestTransformerConfig) TransformResponse(ctx *fiber.Ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
},
|
||||
},
|
||||
// Helpers
|
||||
|
||||
func DeserializeOptions[T any](data any) T {
|
||||
var out T
|
||||
raw, _ := json.Marshal(data)
|
||||
_ = json.Unmarshal(raw, &out)
|
||||
return out
|
||||
}
|
||||
|
||||
// Map of Transformers
|
||||
// Every transformer need to be mapped here so that they can get work.
|
||||
|
||||
var Transformers = map[string]RequestTransformer{
|
||||
"replacePath": ReplacePath,
|
||||
}
|
24
pkg/sign/transformers/replace_path.go
Normal file
24
pkg/sign/transformers/replace_path.go
Normal file
@ -0,0 +1,24 @@
|
||||
package transformers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ReplacePath = RequestTransformer{
|
||||
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))
|
||||
}
|
||||
},
|
||||
}
|
Loading…
Reference in New Issue
Block a user