func Serve(addr string, peerAddrs []string) error { h := newHub(addr, peerAddrs) http.HandleFunc("/", h.handleConn) go func() { time.Sleep(100 * time.Millisecond) gosh.SendVars(map[string]string{"ready": ""}) }() return http.ListenAndServe(addr, nil) }
func Serve() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, world!") }) // Note: With http.ListenAndServe() there's no easy way to tell which port // number we were assigned, so instead we use net.Listen() followed by // http.Server.Serve(). srv := &http.Server{Addr: "localhost:0"} ln, err := net.Listen("tcp", srv.Addr) if err != nil { panic(err) } go func() { time.Sleep(100 * time.Millisecond) gosh.SendVars(map[string]string{"addr": ln.Addr().String()}) }() if err = srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}); err != nil { panic(err) } }
// Functions with parameters. var ( exitFunc = gosh.RegisterFunc("exitFunc", func(code int) { os.Exit(code) }) sleepFunc = gosh.RegisterFunc("sleepFunc", func(d time.Duration, code int) { // For TestSignal and TestTerminate. ch := make(chan os.Signal, 1) signal.Notify(ch, os.Interrupt) go func() { <-ch os.Exit(0) }() // The parent waits for this "ready" notification to avoid the race where a // signal is sent before the handler is installed. gosh.SendVars(map[string]string{"ready": ""}) time.Sleep(d) os.Exit(code) }) printFunc = gosh.RegisterFunc("printFunc", func(v ...interface{}) { fmt.Print(v...) }) printfFunc = gosh.RegisterFunc("printfFunc", func(format string, v ...interface{}) { fmt.Printf(format, v...) }) ) //////////////////////////////////////////////////////////////////////////////// // Shell tests type customTB struct {