func NewFilter(config *Config) (filters.Filter, error) { f1, err := filters.GetFilter(config.Transport) if err != nil { return nil, err } f2, ok := f1.(filters.RoundTripFilter) if !ok { return nil, fmt.Errorf("%#v was not a filters.RoundTripFilter", f1) } fetchServers := make([]*FetchServer, 0) for _, appid := range config.AppIds { u, err := url.Parse(fmt.Sprintf("%s://%s.%s%s", config.Scheme, appid, config.Domain, config.Path)) if err != nil { return nil, err } fs := &FetchServer{ URL: u, Password: config.Password, SSLVerify: config.SSLVerify, } fetchServers = append(fetchServers, fs) } return &Filter{ FetchServers: fetchServers, Transport: f2, Sites: httpproxy.NewHostMatcher(config.Sites), }, nil }
func getFilters(config *Config) ([]filters.RequestFilter, []filters.RoundTripFilter, []filters.ResponseFilter) { fs := make(map[string]filters.Filter) for _, names := range [][]string{config.Filters.Request, config.Filters.RoundTrip, config.Filters.Response} { for _, name := range names { if _, ok := fs[name]; !ok { f, err := filters.GetFilter(name) if err != nil { glog.Fatalf("filters.GetFilter(%#v) failed: %#v", name, err) } fs[name] = f } } } requestFilters := make([]filters.RequestFilter, 0) for _, name := range config.Filters.Request { f := fs[name] f1, ok := f.(filters.RequestFilter) if !ok { glog.Fatalf("%#v is not a RequestFilter", f) } requestFilters = append(requestFilters, f1) } roundtripFilters := make([]filters.RoundTripFilter, 0) for _, name := range config.Filters.RoundTrip { f := fs[name] f1, ok := f.(filters.RoundTripFilter) if !ok { glog.Fatalf("%#v is not a RoundTripFilter", f) } roundtripFilters = append(roundtripFilters, f1) } responseFilters := make([]filters.ResponseFilter, 0) for _, name := range config.Filters.Response { f := fs[name] f1, ok := f.(filters.ResponseFilter) if !ok { glog.Fatalf("%#v is not a ResponseFilter", f) } responseFilters = append(responseFilters, f1) } return requestFilters, roundtripFilters, responseFilters }