♻️ Refactored process manager
This commit is contained in:
41
pkg/navi/transformers/compress.go
Normal file
41
pkg/navi/transformers/compress.go
Normal file
@ -0,0 +1,41 @@
|
||||
package transformers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
var CompressResponse = RequestTransformer{
|
||||
ModifyResponse: func(options any, ctx *fiber.Ctx) error {
|
||||
opts := DeserializeOptions[struct {
|
||||
Level int `json:"level" yaml:"level"`
|
||||
}](options)
|
||||
|
||||
var fctx = func(c *fasthttp.RequestCtx) {}
|
||||
var compressor fasthttp.RequestHandler
|
||||
switch opts.Level {
|
||||
// Best Speed Mode
|
||||
case 1:
|
||||
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
|
||||
fasthttp.CompressBrotliBestSpeed,
|
||||
fasthttp.CompressBestSpeed,
|
||||
)
|
||||
// Best Compression Mode
|
||||
case 2:
|
||||
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
|
||||
fasthttp.CompressBrotliBestCompression,
|
||||
fasthttp.CompressBestCompression,
|
||||
)
|
||||
// Default Mode
|
||||
default:
|
||||
compressor = fasthttp.CompressHandlerBrotliLevel(fctx,
|
||||
fasthttp.CompressBrotliDefaultCompression,
|
||||
fasthttp.CompressDefaultCompression,
|
||||
)
|
||||
}
|
||||
|
||||
compressor(ctx.Context())
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
61
pkg/navi/transformers/module.go
Normal file
61
pkg/navi/transformers/module.go
Normal file
@ -0,0 +1,61 @@
|
||||
package transformers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// Definitions
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
type RequestTransformer struct {
|
||||
ModifyRequest func(options any, ctx *fiber.Ctx) error
|
||||
ModifyResponse func(options any, ctx *fiber.Ctx) error
|
||||
}
|
||||
|
||||
type RequestTransformerConfig struct {
|
||||
Type string `json:"type" yaml:"type"`
|
||||
Options any `json:"options" yaml:"options"`
|
||||
}
|
||||
|
||||
func (v *RequestTransformerConfig) TransformRequest(ctx *fiber.Ctx) error {
|
||||
for k, f := range Transformers {
|
||||
if k == v.Type {
|
||||
if f.ModifyRequest != nil {
|
||||
return f.ModifyRequest(v.Options, ctx)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *RequestTransformerConfig) TransformResponse(ctx *fiber.Ctx) error {
|
||||
for k, f := range Transformers {
|
||||
if k == v.Type {
|
||||
if f.ModifyResponse != nil {
|
||||
return f.ModifyResponse(v.Options, ctx)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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,
|
||||
"compressResponse": CompressResponse,
|
||||
}
|
25
pkg/navi/transformers/replace_path.go
Normal file
25
pkg/navi/transformers/replace_path.go
Normal file
@ -0,0 +1,25 @@
|
||||
package transformers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ReplacePath = RequestTransformer{
|
||||
ModifyRequest: func(options any, ctx *fiber.Ctx) error {
|
||||
opts := DeserializeOptions[struct {
|
||||
Pattern string `json:"pattern" yaml:"pattern"`
|
||||
Value string `json:"value" yaml:"value"`
|
||||
Repl string `json:"repl" yaml:"repl"` // Use when complex mode(regexp) enabled
|
||||
Complex bool `json:"complex" yaml:"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))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
Reference in New Issue
Block a user