diff --git a/server.go b/server.go index 122dd53..f52cbe5 100644 --- a/server.go +++ b/server.go @@ -20,10 +20,12 @@ func New(sh ServerHandler) Server { type Server interface { Start() error Stop() - Serve() error + Context() ServerContext } type server struct { + ctx ServerContext + sh ServerHandler listener net.Listener @@ -44,17 +46,17 @@ func (s *server) Start() error { panic("Server: server is already running. Stop it before starting it again") } var err error - if s.listener, err = s.sh.Listen(); nil != err { - return err - } + s.ctx = s.sh.ServerContext() - if err = s.sh.Init(); nil != err { + if err = s.sh.Init(s.ctx); nil != err { logging.Logger().Panic(fmt.Sprintf("Server: Initialization of server has been failed %v", err)) } - s.stopChan = make(chan struct{}) + if s.listener, err = s.sh.Listen(s.ctx); nil != err { + return err + } - s.sh.OnStart() + s.stopChan = make(chan struct{}) s.stopWg.Add(1) go handleServer(s) @@ -75,17 +77,12 @@ func (s *server) Stop() { logging.Logger().Info(fmt.Sprintf("Server[%s] is stopped", s.sh.GetName())) } -func (s *server) Serve() error { - if err := s.Start(); err != nil { - return err - } - s.stopWg.Wait() - return nil -} - func handleServer(s *server) { defer s.stopWg.Done() + logging.Logger().Info(fmt.Sprintf("Server[%s] is started", s.sh.GetName())) + s.sh.OnStart(s.ctx) + var conn net.Conn var err error var stopping atomic.Value