diff --git a/README.md b/README.md index f1497d0..a61e8ec 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ A global proxy for go modules. see: [https://goproxy.io](https://goproxy.io) ## Requirements 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. ## Build git clone https://github.com/goproxyio/goproxy.git @@ -18,6 +19,9 @@ A global proxy for go modules. see: [https://goproxy.io](https://goproxy.io) ./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 not different from the old GOPATH, or mayebe deadlock. + See the file test/get_test.sh. + ### Router mode Use the -proxy flag switch to "Router mode", which diff --git a/go.mod b/go.mod index 251162e..6f38513 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/goproxyio/goproxy +module github.com/goproxyio/goproxy/v2 go 1.12 diff --git a/main.go b/main.go index 53d4e82..96d035d 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ import ( "syscall" "time" - "github.com/goproxyio/goproxy/proxy" + "github.com/goproxyio/goproxy/v2/proxy" "golang.org/x/mod/module" ) @@ -66,6 +66,9 @@ func init() { os.Setenv("GOPRIVATE", excludeHost) } + // Enable Go module + os.Setenv("GO111MODULE", "on") + downloadRoot = getDownloadRoot() } @@ -116,7 +119,7 @@ func getDownloadRoot() string { } if cacheDir != "" { os.Setenv("GOPATH", cacheDir) - return filepath.Join(cacheDir, "pkg/mod/cache/download") + return filepath.Join(cacheDir, "pkg", "mod", "cache", "download") } if err := goJSON(&env, "go", "env", "-json", "GOPATH"); err != nil { log.Fatal(err) @@ -125,7 +128,7 @@ func getDownloadRoot() string { if len(list) == 0 || list[0] == "" { log.Fatalf("missing $GOPATH") } - return filepath.Join(list[0], "pkg/mod/cache/download") + return filepath.Join(list[0], "pkg", "mod", "cache", "download") } // goJSON runs the go command and parses its JSON output into dst. @@ -174,7 +177,7 @@ func (*ops) List(ctx context.Context, mpath string) (proxy.File, error) { if err != nil { return nil, err } - file := filepath.Join(downloadRoot, escMod+"/@v/listproxy") + file := filepath.Join(downloadRoot, escMod, "@v", "list") if info, err := os.Stat(file); err == nil && time.Since(info.ModTime()) < listExpire { return os.Open(file) } diff --git a/proxy/router.go b/proxy/router.go index a6663df..33a745e 100644 --- a/proxy/router.go +++ b/proxy/router.go @@ -15,7 +15,7 @@ import ( "strings" "time" - "github.com/goproxyio/goproxy/renameio" + "github.com/goproxyio/goproxy/v2/renameio" ) const ListExpire = 5 * time.Minute @@ -115,11 +115,25 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { rt.srv.ServeHTTP(w, r) return } + file := filepath.Join(rt.downloadRoot, r.URL.Path) if info, err := os.Stat(file); err == nil { if f, err := os.Open(file); err == nil { var ctype string defer f.Close() + if strings.HasSuffix(r.URL.Path, "/@latest") { + if time.Since(info.ModTime()) >= ListExpire { + log.Printf("------ --- %s [proxy]\n", r.URL) + rt.proxy.ServeHTTP(w, r) + } else { + ctype = "text/plain; charset=UTF-8" + w.Header().Set("Content-Type", ctype) + log.Printf("------ --- %s [cached]\n", r.URL) + http.ServeContent(w, r, "", info.ModTime(), f) + } + return + } + i := strings.Index(r.URL.Path, "/@v/") if i < 0 { http.Error(w, "no such path", http.StatusNotFound) diff --git a/renameio/renameio.go b/renameio/renameio.go index 5d08f6b..6a02991 100644 --- a/renameio/renameio.go +++ b/renameio/renameio.go @@ -13,7 +13,7 @@ import ( "path/filepath" "strconv" - "github.com/goproxyio/goproxy/robustio" + "github.com/goproxyio/goproxy/v2/robustio" ) const patternSuffix = ".tmp" diff --git a/renameio/renameio_test.go b/renameio/renameio_test.go index 172df35..14f419d 100644 --- a/renameio/renameio_test.go +++ b/renameio/renameio_test.go @@ -20,7 +20,7 @@ import ( "testing" "time" - "github.com/goproxyio/goproxy/robustio" + "github.com/goproxyio/goproxy/v2/robustio" ) func TestConcurrentReadsAndWrites(t *testing.T) { diff --git a/test/get_test.sh b/test/get_test.sh index 10212b7..d1fb500 100755 --- a/test/get_test.sh +++ b/test/get_test.sh @@ -2,6 +2,7 @@ export GO111MODULE=on export GOPROXY='http://127.0.0.1:8081' +export GOPATH=/tmp/go datafile='test/testdata/get.txt'