update flags

pull/23/head
kun 2018-12-25 19:53:45 +08:00
parent ebcb9429d3
commit 4b5758264e
3 changed files with 26 additions and 21 deletions

View File

@ -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

32
main.go
View File

@ -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 {

View File

@ -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") {