From 126991f48582dca3920c51c93443abb7708286c0 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Thu, 16 Oct 2014 16:38:11 -0500 Subject: [PATCH] Automatically open web page if operatin system supports command --- main.go | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index cda6472..40564bc 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "github.com/jessevdk/go-flags" _ "github.com/lib/pq" "os" + "os/exec" + "os/signal" ) const VERSION = "0.1.0" @@ -72,16 +74,7 @@ func initOptions() { } } -func main() { - initOptions() - initClient() - - defer dbClient.db.Close() - - if !options.Debug { - gin.SetMode("release") - } - +func startServer() { router := gin.Default() router.GET("/", API_Home) @@ -98,6 +91,37 @@ func main() { router.GET("/static/:type/:name", API_ServeAsset) fmt.Println("Starting server...") - fmt.Println("Once started you can view application at http://localhost:8080") - router.Run(":8080") + go router.Run(":8080") +} + +func handleSignals() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, os.Kill) + <-c +} + +func openPage() { + fmt.Println("To view database open http://localhost:8080 in browser") + + _, err := exec.Command("which", "open").Output() + if err != nil { + return + } + + exec.Command("open", "http://localhost:8080").Output() +} + +func main() { + initOptions() + initClient() + + defer dbClient.db.Close() + + if !options.Debug { + gin.SetMode("release") + } + + startServer() + openPage() + handleSignals() }