70 lines
1.1 KiB
Go
70 lines
1.1 KiB
Go
package server
|
|
|
|
import "sync/atomic"
|
|
|
|
type ServerHandler interface {
|
|
ConnectionHandler
|
|
|
|
GetName() string
|
|
ServerCtx() ServerCtx
|
|
|
|
Init(serverCtx ServerCtx) error
|
|
OnStart(serverCtx ServerCtx) error
|
|
OnStop(serverCtx ServerCtx)
|
|
Destroy(serverCtx ServerCtx)
|
|
|
|
Validate() error
|
|
}
|
|
|
|
type ServerHandlers struct {
|
|
ConnectionHandlers
|
|
|
|
// Server name for sending in response headers.
|
|
//
|
|
// Default server name is used if left blank.
|
|
Name string `json:"name"`
|
|
|
|
validated atomic.Value
|
|
}
|
|
|
|
func (sh *ServerHandlers) ServerCtx() ServerCtx {
|
|
return NewServerCtx(nil)
|
|
}
|
|
|
|
func (sh *ServerHandlers) Init(serverCtx ServerCtx) error {
|
|
return nil
|
|
}
|
|
|
|
func (sh *ServerHandlers) OnStart(serverCtx ServerCtx) error {
|
|
return nil
|
|
}
|
|
|
|
func (sh *ServerHandlers) OnStop(serverCtx ServerCtx) {
|
|
|
|
}
|
|
|
|
func (sh *ServerHandlers) Destroy(serverCtx ServerCtx) {
|
|
|
|
}
|
|
|
|
func (sh *ServerHandlers) GetName() string {
|
|
return sh.Name
|
|
}
|
|
|
|
func (sh *ServerHandlers) Validate() error {
|
|
if nil != sh.validated.Load() {
|
|
return nil
|
|
}
|
|
sh.validated.Store(true)
|
|
|
|
if err := sh.ConnectionHandlers.Validate(); nil != err {
|
|
return err
|
|
}
|
|
|
|
if "" == sh.Name {
|
|
sh.Name = "Server"
|
|
}
|
|
|
|
return nil
|
|
}
|