options.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package gzip
  2. import (
  3. "strings"
  4. )
  5. var (
  6. DefaultExcludedExtentions = NewExcludedExtensions([]string{
  7. ".png", ".gif", ".jpeg", ".jpg",
  8. })
  9. DefaultOptions = &Options{
  10. ExcludedExtensions: DefaultExcludedExtentions,
  11. }
  12. )
  13. type Options struct {
  14. ExcludedExtensions ExcludedExtensions
  15. ExcludedPaths ExcludedPaths
  16. }
  17. type Option func(*Options)
  18. func WithExcludedExtensions(args []string) Option {
  19. return func(o *Options) {
  20. o.ExcludedExtensions = NewExcludedExtensions(args)
  21. }
  22. }
  23. func WithExcludedPaths(args []string) Option {
  24. return func(o *Options) {
  25. o.ExcludedPaths = NewExcludedPaths(args)
  26. }
  27. }
  28. // Using map for better lookup performance
  29. type ExcludedExtensions map[string]bool
  30. func NewExcludedExtensions(extensions []string) ExcludedExtensions {
  31. res := make(ExcludedExtensions)
  32. for _, e := range extensions {
  33. res[e] = true
  34. }
  35. return res
  36. }
  37. func (e ExcludedExtensions) Contains(target string) bool {
  38. _, ok := e[target]
  39. return ok
  40. }
  41. type ExcludedPaths []string
  42. func NewExcludedPaths(paths []string) ExcludedPaths {
  43. return ExcludedPaths(paths)
  44. }
  45. func (e ExcludedPaths) Contains(requestURI string) bool {
  46. for _, path := range e {
  47. if strings.HasPrefix(requestURI, path) {
  48. return true
  49. }
  50. }
  51. return false
  52. }