Documentation ¶
Overview ¶
Package goproxy implements a minimalist Go module proxy handler.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cacher ¶
type Cacher interface { // Get gets the matched cache for the name. It returns [fs.ErrNotExist] // if not found. // // Note that the returned [io.ReadCloser] can optionally implement the // following interfaces: // 1. [io.Seeker], mainly for the Range request header. // 2. interface{ LastModified() time.Time }, mainly for the // Last-Modified response header. Also for the If-Unmodified-Since, // If-Modified-Since, and If-Range request headers when 1 is // implemented. // 3. interface{ ModTime() time.Time }, same as 2 but with lower // priority. // 4. interface{ ETag() string }, mainly for the ETag response header. // Also for the If-Match, If-None-Match, and If-Range request // headers when 1 is implemented. Note that the return value will be // assumed to have complied with RFC 7232, section 2.3, so it will // be used directly without further processing. Get(ctx context.Context, name string) (io.ReadCloser, error) // Put puts a cache for the name with the content. Put(ctx context.Context, name string, content io.ReadSeeker) error }
Cacher defines a set of intuitive methods used to cache module files for Goproxy.
type DirCacher ¶ added in v0.4.0
type DirCacher string
DirCacher implements Cacher using a directory on the local disk. If the directory does not exist, it will be created with 0755 permissions. Cache files will be created with 0644 permissions.
type Goproxy ¶
type Goproxy struct { // GoBinName is the name of the Go binary. // // If GoBinName is empty, "go" is used. // // Note that the version of the Go binary targeted by GoBinName must be // at least version 1.11. GoBinName string // GoBinEnv is the environment of the Go binary. Each entry is in the // form "key=value". // // If GoBinEnv is nil, [os.Environ] is used. // // If GoBinEnv contains duplicate environment keys, only the last value // in the slice for each duplicate key is used. // // Note that GOPROXY, GONOPROXY, GOSUMDB, GONOSUMDB, and GOPRIVATE are // built-in supported. This means they can be set, even if the version // of the Go binary targeted by [Goproxy.GoBinName] is before version // 1.13. GoBinEnv []string // GoBinMaxWorkers is the maximum number of concurrently executing // commands for the Go binary. // // If GoBinMaxWorkers is zero, there is no limit. GoBinMaxWorkers int // PathPrefix is the prefix for all request paths. It is used to trim // the request paths using [strings.TrimPrefix]. // // If PathPrefix is not empty, it must start with "/" and typically end // with "/". PathPrefix string // Cacher is used to cache module files. // // If Cacher is nil, module files will be temporarily stored on the // local disk and discarded when the request ends. Cacher Cacher // CacherMaxCacheBytes is the maximum number of bytes allowed for // storing a new module file in [Goproxy.Cacher]. // // If CacherMaxCacheBytes is zero, there is no limit. CacherMaxCacheBytes int // ProxiedSUMDBs is a list of proxied checksum databases (see // https://go.dev/design/25530-sumdb#proxying-a-checksum-database). Each // entry is in the form "<sumdb-name>" or "<sumdb-name> <sumdb-URL>". // The first form is a shorthand for the second, where the corresponding // <sumdb-URL> will be the <sumdb-name> itself as a host with an "https" // scheme. // // If ProxiedSUMDBs contains duplicate checksum database names, only the // last value in the slice for each duplicate checksum database name is // used. ProxiedSUMDBs []string // Transport is used to perform all requests except those initiated by // calling the Go binary targeted by [Goproxy.GoBinName]. // // If Transport is nil, [http.DefaultTransport] is used. Transport http.RoundTripper // TempDir is the directory for storing temporary files. // // If TempDir is empty, [os.TempDir] is used. TempDir string // ErrorLogger is used to log errors that occur during proxying. // // If ErrorLogger is nil, [log.Default] is used. ErrorLogger *log.Logger // contains filtered or unexported fields }
Goproxy is the top-level struct of this project.
Note that Goproxy will still adhere to your environment variables. This means you can set GOPROXY to serve Goproxy itself under other proxies. By setting GONOPROXY and GOPRIVATE, you can instruct Goproxy on which modules to fetch directly, rather than using those proxies. Additionally, you can set GOSUMDB, GONOSUMDB, and GOPRIVATE to specify how Goproxy should verify the modules it has just fetched. Importantly, all of these mentioned environment variables are built-in supported, resulting in fewer external command calls and a significant performance boost.
For requests involving the download of a large number of modules (e.g., for bulk static analysis), Goproxy supports a non-standard header, "Disable-Module-Fetch: true", which instructs it to return only cached content.
Make sure that all fields of Goproxy have been finalized before calling any of its methods.
func (*Goproxy) ServeHTTP ¶
func (g *Goproxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP implements http.Handler.