✨ Compress Transformer
All checks were successful
release-nightly / build-docker (push) Successful in 1m4s
All checks were successful
release-nightly / build-docker (push) Successful in 1m4s
This commit is contained in:
parent
ae165e0f12
commit
f31d35c86c
@ -92,7 +92,9 @@ func UseProxies(app *fiber.App) {
|
||||
func makeResponse(ctx *fiber.Ctx, site *sign.SiteConfig) error {
|
||||
// Modify request
|
||||
for _, transformer := range site.Transformers {
|
||||
transformer.TransformRequest(ctx)
|
||||
if err := transformer.TransformRequest(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Forward
|
||||
@ -100,7 +102,9 @@ func makeResponse(ctx *fiber.Ctx, site *sign.SiteConfig) error {
|
||||
|
||||
// Modify response
|
||||
for _, transformer := range site.Transformers {
|
||||
transformer.TransformResponse(ctx)
|
||||
if err := transformer.TransformResponse(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
|
19
pkg/sign/transformers/compress.go
Normal file
19
pkg/sign/transformers/compress.go
Normal file
@ -0,0 +1,19 @@
|
||||
package transformers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/compress"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
var CompressResponse = RequestTransformer{
|
||||
ModifyResponse: func(options any, ctx *fiber.Ctx) error {
|
||||
opts := DeserializeOptions[struct {
|
||||
Level int `json:"level"`
|
||||
}](options)
|
||||
level := lo.Ternary(opts.Level < 0 || opts.Level > 2, 0, opts.Level)
|
||||
ware := compress.New(compress.Config{Level: compress.Level(level)})
|
||||
|
||||
return ware(ctx)
|
||||
},
|
||||
}
|
@ -8,8 +8,8 @@ import (
|
||||
// Definitions
|
||||
|
||||
type RequestTransformer struct {
|
||||
ModifyRequest func(options any, ctx *fiber.Ctx)
|
||||
ModifyResponse func(options any, ctx *fiber.Ctx)
|
||||
ModifyRequest func(options any, ctx *fiber.Ctx) error
|
||||
ModifyResponse func(options any, ctx *fiber.Ctx) error
|
||||
}
|
||||
|
||||
type RequestTransformerConfig struct {
|
||||
@ -17,26 +17,28 @@ type RequestTransformerConfig struct {
|
||||
Options any `json:"options" yaml:"options"`
|
||||
}
|
||||
|
||||
func (v *RequestTransformerConfig) TransformRequest(ctx *fiber.Ctx) {
|
||||
func (v *RequestTransformerConfig) TransformRequest(ctx *fiber.Ctx) error {
|
||||
for k, f := range Transformers {
|
||||
if k == v.Type {
|
||||
if f.ModifyRequest != nil {
|
||||
f.ModifyRequest(v.Options, ctx)
|
||||
return f.ModifyRequest(v.Options, ctx)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *RequestTransformerConfig) TransformResponse(ctx *fiber.Ctx) {
|
||||
func (v *RequestTransformerConfig) TransformResponse(ctx *fiber.Ctx) error {
|
||||
for k, f := range Transformers {
|
||||
if k == v.Type {
|
||||
if f.ModifyResponse != nil {
|
||||
f.ModifyResponse(v.Options, ctx)
|
||||
return f.ModifyResponse(v.Options, ctx)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helpers
|
||||
@ -53,4 +55,5 @@ func DeserializeOptions[T any](data any) T {
|
||||
|
||||
var Transformers = map[string]RequestTransformer{
|
||||
"replacePath": ReplacePath,
|
||||
"compressResponse": CompressResponse,
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
var ReplacePath = RequestTransformer{
|
||||
ModifyRequest: func(options any, ctx *fiber.Ctx) {
|
||||
ModifyRequest: func(options any, ctx *fiber.Ctx) error {
|
||||
opts := DeserializeOptions[struct {
|
||||
Pattern string `json:"pattern"`
|
||||
Value string `json:"value"`
|
||||
@ -20,5 +20,6 @@ var ReplacePath = RequestTransformer{
|
||||
} else if ex := regexp.MustCompile(opts.Pattern); ex != nil {
|
||||
ctx.Path(ex.ReplaceAllString(path, opts.Repl))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user