Compress Transformer
All checks were successful
release-nightly / build-docker (push) Successful in 1m4s

This commit is contained in:
LittleSheep 2023-12-16 11:42:14 +08:00
parent ae165e0f12
commit f31d35c86c
4 changed files with 37 additions and 10 deletions

View File

@ -92,7 +92,9 @@ func UseProxies(app *fiber.App) {
func makeResponse(ctx *fiber.Ctx, site *sign.SiteConfig) error { func makeResponse(ctx *fiber.Ctx, site *sign.SiteConfig) error {
// Modify request // Modify request
for _, transformer := range site.Transformers { for _, transformer := range site.Transformers {
transformer.TransformRequest(ctx) if err := transformer.TransformRequest(ctx); err != nil {
return err
}
} }
// Forward // Forward
@ -100,7 +102,9 @@ func makeResponse(ctx *fiber.Ctx, site *sign.SiteConfig) error {
// Modify response // Modify response
for _, transformer := range site.Transformers { for _, transformer := range site.Transformers {
transformer.TransformResponse(ctx) if err := transformer.TransformResponse(ctx); err != nil {
return err
}
} }
return err return err

View 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)
},
}

View File

@ -8,8 +8,8 @@ import (
// Definitions // Definitions
type RequestTransformer struct { type RequestTransformer struct {
ModifyRequest func(options any, ctx *fiber.Ctx) ModifyRequest func(options any, ctx *fiber.Ctx) error
ModifyResponse func(options any, ctx *fiber.Ctx) ModifyResponse func(options any, ctx *fiber.Ctx) error
} }
type RequestTransformerConfig struct { type RequestTransformerConfig struct {
@ -17,26 +17,28 @@ type RequestTransformerConfig struct {
Options any `json:"options" yaml:"options"` 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 { for k, f := range Transformers {
if k == v.Type { if k == v.Type {
if f.ModifyRequest != nil { if f.ModifyRequest != nil {
f.ModifyRequest(v.Options, ctx) return f.ModifyRequest(v.Options, ctx)
} }
break break
} }
} }
return nil
} }
func (v *RequestTransformerConfig) TransformResponse(ctx *fiber.Ctx) { func (v *RequestTransformerConfig) TransformResponse(ctx *fiber.Ctx) error {
for k, f := range Transformers { for k, f := range Transformers {
if k == v.Type { if k == v.Type {
if f.ModifyResponse != nil { if f.ModifyResponse != nil {
f.ModifyResponse(v.Options, ctx) return f.ModifyResponse(v.Options, ctx)
} }
break break
} }
} }
return nil
} }
// Helpers // Helpers
@ -53,4 +55,5 @@ func DeserializeOptions[T any](data any) T {
var Transformers = map[string]RequestTransformer{ var Transformers = map[string]RequestTransformer{
"replacePath": ReplacePath, "replacePath": ReplacePath,
"compressResponse": CompressResponse,
} }

View File

@ -7,7 +7,7 @@ import (
) )
var ReplacePath = RequestTransformer{ var ReplacePath = RequestTransformer{
ModifyRequest: func(options any, ctx *fiber.Ctx) { ModifyRequest: func(options any, ctx *fiber.Ctx) error {
opts := DeserializeOptions[struct { opts := DeserializeOptions[struct {
Pattern string `json:"pattern"` Pattern string `json:"pattern"`
Value string `json:"value"` Value string `json:"value"`
@ -20,5 +20,6 @@ var ReplacePath = RequestTransformer{
} else if ex := regexp.MustCompile(opts.Pattern); ex != nil { } else if ex := regexp.MustCompile(opts.Pattern); ex != nil {
ctx.Path(ex.ReplaceAllString(path, opts.Repl)) ctx.Path(ex.ReplaceAllString(path, opts.Repl))
} }
return nil
}, },
} }