diff --git a/README.md b/README.md index a8f8aa0..80118c8 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A global proxy for go modules. see: [https://goproxy.io](https://goproxy.io) ## Started - ./goproxy -listen=0.0.0.0:80 -root=/ext + ./goproxy -listen=0.0.0.0:80 -cacheDir=/cache ## Docker @@ -17,11 +17,12 @@ A global proxy for go modules. see: [https://goproxy.io](https://goproxy.io) Use the -v flag to persisting the proxy module data (change ___go_repo___ to your own dir): - docker run --name goproxy -d -p80:8081 -v go_repo:/ext goproxyio/goproxy + docker run --name goproxy -d -p80:8081 -v go_repo:/cache goproxyio/goproxy ## Docker Compose docker-compose up ## Appendix -1. set `$GOPROXY` to chain your proxy or disable the proxy + +1. set `$GOPROXY` to change your proxy or disable the proxy diff --git a/main.go b/main.go index d6e4ebe..82acc18 100644 --- a/main.go +++ b/main.go @@ -4,43 +4,45 @@ package main import ( "flag" - "github.com/goproxyio/goproxy/pkg/proxy" "log" "net/http" "os" "path/filepath" "time" + + "github.com/goproxyio/goproxy/pkg/proxy" ) var listen string -var root string +var cacheDir string func init() { log.SetOutput(os.Stdout) - flag.StringVar(&root, "root", "/go", "root cache dir to save") + flag.StringVar(&cacheDir, "cacheDir", "", "go modules cache dir") flag.StringVar(&listen, "listen", "0.0.0.0:8081", "service listen address") flag.Parse() - if err := os.MkdirAll(root, os.ModePerm); err != nil { - log.Fatalf("goproxy: make root dir failed: %s", err) - } } func main() { - - // sigs := make(chan os.Signal) - // signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - log.Printf("goproxy: %s inited. listen on %s\n", time.Now().Format("2006-01-02 15:04:05"), listen) - cacheDir := filepath.Join(root, "pkg", "mod", "cache", "download") - if _, err := os.Stat(cacheDir); os.IsNotExist(err) { - log.Printf("goproxy: cache dir %s is not exist. To create\n", cacheDir) - if err := os.MkdirAll(cacheDir, 0755); err != nil { + if cacheDir == "" { + cacheDir = "/go" + gpEnv := os.Getenv("GOPATH") + gp := filepath.SplitList(gpEnv) + if gp[0] != "" { + cacheDir = gp[0] + } + } + fullCacheDir := filepath.Join(cacheDir, "pkg", "mod", "cache", "download") + if _, err := os.Stat(fullCacheDir); os.IsNotExist(err) { + log.Printf("goproxy: cache dir %s is not exist. To create it.\n", fullCacheDir) + if err := os.MkdirAll(fullCacheDir, 0755); err != nil { log.Fatalf("make cache dir failed: %s", err) } } - http.Handle("/", proxy.NewProxy(root)) + http.Handle("/", proxy.NewProxy(cacheDir)) // TODO: TLS, graceful shutdown err := http.ListenAndServe(listen, nil) if nil != err { diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index 78a6ec3..7530b9b 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -18,10 +18,10 @@ var cacheDir string var innerHandle http.Handler func NewProxy(cache string) http.Handler { - modfetch.PkgMod = filepath.Join(cache, "pkg/mod") - codehost.WorkRoot = filepath.Join(modfetch.PkgMod, "cache/vcs") + modfetch.PkgMod = filepath.Join(cache, "pkg", "mod") + codehost.WorkRoot = filepath.Join(modfetch.PkgMod, "cache", "vcs") - cacheDir = filepath.Join(modfetch.PkgMod, "cache/download") + cacheDir = filepath.Join(modfetch.PkgMod, "cache", "download") innerHandle = http.FileServer(http.Dir(cacheDir)) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -71,10 +71,12 @@ func NewProxy(cache string) http.Handler { rev, err := repo.Stat("latest") if err != nil { errLogger.Printf("latest failed: %v", err) + return } if err := downloadMod(modPath, rev.Version); err != nil { errLogger.Printf("download get err %s", err) } + } if strings.HasSuffix(r.URL.Path, "/@v/list") {