-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
86 lines (76 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"github.com/yangxikun/guruweb/output"
)
var (
httpAddr = flag.String("http", ":8080", "HTTP listen address")
verbose = flag.Bool("v", false, "verbose mode")
open = flag.Bool("open", true, "try to open browser")
scope = flag.String("scope", "...", "comma-separated list of `packages` the analysis should be limited to.\n\t" +
"see buildutil.ExpandPatterns for more help")
)
func main() {
flag.Parse()
output.Normal("guruweb is starting...")
initConfig(strings.Split(*scope, ","), *verbose)
err := initIndex()
exitOn(err)
registerHandlers()
srv := &http.Server{Addr: *httpAddr}
l, err := net.Listen("tcp", srv.Addr)
exitOn(err)
url := fmt.Sprintf("http://localhost%s/", *httpAddr)
if *open {
err = startBrowser(url)
if err != nil {
output.Warn(err.Error())
output.Warn("open browser fail")
}
}
output.Normal("GOPATH: %s", os.Getenv("GOPATH"))
output.Normal("GuruWeb is running at %s", url)
exitError(srv.Serve(l))
}
// startBrowser tries to open the URL in a browser
// and reports whether it succeeds.
func startBrowser(url string) error {
// try to start the browser
var args []string
switch runtime.GOOS {
case "darwin":
args = []string{"open"}
case "windows":
args = []string{"cmd", "/c", "start"}
default:
args = []string{"xdg-open"}
}
cmd := exec.Command(args[0], append(args[1:], url)...)
return cmd.Start()
}
func registerHandlers() {
http.HandleFunc("/", serveIndex)
http.HandleFunc("/recommend-search", serveRecommendSearch)
http.HandleFunc("/recommend-pkgs", serveRecommendPkgs)
http.HandleFunc("/file", serveFile)
http.HandleFunc("/query", serveQuery)
http.HandleFunc("/config", serveConfig)
staticPrefix := "/static/"
http.Handle(staticPrefix, http.StripPrefix(staticPrefix, http.HandlerFunc(ServeStatic)))
}
func exitOn(err error) {
if err != nil {
exitError(err)
}
}
func exitError(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}