public CachingProxy(RequestDelegate next, IWebHostEnvironment hostingEnv,
                            ILoggerFactory loggerFactory, IOptions <CachingProxyConfig> config, ProxyHttpClient httpClient)
        {
            myLogger = loggerFactory.CreateLogger(GetType().FullName);
            myLogger.LogWarning("Initialising. Config:\n" + config.Value);

            myNext       = next;
            myHttpClient = httpClient;

            myMinimumFreeDiskSpaceMb = config.Value.MinimumFreeDiskSpaceMb;
            myLocalCachePath         = config.Value.LocalCachePath;
            if (myLocalCachePath == null)
            {
                throw new ArgumentNullException("", "LocalCachePath could not be null");
            }
            if (!Directory.Exists(myLocalCachePath))
            {
                throw new ArgumentException("LocalCachePath doesn't exist: " + myLocalCachePath);
            }

            myRemoteServers = new RemoteServers(config.Value.Prefixes.ToList(), config.Value.ContentTypeValidationPrefixes.ToList());
            foreach (var remoteServer in myRemoteServers.Servers)
            {
                // force reconnection (and DNS re-resolve) every two minutes
                ServicePointManager.FindServicePoint(remoteServer.RemoteUri).ConnectionLeaseTimeout = 120000;
            }

            myContentTypeProvider = new FileExtensionContentTypeProvider();
            myCacheFileProvider   = new CacheFileProvider(myLocalCachePath);

            var staticFileOptions = new StaticFileOptions
            {
                FileProvider          = myCacheFileProvider,
                ServeUnknownFileTypes = true,
                HttpsCompression      = HttpsCompressionMode.DoNotCompress,
                ContentTypeProvider   = myContentTypeProvider,
                OnPrepareResponse     = ctx =>
                {
                    SetStatusHeader(ctx.Context, CachingProxyStatus.HIT);
                    AddEternalCachingControl(ctx.Context);
                }
            };

            myStaticFileMiddleware =
                new StaticFileMiddleware(next, hostingEnv, Options.Create(staticFileOptions), loggerFactory);

            myBlacklistRegex = !string.IsNullOrWhiteSpace(config.Value.BlacklistUrlRegex)
        ? new Regex(config.Value.BlacklistUrlRegex, RegexOptions.Compiled)
        : null;

            myRedirectToRemoteUrlsRegex = !string.IsNullOrWhiteSpace(config.Value.RedirectToRemoteUrlsRegex)
        ? new Regex(config.Value.RedirectToRemoteUrlsRegex, RegexOptions.Compiled)
        : null;
        }