func (p *App) Run() { running := make(chan bool, 1) addr := ":" + os.Getenv("GOPORT") lnet := os.Getenv("GONET") gosock := os.Getenv("GOSOCK") //启动程序监听 if strings.ToUpper(os.Getenv("GRACEFUL")) == "ON" { go func() { endless.DefaultReadTimeOut = 5 * time.Second endless.DefaultWriteTimeOut = 10 * time.Second err := endless.ListenAndServe(addr, p.Handlers) if err != nil { log.Println("[Eey]Listen error:", err) } log.Println(fmt.Sprintf("[Eye]Server on %s stopped", addr)) running <- true }() } else { if strings.ToUpper(lnet) == "TCP" { log.Println("[Eey]Listen as", lnet, addr) go func() { server := http.Server{ Addr: addr, Handler: p.Handlers, ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, } err := server.ListenAndServe() if err != nil { log.Println("[Eey]Listen error:", err) } log.Println(fmt.Sprintf("[Eye]Server on %s stopped", addr)) running <- true }() } else { log.Println("[Eey]Listen as", lnet, gosock) go func() { exec.Command("/bin/sh", "-c", "rm "+gosock).Run() unix, err := net.Listen("unix", gosock) exec.Command("/bin/sh", "-c", "chmod a+w "+gosock).Run() if err != nil { log.Println("[Eey]Listen error:", err) } fcgi.Serve(unix, p.Handlers) log.Println(fmt.Sprintf("[Eye]Server on %s stopped", addr)) running <- true }() } } <-running log.Println("[Eye]All servers stopped. Exiting.") os.Exit(0) }
func main() { mux1 := mux.NewRouter() mux1.HandleFunc("/hello", handler). Methods("GET") mux1.HandleFunc("/foo", handlerFoo). Methods("GET") mux2 := mux.NewRouter() mux2.HandleFunc("/bar", handlerBar). Methods("GET") log.Println("Starting servers...") w := sync.WaitGroup{} w.Add(2) go func() { time.Sleep(time.Second) err := endless.ListenAndServe("localhost:4242", mux1) if err != nil { log.Println(err) } log.Println("Server on 4242 stopped") w.Done() }() go func() { err := endless.ListenAndServe("localhost:4243", mux2) if err != nil { log.Println(err) } log.Println("Server on 4243 stopped") w.Done() }() w.Wait() log.Println("All servers stopped. Exiting.") os.Exit(0) }