diff --git a/main/main.go b/main/main.go index ed6e57e..8d2de02 100644 --- a/main/main.go +++ b/main/main.go @@ -1,18 +1,37 @@ package main import ( + "flag" "fmt" + "io" "log" "net/http" "os" + "os/exec" "os/signal" + "runtime" + "strings" "360.cn/elasticHD/main/search" _ "360.cn/elasticHD/main/statik" "github.com/rakyll/statik/fs" ) +var ( + Stderr io.Writer = os.Stderr // Stderr is the io.Writer to which executed commands write standard error. + Stdout io.Writer = os.Stdout // Stdout is the io.Writer to which executed commands write standard output. +) + +// Config ... +type Config struct { + ServerHost string +} + +var config *Config + func main() { + + parseFlags() statikFS, err := fs.New() // 静态文件编译成二进制 if err != nil { log.Fatalf(err.Error()) @@ -31,7 +50,8 @@ func main() { fmt.Println("Cant start server:", err) os.Exit(1) } - }(`0.0.0.0:9800`, mux) + }(config.ServerHost, mux) + openPage() // 捕捉ctrl+C 退出信号 handleSignals() } @@ -41,3 +61,42 @@ func handleSignals() { signal.Notify(c, os.Interrupt, os.Kill) <-c } + +func openPage() { + url := fmt.Sprintf("http://%v", config.ServerHost) + fmt.Println("To view elasticHD console open", url, "in browser") + var err error + switch runtime.GOOS { + case "linux": + err = runCmd("xdg-open", url) + case "darwin": + err = runCmd("open", url) + case "windows": + r := strings.NewReplacer("&", "^&") + err = runCmd("cmd", "/c", "start", r.Replace(url)) + default: + err = fmt.Errorf("unsupported platform") + } + if err != nil { + fmt.Println(err) + } +} + +// runCmd run command opens a new browser window pointing to url. +func runCmd(prog string, args ...string) error { + cmd := exec.Command(prog, args...) + cmd.Stdout = Stdout + cmd.Stderr = Stderr + return cmd.Run() +} + +// parseFlags parse flags of program. +func parseFlags() { + config = &Config{} + flag.StringVar(&config.ServerHost, "p", "0.0.0.0:9800", "local server address") + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) + flag.PrintDefaults() + } + flag.Parse() +}