Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I start the browser AFTER the server started listening?

Tags:

http

server

go

In Go, how can I start the browser AFTER the server started listening?

Preferably the simplest way possible.

My code so far, super dumbed down 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")
}
like image 647
Stefan Steiger Avatar asked Sep 23 '15 11:09

Stefan Steiger


2 Answers

Open the listener, start the browser and then 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)) 

There's no need to poll as shown in another answer. The browser will connect if the listening socket is open before the browser is started.

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

like image 163
Bayta Darell Avatar answered Nov 10 '22 06:11

Bayta Darell


If there is no error, http.ListenAndServe() will never return. So you shouldn't add code after that except code that handles failure.

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

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

The following example delays startup for 7 seconds on purpose. The new goroutine starts an endless for loop that checks if server is up, sleeping 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))

Example output:

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!
like image 32
icza Avatar answered Nov 10 '22 04:11

icza