Back to

Package goproxy

Latest Go to latest

The latest major version is .

Published: Jul 14, 2020 | License: Unlicense | Module:


Package goproxy implements a minimalist Go module proxy handler.



var ErrCacheNotFound = errors.New("cache not found")

ErrCacheNotFound is the error resulting if a path search failed to find a cache.

type Cache

type Cache interface {

	// Name returns the unique Unix path style name.
	Name() string

	// MIMEType returns the MIME type.
	MIMEType() string

	// Size returns the length in bytes.
	Size() int64

	// ModTime returns the modification time.
	ModTime() time.Time

	// Checksum returns the checksum.
	Checksum() []byte

Cache is the cache unit of the `Cacher`.

type Cacher

type Cacher interface {
	// NewHash returns a new instance of the `hash.Hash` used to compute the
	// checksums of the caches.
	NewHash() hash.Hash

	// Cache returns the matched `Cache` for the name from the underlying
	// cacher. It returns the `ErrCacheNotFound` if not found.
	// It is the caller's responsibility to close the returned `Cache`.
	Cache(ctx context.Context, name string) (Cache, error)

	// SetCache sets the c to the underlying cacher.
	// It is the caller's responsibility to close the c.
	SetCache(ctx context.Context, c Cache) error

Cacher is the interface that defines a set of methods used to cache module files for the `Goproxy`.

If you are looking for some useful implementations of the `Cacher`, simply visit the "" package.

type Goproxy

type Goproxy struct {
	// GoBinName is the name of the Go binary.
	// The version of the Go binary target by the `GoBinName` must be at
	// least v1.11.
	// Default value: "go"
	GoBinName string `mapstructure:"go_bin_name"`

	// GoBinEnv is the environment of the Go binary. Each entry is of the
	// form "key=value".
	// Note that GOPROXY (with comma-separated list support), GONOPROXY,
	// GOSUMDB, GONOSUMDB and GOPRIVATE are built-in supported. It means
	// that they can be set even the version of the Go binary target by the
	// `GoBinName` is before v1.13.
	// If the `GoBinEnv` contains duplicate environment keys, only the last
	// value in the slice for each duplicate key is used.
	// Default value: `os.Environ()`
	GoBinEnv []string `mapstructure:"go_bin_env"`

	// GoBinMaxWorkers is the maximum number of commands allowed for the Go
	// binary to execute at the same time.
	// If the `GoBinMaxWorkers` is zero, there is no limitation.
	// Default value: 0
	GoBinMaxWorkers int `mapstructure:"go_bin_max_workers"`

	// GoBinFetchTimeout is the maximum duration allowed for the Go binary
	// to fetch a module.
	// Default value: `time.Minute`
	GoBinFetchTimeout time.Duration `mapstructure:"go_bin_fetch_timeout"`

	// PathPrefix is the prefix of all request paths. It will be used to
	// trim the request paths via the `strings.TrimPrefix`.
	// If the `PathPrefix` is not empty, it should start with "/".
	// Default value: ""
	PathPrefix string `mapstructure:"path_prefix"`

	// Cacher is the `Cacher` that used to cache module files.
	// If the `Cacher` is nil, the module files will be temporarily stored
	// in the local disk and discarded as the request ends.
	// Default value: nil
	Cacher Cacher `mapstructure:"cacher"`

	// CacherMaxCacheBytes is the maximum number of bytes allowed for the
	// `Cacher` to store a cache.
	// If the `CacherMaxCacheBytes` is zero, there is no limitation.
	// Default value: 0
	CacherMaxCacheBytes int `mapstructure:"cacher_max_cache_bytes"`

	// ProxiedSUMDBs is the proxied checksum databases. Each value should be
	// given the format of "<sumdb-name>" or "<sumdb-name> <sumdb-URL>". The
	// first format can be seen as a shorthand for the second format. In the
	// case of the first format, the corresponding checksum database URL
	// will be the checksum database name itself as a host with an "https"
	// scheme.
	// Default value: nil
	ProxiedSUMDBs []string `mapstructure:"proxied_sumdbs"`

	// InsecureMode indicates whether the insecure mode is enabled.
	// If the `InsecureMode` is true, TLS accepts any certificate presented
	// by the server and any host name in that certificate.
	InsecureMode bool `mapstructure:"insecure_mode"`

	// ErrorLogger is the `log.Logger` that logs errors that occur while
	// proxying.
	// If the `ErrorLogger` is nil, logging is done via the "log" package's
	// standard logger.
	// Default value: nil
	ErrorLogger *log.Logger `mapstructure:"-"`
	// contains filtered or unexported fields

Goproxy is the top-level struct of this project.

Note that the `Goproxy` will not mess with your environment variables, it will still follow your GOPROXY, GONOPROXY, GOSUMDB, GONOSUMDB and GOPRIVATE. It means that you can set GOPROXY to serve the `Goproxy` itself under other proxies, and by setting GONOPROXY and GOPRIVATE to indicate which modules the `Goproxy` should download directly instead of using those proxies. And of course, you can also set GOSUMDB, GONOSUMDB and GOPRIVATE to indicate how the `Goproxy` should verify the modules.

Since GOPROXY (with comma-separated list support), GONOPROXY, GOSUMDB, GONOSUMDB and GOPRIVATE were first introduced in Go 1.13, so we implemented a built-in support for them. Now, you can set them even the version of the Go binary target by the `Goproxy.GoBinName` is before v1.13.

It is highly recommended not to modify the value of any field of the `Goproxy` after calling the `Goproxy.ServeHTTP`, which will cause unpredictable problems.

The new instances of the `Goproxy` should only be created by calling the `New`.

func New

func New() *Goproxy

New returns a new instance of the `Goproxy` with default field values.

The `New` is the only function that creates new instances of the `Goproxy` and keeps everything working.

func (*Goproxy) ServeHTTP

func (g *Goproxy) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP implements the `http.Handler`.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier