diff --git a/server.go b/server.go index af0ac25..7c1de37 100644 --- a/server.go +++ b/server.go @@ -72,13 +72,11 @@ func (s *server) Start() error { s.listener = newGracefulListener(listener, s.sh.GetMaxStopWaitTime()) - s.stopChan = make(chan struct{}) - - if err = s.sh.OnStart(); nil != err { - logging.Logger().Panic(fmt.Sprintf("Server: server cannot start %v", err)) + if err = s.sh.Init(); nil != err { + logging.Logger().Panic(fmt.Sprintf("Server: Initialization of server has been failed %v", err)) } - logging.Logger().Info(fmt.Sprintf("Server[%s] is started", s.sh.GetName())) + s.stopChan = make(chan struct{}) s.stopWg.Add(1) go handleServer(s) @@ -109,6 +107,9 @@ func handleServer(s *server) { } }() + logging.Logger().Info(fmt.Sprintf("Server[%s] is started", s.sh.GetName())) + s.sh.OnStart() + select { case <-s.stopChan: s.listener.Close() diff --git a/server_handler.go b/server_handler.go index de80858..c7828cc 100644 --- a/server_handler.go +++ b/server_handler.go @@ -8,11 +8,37 @@ import ( ) type ServerHandler interface { + // Init invoked before the server is started + // If you override ths method, must call + // + // func (sh *ServerHandler) Init() error { + // if err := sh.ServerHandler.Init(); nil != err { + // return err + // } + // ... + // return nil + // } + Init() error Listen() (net.Listener, error) + // OnStart invoked when server is started + // If you override ths method, must call + // + // func (sh *ServerHandler) OnStart() error { + // sh.ServerHandler.OnStart() + // ... + // return nil + // } + OnStart() + CheckOrigin(ctx *fasthttp.RequestCtx) bool OnError(ctx *fasthttp.RequestCtx, status int, reason error) - - OnStart() error + // OnStop invoked when server is stopped + // If you override ths method, must call + // + // func (sh *ServerHandler) OnStop() { + // ... + // sh.ServerHandler.OnStop() + // } OnStop() RegisterSocketHandler(path string, handler SocketHandler)