Go: How to start a browser AFTER the server starts listening?

In Go, how can I launch the browser AFTER the server starts listening?
Preferably, the easiest way.

My code so far, super plunged to the point:

package main import ( // Standard library packages "fmt" "net/http" "github.com/skratchdot/open-golang/open" // Third party packages "github.com/julienschmidt/httprouter" ) // go get github.com/toqueteos/webbrowser func main() { // Instantiate a new router r := httprouter.New() // Add a handler on /test r.GET("/test", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { // Simply write some test data for now fmt.Fprint(w, "Welcome!\n") }) //open.Run("https://google.com/") // open.Start("https://google.com") // http://127.0.0.1:3000/test // Fire up the server http.ListenAndServe("localhost:3000", r) fmt.Println("ListenAndServe is blocking") open.RunWith("http://localhost:3000/test", "firefox") fmt.Println("Done") } 
+4
source share
2 answers

If there is no error, http.ListenAndServe() will never return. Therefore, after this, you should not add code other than code that handles the failure.

You need to start a new goroutine, so ListenAndServe() is called in one goroutine, and code checking, if it is up, should be run on another goroutine.

And you can check if your server is working by making a simple HTTP GET call for it, for example, using http.Get() .

The following example delays the launch for 7 seconds. The new goroutine launches an infinite for loop, which checks if the server is working, sleep 1 second between attempts.

Example:

 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hi!")) }) go func() { for { time.Sleep(time.Second) log.Println("Checking if started...") resp, err := http.Get("http://localhost:8081") if err != nil { log.Println("Failed:", err) continue } resp.Body.Close() if resp.StatusCode != http.StatusOK { log.Println("Not OK:", resp.StatusCode) continue } // Reached this point: server is up and running! break } log.Println("SERVER UP AND RUNNING!") }() log.Println("Starting server...") time.Sleep(time.Second * 7) log.Fatal(http.ListenAndServe(":8081", nil)) 

Output Example:

 2015/09/23 13:53:03 Starting server... 2015/09/23 13:53:04 Checking if started... 2015/09/23 13:53:06 Failed: Get http://localhost:8081: dial tcp [::1]:8081: connectex: No connection could be made because the target machine actively refused it. 2015/09/23 13:53:07 Checking if started... 2015/09/23 13:53:09 Failed: Get http://localhost:8081: dial tcp [::1]:8081: connectex: No connection could be made because the target machine actively refused it. 2015/09/23 13:53:10 Checking if started... 2015/09/23 13:53:10 SERVER UP AND RUNNING! 
+6
source

Open a listener, launch a browser and enter the server loop:

 l, err := net.Listen("tcp", "localhost:3000") if err != nil { log.Fatal(err) } // The browser can connect now because the listening socket is open. err := open.Start("http://localhost:3000/test") if err != nil { log.Println(err) } // Start the blocking server loop. log.Fatal(http.Serve(l, r)) 

No polling needed as shown in another answer. The browser will be connected if the auditory socket is open before the browser starts.

ListenAndServe is a convenience function that opens a socket and calls Serve. The code in this answer separates these steps, so the browser can be opened after listening, but before blocking the Serve call.

+13
source

Source: https://habr.com/ru/post/1232092/


All Articles