Пример #1
0
func main() {
	proxy := &httputil.ReverseProxy{
		Director: func(r *http.Request) {
		},
	}

	var cache *httpcache.Cache

	if useDisk || dir != "" {
		log.Printf("storing cached resources in %s", dir)
		if err := os.MkdirAll(dir, 0700); err != nil {
			log.Fatal(err)
		}
		var err error
		cache, err = httpcache.NewDiskCache(dir)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		cache = httpcache.NewMemoryCache()
	}

	handler := httpcache.NewHandler(cache, proxy)
	handler.Shared = !private

	respLogger := httplog.NewResponseLogger(handler)
	respLogger.DumpRequests = dumpHttp
	respLogger.DumpResponses = dumpHttp
	respLogger.DumpErrors = dumpHttp

	log.Printf("listening on http://%s", listen)
	log.Fatal(http.ListenAndServe(listen, respLogger))
}
Пример #2
0
func cacheHandler(cacheOpt string, h http.Handler) http.Handler {
	if cacheOpt == "" {
		return h
	}
	var cache *httpcache.Cache
	if cacheOpt == "mem" {
		cache = httpcache.NewMemoryCache()
		log.Printf("Using in-memory HTTP cache.")
	} else if strings.HasPrefix(cacheOpt, "disk:") {
		dir := cacheOpt[len("disk:"):]
		log.Printf("Using on-disk HTTP cache at %q.", dir)
		var err error
		cache, err = httpcache.NewDiskCache(dir)
		if err != nil {
			log.Fatalf("Error creating HTTP disk cache at dir %q: %s.", dir, err)
		}
	} else {
		log.Fatalf("Invalid -cache option: %q.", cacheOpt)
	}
	ch := httpcache.NewHandler(cache, h)
	httpcache.DebugLogging, _ = strconv.ParseBool(os.Getenv("LOG_CACHE"))
	return ch
}
Пример #3
0
func main() {
	log.Printf("running apt-proxy %s", version)

	if debug {
		httpcache.DebugLogging = true
	}

	cache, err := httpcache.NewDiskCache(dir)
	if err != nil {
		log.Fatal(err)
	}

	ap := proxy.NewAptProxyFromDefaults()
	ap.Handler = httpcache.NewHandler(cache, ap.Handler)

	logger := httplog.NewResponseLogger(ap.Handler)
	logger.DumpRequests = debug
	logger.DumpResponses = debug
	logger.DumpErrors = debug
	ap.Handler = logger

	log.Printf("proxy listening on %s", listen)
	log.Fatal(http.ListenAndServe(listen, ap))
}