pull/128/head
kun 2019-11-28 17:37:35 +08:00
commit 3b5c46d563
7 changed files with 30 additions and 8 deletions

View File

@ -5,6 +5,7 @@ A global proxy for go modules. see: [https://goproxy.io](https://goproxy.io)
## Requirements ## Requirements
It invokes the local go command to answer requests. 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 ## Build
git clone https://github.com/goproxyio/goproxy.git 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 ./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 ### Router mode
Use the -proxy flag switch to "Router mode", which Use the -proxy flag switch to "Router mode", which

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/goproxyio/goproxy module github.com/goproxyio/goproxy/v2
go 1.12 go 1.12

11
main.go
View File

@ -33,7 +33,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/goproxyio/goproxy/proxy" "github.com/goproxyio/goproxy/v2/proxy"
"golang.org/x/mod/module" "golang.org/x/mod/module"
) )
@ -66,6 +66,9 @@ func init() {
os.Setenv("GOPRIVATE", excludeHost) os.Setenv("GOPRIVATE", excludeHost)
} }
// Enable Go module
os.Setenv("GO111MODULE", "on")
downloadRoot = getDownloadRoot() downloadRoot = getDownloadRoot()
} }
@ -116,7 +119,7 @@ func getDownloadRoot() string {
} }
if cacheDir != "" { if cacheDir != "" {
os.Setenv("GOPATH", 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 { if err := goJSON(&env, "go", "env", "-json", "GOPATH"); err != nil {
log.Fatal(err) log.Fatal(err)
@ -125,7 +128,7 @@ func getDownloadRoot() string {
if len(list) == 0 || list[0] == "" { if len(list) == 0 || list[0] == "" {
log.Fatalf("missing $GOPATH") 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. // 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 { if err != nil {
return nil, err 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 { if info, err := os.Stat(file); err == nil && time.Since(info.ModTime()) < listExpire {
return os.Open(file) return os.Open(file)
} }

View File

@ -15,7 +15,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/goproxyio/goproxy/renameio" "github.com/goproxyio/goproxy/v2/renameio"
) )
const ListExpire = 5 * time.Minute const ListExpire = 5 * time.Minute
@ -115,11 +115,25 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rt.srv.ServeHTTP(w, r) rt.srv.ServeHTTP(w, r)
return return
} }
file := filepath.Join(rt.downloadRoot, r.URL.Path) file := filepath.Join(rt.downloadRoot, r.URL.Path)
if info, err := os.Stat(file); err == nil { if info, err := os.Stat(file); err == nil {
if f, err := os.Open(file); err == nil { if f, err := os.Open(file); err == nil {
var ctype string var ctype string
defer f.Close() 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/") i := strings.Index(r.URL.Path, "/@v/")
if i < 0 { if i < 0 {
http.Error(w, "no such path", http.StatusNotFound) http.Error(w, "no such path", http.StatusNotFound)

View File

@ -13,7 +13,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"github.com/goproxyio/goproxy/robustio" "github.com/goproxyio/goproxy/v2/robustio"
) )
const patternSuffix = ".tmp" const patternSuffix = ".tmp"

View File

@ -20,7 +20,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/goproxyio/goproxy/robustio" "github.com/goproxyio/goproxy/v2/robustio"
) )
func TestConcurrentReadsAndWrites(t *testing.T) { func TestConcurrentReadsAndWrites(t *testing.T) {

View File

@ -2,6 +2,7 @@
export GO111MODULE=on export GO111MODULE=on
export GOPROXY='http://127.0.0.1:8081' export GOPROXY='http://127.0.0.1:8081'
export GOPATH=/tmp/go
datafile='test/testdata/get.txt' datafile='test/testdata/get.txt'