A global proxy for go modules. see: https://goproxy.io
本项目用来构建即刻内部的 goproxy 代理,封装了如下细节:
- 通过 proxy 加速访问 github 私有 repo。
- 使用多个 upstream backend 代理公共 repo,默认情况下优先从 https://goproxy.cn 拉取依赖,支持重试,如果重试仍然不过,会继续使用 https://mirrors.aliyun.com/goproxy/ 代理再次重试。
- 使用 https://goproxy.cn 代理 golang 官方 sum.golang.org 的校验。
- 增加一层 cache。
用户在使用时只需要配置如下环境变量即可:
# GONOSUMDB 让 go cli 对于私有 repo 不校验 sumdb,必须在go get 前配置该变量
# 必须将 GOPRIVATE 变量置为空(若已经为空不用处理),否则 go cli 将不会走 GOPROXY 拉取 GOPRIVATE 中定义的路径
GOPRIVATE="" GONOSUMDB="github.com/iftechio" GOPROXY=http://goproxy.infra.svc.cluster.local:8081 go get -v github.com/iftechio/jike-sdk/go
It invokes the local go command to answer requests.
The default cacheDir is GOPATH, you can set it up by yourself according to the situation.
git clone https://github.com/goproxyio/goproxy.git
cd goproxy
make
./bin/goproxy -listen=0.0.0.0:80 -cacheDir=/tmp/test
If you run `go get -v pkg` in the proxy machine, should set a new GOPATH which is different from the old GOPATH, or mayebe deadlock.
See the file test/get_test.sh.
Use the -proxy flag switch to "Router mode", which implements route filter to routing private module or public module .
direct
+----------------------------------> private repo
|
match|pattern
|
+---+---+ +----------+
go get +-------> |goproxy| +-------> |goproxy.io| +---> golang.org/x/net
+-------+ +----------+
router mode proxy mode
In Router mode, use the -exclude flag set pattern , direct to the repo which match the module path, pattern are matched to the full path specified, not only to the host component.
./bin/goproxy -listen=0.0.0.0:80 -cacheDir=/tmp/test -proxy https://goproxy.io -exclude "*.corp.example.com,rsc.io/private"
docker run -d -p80:8081 goproxy/goproxy
Use the -v flag to persisting the proxy module data (change cacheDir to your own dir):
docker run -d -p80:8081 -v cacheDir:/go goproxy/goproxy
docker-compose up
- set
export GOPROXY=http://localhost
to enable your goproxy. - set
export GOPROXY=direct
to disable it.