• Private servers installed
  • Start the
  • Packaging scripts
  • GOPROXY
    • GET $GOPROXY//@v/list Returns all known versions of the current Module, one per line
    • GET $GOPROXY//@v/. Info Returns the version metadata in JSON format
    • GET $GOPROXY//@v/. Mod Returns the go.mod file for the module version
    • GET $GOPROXY//@v/.zip Returns the zip package of the corresponding module version.
  • The realization of a native

Before, I introduced how to package the environment online, which can solve the problem. However, because the dependency package is downloaded by proxy, the packaging process is slow. According to my own feeling, it takes 2 minutes to type a project. And there is a serious problem that once the download fails, the packaging fails and you have to try again. It’s not a good experience. As for Go Module packaging, I feel that the future development direction is still the same as Java, you have to customize your own server, this packaging will be fast, but also safe. If the private server caches the current version of the package, it will return directly; Otherwise private server to download the corresponding version of the code.

Private servers installed

First you need to install Go1.11.

I’m using your Athens athenaeum.

First download the code:

git clone https://github.com/gomods/athens
cd athens
Copy the code

Even if using private server still need to set up proxy:

Export HTTP_PROXY = 10.244.255.3:7766 export HTTPS_PROXY = 10.244.255.3:7766Copy the code

Compiling and installing binaries:

go install
Copy the code

Start the

Because it is installed through Go Install, it will be installed in $GOBIN, which will be global and can be called directly.

./proxy
Copy the code

However, this is too simple. I use Supervisor to do process daemon, and the configuration file is as follows:

[program:proxy] command=/path/to/proxy -config_file=/path/to/github.com/gomods/athens/config.dev.toml Environment = HTTP_PROXY = "10.244.255.3:7766", HTTPS_PROXY = "10.244.255.3:7766 stdout_logfile ="/TMP/proxy. The log stderr_logfile=/tmp/proxy.log autostart=true autorestart=true startsecs=5 priority=1 stopasgroup=true illasgroup=trueCopy the code

Packaging scripts

There were four lines in the script, but now there are two:

Export GO111MODULE = on export GOPROXY = http://127.0.0.1:3000Copy the code

After the packaging starts, the private server’s log will see something like this:

Handler: GET/github.com/spf13/afero/@v/v1.1.1.zip [200]Copy the code

The package log looks like this:

Go: downloading github.com/spf13/pflag v1.0.2Copy the code

If this is the first download, it may time out:

Go: gopkg in/[email protected]: Unexpected status (http://10.244.255.3:7766/gopkg.in/tomb.v1/@v/v1.0.0-20141024135613-dd632973f1e7.info) : 500 Internal Server ErrorCopy the code

This is ok, it will be fine in a moment, you can put the above link in the browser to brush, can brush out the result that indicates that the download is ready.

{"Version": "v1.0.0-20141024135613-dd632973f1e7", "Time": "2014-10-24t13:56:13z"}Copy the code

GOPROXY

HTTP_PROXY = HTTP_PROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY = GOPROXY

You can use the go help goproxy command for details, or see here.

The Go Module can be downloaded through a proxy. If the environment variable GOPROXY is set, all packages will be downloaded from this proxy.

Proxy HTTP based GET method, request without parameters, so as long as it complies with fixed rules, any server can act as a proxy server. For example, a static file server.

The rule is:

GET $GOPROXY/ /@v/list Returns all known versions of the current Module, one per line

GET/github.com/mnhkahn/gogogo/@v/list v1.0.0 v1.0.1 v1.0.2 v1.0.3 1.0.4 v1.0.5Copy the code

GET $GOPROXY//@v/. Info Returns the version metadata in JSON format

GET/github.com/mnhkahn/gogogo/@v/v1.0.5.info {" Version ":" v1.0.5 ", "Time", "the 2018-09-26 T02:47:43 Z}"Copy the code

Metadata Go structure definition:

type Info struct {
    Version string    // version string
    Time    time.Time // commit time
}
Copy the code

GET $GOPROXY//@v/. Mod Returns the go.mod file for the module version

GET/github.com/mnhkahn/gogogo/@v/v1.0.5.mod module github.com/mnhkahn/gogogo the require (github.com/BurntSushi/toml V0.3.0 / / indirect github.com/ChimeraCoder/gojson v1.0.0 github.com/davecgh/go-spew v1.1.1 / / indirect Github.com/magiconair/properties v1.8.0 github.com/pmezard/go-difflib v1.0.0 / / indirect github.com/sasbury/mini V0.0.0-20161224193750-64 bd399395db github.com/stretchr/testify v1.2.2 golang.org/x/net V0.0.0-20180826012351-8 a410e7b638d gopkg. In/natefinch/lumberjack v2 v2.0.0-20170531160350 - a96e63847dc3 gopkg. In/yaml. V2 V2.2.1 / / indirect)Copy the code

GET $GOPROXY//@v/.zip Returns the zip package of the corresponding module version.

GET /github.com/mnhkahn/gogogo/@v/v1.0.5.mod
v1.0.5.zip
Copy the code

All package names will be encoded in lower case. If there are capital letters, preceded by an exclamation point.

github.com/Azure => github.com/! azureCopy the code

The realization of a native

These environment variables are read at download time:

func PrepareEnv(gopath string) []string {
	pathEnv := fmt.Sprintf("PATH=%s", os.Getenv("PATH"))
	httpProxy := fmt.Sprintf("HTTP_PROXY=%s", os.Getenv("HTTP_PROXY"))
	httpsProxy := fmt.Sprintf("HTTPS_PROXY=%s", os.Getenv("HTTPS_PROXY"))
	noProxy := fmt.Sprintf("NO_PROXY=%s", os.Getenv("NO_PROXY"))
	gopathEnv := fmt.Sprintf("GOPATH=%s", gopath)
	cacheEnv := fmt.Sprintf("GOCACHE=%s", filepath.Join(gopath, "cache"))
	disableCgo := "CGO_ENABLED=0"
	enableGoModules := "GO111MODULE=on"
	...
}
Copy the code

HTTP_PROXY and HTTPS_PROXY are the proxy Settings used by private servers to download packages. NO_PROXY can be added to the proxy whitelist:

export NO_PROXY=gopkg.in,$NO_PROXY
Copy the code

These environment variables are used as temporary environment variables for code download. The logic for downloading dependent packages:

cmd := exec.Command(goBinaryName, "mod", "download", fullURI)
cmd.Env = PrepareEnv(gopath)
cmd.Dir = repoRoot
o, err := cmd.CombinedOutput()
Copy the code

The actual execution is:

HTTP_PROXY = 10.244.255.3:7766 HTTPS_PROXY = 10.244.255.3: GOPATH = 7766 / TMP/athens167327692 GOCACHE = / TMP/athens167327692 / cache go mod download golang.org/x/[email protected]Copy the code

原文 标 题 : build private server for Go module