package main import ( "os" "os/exec" "os/signal" "runtime" "github.com/zggsong/FileSync/config" "github.com/zggsong/FileSync/server" ) func main() { chChomrDie := make(chan struct{}) chBackendDie := make(chan struct{}) go server.Run() go startBrowser(chChomrDie, chBackendDie) chSingal := listen2Interrupt() for { select { case <-chSingal: chBackendDie <- struct{}{} case <-chChomrDie: os.Exit(0) } } } func startBrowser(chChomrDie chan struct{}, chBackendDie chan struct{}) { // 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() }() cmd.Wait() chChomrDie <- struct{}{} } //监听中断信号 func listen2Interrupt() <-chan os.Signal { chSignal := make(chan os.Signal, 1) 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 "" }