func main() { mux1 := mux.NewRouter() mux1.HandleFunc("/hello", handler). Methods("GET") srv := endless.NewServer("localhost:4244", mux1) srv.SignalHooks[endless.PRE_SIGNAL][syscall.SIGUSR1] = append( srv.SignalHooks[endless.PRE_SIGNAL][syscall.SIGUSR1], preSigUsr1) srv.SignalHooks[endless.POST_SIGNAL][syscall.SIGUSR1] = append( srv.SignalHooks[endless.POST_SIGNAL][syscall.SIGUSR1], postSigUsr1) err := srv.ListenAndServe() if err != nil { log.Println(err) } log.Println("Server on 4244 stopped") os.Exit(0) }
// Start will start the service. You must call Configure first. func (server *GenericServer) Start() (chan error, error) { sys := server.Sys done := make(chan error) ready := make(chan bool) endless.DefaultHammerTime = ginHammerTime ginServer := endless.NewServer(server.Sys.BindTo, server.router) ginServer.BeforeBegin = func(_ string) { server.pid = syscall.Getpid() //log.Printf("Actual pid is %d", server.pid) sys.BindTo = ginServer.EndlessListener.Addr().(*net.TCPAddr).String() ready <- true } go func() { err := ginServer.ListenAndServe() done <- err }() <-ready err := sys.WaitForServiceByAddress(sys.Name, sys.BindTo) if err != nil { return nil, err } log.Printf("Server %s started on %s (%s)", sys.Name, sys.Address, sys.BindTo) sys.AddService(sys.Name, sys.BindTo) return done, nil }
func listenAndServer(log log15.Logger, addr string, handler http.Handler) error { conf, err := config.GetConfig() if err != nil { return err } conf.Endless.DefaultHammerTime = strings.TrimSpace(conf.Endless.DefaultHammerTime) if conf.Endless.DefaultHammerTime != "" { duration, err := time.ParseDuration(conf.Endless.DefaultHammerTime) if err == nil { endless.DefaultHammerTime = duration } else { log.Error("Bad format", log15.Ctx{"module": "Endless", "DefaultHammerTime": conf.Endless.DefaultHammerTime, "error": err}) } } var terminated int32 srv := endless.NewServer(addr, handler) preHookFunc := func() { atomic.StoreInt32(&terminated, 1) } srv.RegisterSignalHook(endless.PRE_SIGNAL, syscall.SIGHUP, preHookFunc) srv.RegisterSignalHook(endless.PRE_SIGNAL, syscall.SIGINT, preHookFunc) srv.RegisterSignalHook(endless.PRE_SIGNAL, syscall.SIGTERM, preHookFunc) log.Info("Launching server") err = srv.ListenAndServe() if atomic.LoadInt32(&terminated) == 0 { if err != nil { log.Error("During startup, error has occurred", "error", err) } return err } else { log.Info("Server is going to shutdown") return nil } }