goproxy/main.go

71 lines
1.4 KiB
Go
Raw Normal View History

2018-12-24 07:51:40 +00:00
//go:generate bash build/generate.sh
2018-09-01 04:23:32 +00:00
package main
import (
2018-09-03 03:09:50 +00:00
"flag"
2018-12-24 07:51:40 +00:00
"log"
2018-09-01 04:23:32 +00:00
"net/http"
"os"
2018-12-26 01:36:02 +00:00
"os/signal"
2018-09-01 04:23:32 +00:00
"path/filepath"
2018-12-26 01:36:02 +00:00
"syscall"
2018-09-06 03:20:02 +00:00
"time"
2018-12-25 11:53:45 +00:00
"github.com/goproxyio/goproxy/pkg/proxy"
2018-09-01 04:23:32 +00:00
)
2018-09-03 03:09:50 +00:00
var listen string
2018-12-25 11:53:45 +00:00
var cacheDir string
2018-09-03 03:09:50 +00:00
func init() {
2018-12-24 07:51:40 +00:00
log.SetOutput(os.Stdout)
2018-12-25 11:53:45 +00:00
flag.StringVar(&cacheDir, "cacheDir", "", "go modules cache dir")
2018-09-03 03:09:50 +00:00
flag.StringVar(&listen, "listen", "0.0.0.0:8081", "service listen address")
flag.Parse()
}
2018-09-01 04:23:32 +00:00
func main() {
2018-12-26 01:36:02 +00:00
errCh := make(chan error)
2018-12-24 07:51:40 +00:00
log.Printf("goproxy: %s inited. listen on %s\n", time.Now().Format("2006-01-02 15:04:05"), listen)
2018-09-06 04:19:33 +00:00
2018-12-25 11:53:45 +00:00
if cacheDir == "" {
cacheDir = "/go"
gpEnv := os.Getenv("GOPATH")
2018-12-26 14:03:22 +00:00
if gpEnv != "" {
gp := filepath.SplitList(gpEnv)
if gp[0] != "" {
cacheDir = gp[0]
}
2018-12-25 11:53:45 +00:00
}
}
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 {
2018-12-24 07:51:40 +00:00
log.Fatalf("make cache dir failed: %s", err)
2018-09-01 04:23:32 +00:00
}
}
2018-12-25 11:53:45 +00:00
http.Handle("/", proxy.NewProxy(cacheDir))
2018-12-26 01:36:02 +00:00
go func() {
err := http.ListenAndServe(listen, nil)
if err != nil {
errCh <- err
}
}()
signCh := make(chan os.Signal)
signal.Notify(signCh, os.Interrupt, syscall.SIGTERM)
select {
case err := <-errCh:
log.Fatal(err)
case sign := <-signCh:
log.Printf("Server gracefully %s", sign)
}
2018-09-01 04:23:32 +00:00
}