Created
May 3, 2023 08:29
-
-
Save Peltoche/bec6b0c9f01ac9f74d4a3541a2e4baaf to your computer and use it in GitHub Desktop.
A test to see if we can listen on IPv4 and IPv6 on the same port
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "net/http" | |
| "time" | |
| "github.com/labstack/echo/v4" | |
| ) | |
| func ping(c echo.Context) error { | |
| return c.JSON(http.StatusOK, map[string]string{"message": "pong"}) | |
| } | |
| func startServer(network, addr string) { | |
| srv := echo.New() | |
| srv.HideBanner = true | |
| srv.ListenerNetwork = network | |
| srv.GET("/ping", ping) | |
| err := srv.StartServer(&http.Server{ | |
| Addr: addr, | |
| ReadHeaderTimeout: 15 * time.Second, | |
| }) | |
| if err != nil { | |
| panic(err) | |
| } | |
| } | |
| func queryServer(url string) { | |
| _, err := http.Get(url) | |
| if err == nil { | |
| fmt.Printf("%q is a success\n", url) | |
| return | |
| } | |
| fmt.Printf("%q fails\n", url) | |
| } | |
| func main() { | |
| go startServer("tcp6", "[::1]:8080") | |
| go startServer("tcp4", "127.0.0.1:8080") | |
| time.Sleep(100 * time.Millisecond) | |
| queryServer("http://127.0.0.1:8080/ping") | |
| queryServer("http://localhost:8080/ping") | |
| queryServer("http://[::1]:8080/ping") | |
| // "http://127.0.0.1:8080/ping" is a success | |
| // "http://localhost:8080/ping" is a success | |
| // "http://[::1]:8080/ping" is a success | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment