snow/aid/fiber.go

62 lines
1.3 KiB
Go
Raw Permalink Normal View History

package aid
import (
"slices"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/logger"
)
func FiberLogger() fiber.Handler {
return logger.New(logger.Config{
Format: "(${method}) (${status}) (${latency}) ${path}\n",
2024-01-20 01:58:57 +00:00
Next: func(c *fiber.Ctx) bool {
if (slices.Contains[[]int](
[]int{302, 101},
c.Response().StatusCode(),
)) {
return true
}
if (slices.Contains[[]string](
[]string{"/snow/log", "/purchase/assets/", " /favicon.ico"},
c.Path(),
)) {
return true
}
return false
2024-01-20 01:58:57 +00:00
},
})
}
2024-02-14 20:30:55 +00:00
func FiberLimiter(n int) fiber.Handler {
return limiter.New(limiter.Config{
2024-02-14 20:30:55 +00:00
Max: n,
Expiration: 1 * time.Minute,
})
}
func FiberCors() fiber.Handler {
return cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: "Origin, Content-Type, Accept, Authorization, X-Requested-With",
})
2024-02-19 04:07:40 +00:00
}
// https://github.com/gofiber/fiber/issues/510
func FiberGetQueries(c *fiber.Ctx, queryKeys ...string) map[string][]string {
argsMaps := make(map[string][]string)
for _, keys := range queryKeys {
param := c.Request().URI().QueryArgs().PeekMulti(keys)
for _, value := range param {
argsMaps[keys] = append(argsMaps[keys], string(value))
}
}
return argsMaps
}