server-go/server-handler.go

70 lines
1.1 KiB
Go
Raw Normal View History

2018-04-03 08:55:48 +00:00
package server
2018-04-13 07:11:43 +00:00
import "sync/atomic"
2018-04-03 08:55:48 +00:00
type ServerHandler interface {
2018-04-06 11:47:32 +00:00
ConnectionHandler
2018-04-04 13:28:35 +00:00
GetName() string
2018-04-04 04:01:26 +00:00
ServerCtx() ServerCtx
2018-04-03 08:55:48 +00:00
2018-04-04 04:01:26 +00:00
Init(serverCtx ServerCtx) error
OnStart(serverCtx ServerCtx) error
OnStop(serverCtx ServerCtx)
Destroy(serverCtx ServerCtx)
2018-04-03 08:55:48 +00:00
2018-04-04 04:01:26 +00:00
Validate() error
2018-04-03 08:55:48 +00:00
}
type ServerHandlers struct {
2018-04-06 11:47:32 +00:00
ConnectionHandlers
2018-04-04 04:01:26 +00:00
// Server name for sending in response headers.
//
// Default server name is used if left blank.
2018-04-14 08:49:33 +00:00
Name string `json:"name,omitempty"`
2018-04-13 07:11:43 +00:00
validated atomic.Value
2018-04-03 08:55:48 +00:00
}
2018-04-04 04:01:26 +00:00
func (sh *ServerHandlers) ServerCtx() ServerCtx {
2018-04-04 06:35:07 +00:00
return NewServerCtx(nil)
2018-04-03 08:55:48 +00:00
}
2018-04-04 04:01:26 +00:00
func (sh *ServerHandlers) Init(serverCtx ServerCtx) error {
2018-04-03 08:55:48 +00:00
return nil
}
2018-04-04 04:01:26 +00:00
func (sh *ServerHandlers) OnStart(serverCtx ServerCtx) error {
2018-04-03 08:55:48 +00:00
return nil
}
2018-04-04 04:01:26 +00:00
func (sh *ServerHandlers) OnStop(serverCtx ServerCtx) {
2018-04-03 08:55:48 +00:00
}
2018-04-04 04:01:26 +00:00
func (sh *ServerHandlers) Destroy(serverCtx ServerCtx) {
2018-04-03 08:55:48 +00:00
}
2018-04-04 04:01:26 +00:00
func (sh *ServerHandlers) GetName() string {
return sh.Name
2018-04-03 08:55:48 +00:00
}
2018-04-04 04:01:26 +00:00
func (sh *ServerHandlers) Validate() error {
2018-04-13 07:11:43 +00:00
if nil != sh.validated.Load() {
return nil
}
sh.validated.Store(true)
2018-04-06 11:47:32 +00:00
if err := sh.ConnectionHandlers.Validate(); nil != err {
return err
}
2018-04-04 13:28:35 +00:00
if "" == sh.Name {
sh.Name = "Server"
2018-04-04 04:01:26 +00:00
}
return nil
2018-04-03 08:55:48 +00:00
}