diff --git a/main.go b/main.go index b28cd2b..9d9b237 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "os" "os/exec" "os/signal" + "runtime" "github.com/zggsong/FileSync/config" "github.com/zggsong/FileSync/server" @@ -26,17 +27,16 @@ func main() { } func startBrowser(chChomrDie chan struct{}, chBackendDie chan struct{}) { - chromePath := "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" + // chromePath := "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" + chromePath := LocateChrome() cmd := exec.Command(chromePath, "--app=http://127.0.0.1:"+config.GetPort()+"/static/index.html") cmd.Start() go func() { <-chBackendDie cmd.Process.Kill() }() - go func() { - cmd.Wait() - chChomrDie <- struct{}{} - }() + cmd.Wait() + chChomrDie <- struct{}{} } //监听中断信号 @@ -45,3 +45,53 @@ func listen2Interrupt() <-chan os.Signal { signal.Notify(chSignal, os.Interrupt) return chSignal } + +func LocateChrome() string { + + // If env variable "LORCACHROME" specified and it exists + if path, ok := os.LookupEnv("LORCACHROME"); ok { + if _, err := os.Stat(path); err == nil { + return path + } + } + + var paths []string + switch runtime.GOOS { + case "darwin": + paths = []string{ + "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", + "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary", + "/Applications/Chromium.app/Contents/MacOS/Chromium", + "/usr/bin/google-chrome-stable", + "/usr/bin/google-chrome", + "/usr/bin/chromium", + "/usr/bin/chromium-browser", + } + case "windows": + paths = []string{ + os.Getenv("LocalAppData") + "/Google/Chrome/Application/chrome.exe", + os.Getenv("ProgramFiles") + "/Google/Chrome/Application/chrome.exe", + os.Getenv("ProgramFiles(x86)") + "/Google/Chrome/Application/chrome.exe", + os.Getenv("LocalAppData") + "/Chromium/Application/chrome.exe", + os.Getenv("ProgramFiles") + "/Chromium/Application/chrome.exe", + os.Getenv("ProgramFiles(x86)") + "/Chromium/Application/chrome.exe", + os.Getenv("ProgramFiles(x86)") + "/Microsoft/Edge/Application/msedge.exe", + } + default: + paths = []string{ + "/usr/bin/google-chrome-stable", + "/usr/bin/google-chrome", + "/usr/bin/chromium", + "/usr/bin/chromium-browser", + "/snap/bin/chromium", + } + } + + for _, path := range paths { + if _, err := os.Stat(path); os.IsNotExist(err) { + continue + } + return path + } + return "" +}