config.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package compress
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. )
  5. // Config defines the config for middleware.
  6. type Config struct {
  7. // Next defines a function to skip this middleware when returned true.
  8. //
  9. // Optional. Default: nil
  10. Next func(c *fiber.Ctx) bool
  11. // Level determines the compression algorithm
  12. //
  13. // Optional. Default: LevelDefault
  14. // LevelDisabled: -1
  15. // LevelDefault: 0
  16. // LevelBestSpeed: 1
  17. // LevelBestCompression: 2
  18. Level Level
  19. }
  20. // Level is numeric representation of compression level
  21. type Level int
  22. // Represents compression level that will be used in the middleware
  23. const (
  24. LevelDisabled Level = -1
  25. LevelDefault Level = 0
  26. LevelBestSpeed Level = 1
  27. LevelBestCompression Level = 2
  28. )
  29. // ConfigDefault is the default config
  30. var ConfigDefault = Config{
  31. Next: nil,
  32. Level: LevelDefault,
  33. }
  34. // Helper function to set default values
  35. func configDefault(config ...Config) Config {
  36. // Return default config if nothing provided
  37. if len(config) < 1 {
  38. return ConfigDefault
  39. }
  40. // Override default config
  41. cfg := config[0]
  42. // Set default values
  43. if cfg.Level < LevelDisabled || cfg.Level > LevelBestCompression {
  44. cfg.Level = ConfigDefault.Level
  45. }
  46. return cfg
  47. }