This commit is contained in:
crusader 2017-11-10 18:45:48 +09:00
parent 42c49a5914
commit 9d3486ca28
2 changed files with 34 additions and 7 deletions

View File

@ -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()

View File

@ -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)