12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package gzip
- import (
- "strings"
- )
- var (
- DefaultExcludedExtentions = NewExcludedExtensions([]string{
- ".png", ".gif", ".jpeg", ".jpg",
- })
- DefaultOptions = &Options{
- ExcludedExtensions: DefaultExcludedExtentions,
- }
- )
- type Options struct {
- ExcludedExtensions ExcludedExtensions
- ExcludedPaths ExcludedPaths
- }
- type Option func(*Options)
- func WithExcludedExtensions(args []string) Option {
- return func(o *Options) {
- o.ExcludedExtensions = NewExcludedExtensions(args)
- }
- }
- func WithExcludedPaths(args []string) Option {
- return func(o *Options) {
- o.ExcludedPaths = NewExcludedPaths(args)
- }
- }
- // Using map for better lookup performance
- type ExcludedExtensions map[string]bool
- func NewExcludedExtensions(extensions []string) ExcludedExtensions {
- res := make(ExcludedExtensions)
- for _, e := range extensions {
- res[e] = true
- }
- return res
- }
- func (e ExcludedExtensions) Contains(target string) bool {
- _, ok := e[target]
- return ok
- }
- type ExcludedPaths []string
- func NewExcludedPaths(paths []string) ExcludedPaths {
- return ExcludedPaths(paths)
- }
- func (e ExcludedPaths) Contains(requestURI string) bool {
- for _, path := range e {
- if strings.HasPrefix(requestURI, path) {
- return true
- }
- }
- return false
- }
|