func main() { flag.Parse() http.HandleFunc("/", mainHandler) http.Handle("/assets/", httpgzip.FileServer(assets, httpgzip.FileServerOptions{ServeError: httpgzip.Detailed})) http.Handle("/favicon.ico", http.NotFoundHandler()) printServingAt(*httpFlag) err := http.ListenAndServe(*httpFlag, nil) if err != nil { log.Fatalln("ListenAndServe:", err) } }
func main() { flag.Usage = usage flag.Parse() // If we can have access to a cache directory on this system, use it for // caching HTTP requests of presenters. cacheDir, err := ospath.CacheDir("github.com/shurcooL/Go-Package-Store") if err != nil { log.Println("skipping persistent on-disk caching, because unable to acquire a cache dir:", err) cacheDir = "" } // Set GitHub presenter client. { var transport http.RoundTripper // Optionally, perform GitHub API authentication with provided token. if token := os.Getenv("GO_PACKAGE_STORE_GITHUB_TOKEN"); token != "" { transport = &oauth2.Transport{ Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}), } } if cacheDir != "" { transport = &httpcache.Transport{ Transport: transport, Cache: diskcache.New(filepath.Join(cacheDir, "github-presenter")), MarkCachedResponses: true, } } github.SetClient(&http.Client{Transport: transport}) } // Set Gitiles presenter client. { var transport http.RoundTripper if cacheDir != "" { transport = &httpcache.Transport{ Transport: transport, Cache: diskcache.New(filepath.Join(cacheDir, "gitiles-presenter")), MarkCachedResponses: true, } } gitiles.SetClient(&http.Client{Transport: transport}) } switch { case !production: fmt.Println("Using no real packages (hit /mock.html endpoint for mocks).") pipeline.Done() updater = repo.MockUpdater{} default: fmt.Println("Using all Go packages in GOPATH.") go func() { // This needs to happen in the background because sending input will be blocked on processing. forEachRepository(func(r Repo) { pipeline.AddRepository(r) }) pipeline.Done() }() updater = repo.GopathUpdater{} case *stdinFlag: fmt.Println("Reading the list of newline separated Go packages from stdin.") go func() { // This needs to happen in the background because sending input will be blocked on processing. br := bufio.NewReader(os.Stdin) for line, err := br.ReadString('\n'); err == nil; line, err = br.ReadString('\n') { importPath := line[:len(line)-1] // Trim last newline. pipeline.Add(importPath) } pipeline.Done() }() updater = repo.GopathUpdater{} case *godepsFlag != "": fmt.Println("Reading the list of Go packages from Godeps.json file:", *godepsFlag) g, err := readGodeps(*godepsFlag) if err != nil { log.Fatalln("Failed to read Godeps.json file", err) } go func() { // This needs to happen in the background because sending input will be blocked on processing. for _, dependency := range g.Deps { pipeline.AddRevision(dependency.ImportPath, dependency.Rev) } pipeline.Done() }() updater = nil case *govendorFlag != "": fmt.Println("Reading the list of Go packages from vendor.json file:", *govendorFlag) v, err := readGovendor(*govendorFlag) if err != nil { log.Fatalln("Failed to read vendor.json file:", err) } go func() { // This needs to happen in the background because sending input will be blocked on processing. for _, dependency := range v.Package { pipeline.AddRevision(dependency.Path, dependency.Revision) } pipeline.Done() }() if gu, err := repo.NewGovendorUpdater(""); err == nil { updater = gu } else { log.Println("govendor updater is not available:", err) } } err = loadTemplates() if err != nil { log.Fatalln("loadTemplates:", err) } http.HandleFunc("/index.html", mainHandler) http.Handle("/favicon.ico", http.NotFoundHandler()) fileServer := httpgzip.FileServer(assets, httpgzip.FileServerOptions{ServeError: httpgzip.Detailed}) http.Handle("/assets/", fileServer) http.Handle("/assets/octicons/", http.StripPrefix("/assets", fileServer)) http.Handle("/opened", websocket.Handler(openedHandler)) // Exit server when client tab is closed. if updater != nil { http.HandleFunc("/-/update", updateHandler) go updateWorker() } // Start listening first. listener, err := net.Listen("tcp", *httpFlag) if err != nil { log.Fatalf("failed to listen on %q: %v\n", *httpFlag, err) } if production { // Open a browser tab and navigate to the main page. go open.Open("http://" + *httpFlag + "/index.html") } fmt.Println("Go Package Store server is running at http://" + *httpFlag + "/index.html.") err = http.Serve(listener, nil) if err != nil { log.Fatalln(err) } }