Exemplo n.º 1
0
func client(owner, repository, token string) (*gphr.GitHub, error) {
	cache := httpcache.NewMemoryCache()
	client := httpcache.NewTransport(cache).Client()

	gh := gphr.NewGitHub(owner, repository, client, token)
	return gh, nil
}
Exemplo n.º 2
0
// parseCache parses the cache-related flags and returns the specified Cache implementation.
func parseCache() (imageproxy.Cache, error) {
	if *cache == "" {
		if *cacheDir != "" {
			return diskCache(*cacheDir), nil
		}
		return nil, nil
	}

	if *cache == "memory" {
		return httpcache.NewMemoryCache(), nil
	}

	u, err := url.Parse(*cache)
	if err != nil {
		return nil, fmt.Errorf("error parsing cache flag: %v", err)
	}

	switch u.Scheme {
	case "s3":
		u.Scheme = "https"
		return s3cache.New(u.String()), nil
	case "file":
		fallthrough
	default:
		return diskCache(u.Path), nil
	}
}
Exemplo n.º 3
0
func main() {
	flag.Parse()

	if *version {
		fmt.Printf("%v\nBuild: %v\n", VERSION, BUILD_DATE)
		return
	}

	var c httpcache.Cache
	if *cacheDir != "" {
		d := diskv.New(diskv.Options{
			BasePath:     *cacheDir,
			CacheSizeMax: *cacheSize * 1024 * 1024,
		})
		c = diskcache.NewWithDiskv(d)
	} else if *cacheSize != 0 {
		c = httpcache.NewMemoryCache()
	}

	p := imageproxy.NewProxy(nil, c)
	if *whitelist != "" {
		p.Whitelist = strings.Split(*whitelist, ",")
	}
	if *referrers != "" {
		p.Referrers = strings.Split(*referrers, ",")
	}
	if *signatureKey != "" {
		key := []byte(*signatureKey)
		if strings.HasPrefix(*signatureKey, "@") {
			file := strings.TrimPrefix(*signatureKey, "@")
			var err error
			key, err = ioutil.ReadFile(file)
			if err != nil {
				log.Fatalf("error reading signature file: %v", err)
			}
		}
		p.SignatureKey = key
	}
	if *baseURL != "" {
		var err error
		p.DefaultBaseURL, err = url.Parse(*baseURL)
		if err != nil {
			log.Fatalf("error parsing baseURL: %v", err)
		}
	}

	p.ScaleUp = *scaleUp

	server := &http.Server{
		Addr:    *addr,
		Handler: p,
	}

	fmt.Printf("imageproxy (version %v) listening on %s\n", VERSION, server.Addr)
	err := server.ListenAndServe()
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
Exemplo n.º 4
0
func newTransportContext() context.Context {
	var cache httpcache.Cache = httpcache.NewMemoryCache()
	if cachePath := os.Getenv("GITHUB_CACHE_PATH"); cachePath != "" {
		cache = diskcache.New(cachePath)
	}
	tr := httpcache.NewTransport(cache)

	c := &http.Client{Transport: tr}
	return context.WithValue(context.Background(), oauth2.HTTPClient, c)
}
Exemplo n.º 5
0
// Client initializes the authorization with the GitHub API
func (g GitHub) Client() *octokat.Client {
	var cache httpcache.Cache
	if cachePath := os.Getenv("GITHUB_CACHE_PATH"); cachePath != "" {
		cache = diskcache.New(cachePath)
	} else {
		cache = httpcache.NewMemoryCache()
	}
	tr := httpcache.NewTransport(cache)

	c := &http.Client{Transport: tr}

	gh := octokat.NewClient()
	gh = gh.WithToken(g.AuthToken)
	gh = gh.WithHTTPClient(c)
	return gh
}