mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-09 21:07:46 +00:00
This is for `@vercel/go` to work on Windows, which currently fails with this error: ``` panic: write pipe: The handle is invalid. goroutine 1 [running]: main.main() C:/Users/Nathan/Code/casca/vercel-go-test/.vercel/cache/go/2flefmhra8o/api/vercel-dev-server-main.go:25 +0x1ec exit status 2 ``` So this fallback writes the port number to a temp file that `startDevServer()` polls for in order to figure out the port number.
37 lines
690 B
Go
37 lines
690 B
Go
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
func main() {
|
|
// create a new handler
|
|
handler := http.HandlerFunc(__HANDLER_FUNC_NAME)
|
|
|
|
// https://stackoverflow.com/a/43425461/376773
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
port := listener.Addr().(*net.TCPAddr).Port
|
|
portBytes := []byte(strconv.Itoa(port))
|
|
|
|
file := os.NewFile(3, "pipe")
|
|
_, err2 := file.Write(portBytes)
|
|
if err2 != nil {
|
|
portFile := os.Getenv("VERCEL_DEV_PORT_FILE")
|
|
os.Unsetenv("VERCEL_DEV_PORT_FILE")
|
|
err3 := ioutil.WriteFile(portFile, portBytes, 0644)
|
|
if err3 != nil {
|
|
panic(err3)
|
|
}
|
|
}
|
|
|
|
panic(http.Serve(listener, handler))
|
|
}
|