91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"reflect"
|
|
|
|
oa "git.loafle.net/overflow/annotation-go"
|
|
oda "git.loafle.net/overflow/di-go/annotation"
|
|
olog "git.loafle.net/overflow/log-go"
|
|
"git.loafle.net/overflow/server-go"
|
|
ossw "git.loafle.net/overflow/server-go/socket/web"
|
|
"git.loafle.net/overflow_scanner/probe/internal/pubsub"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type ServerHandler interface {
|
|
ossw.ServerHandler
|
|
}
|
|
|
|
type ServerHandlers struct {
|
|
ossw.ServerHandlers
|
|
|
|
Services []interface{}
|
|
|
|
PubSub *pubsub.PubSub
|
|
}
|
|
|
|
func (sh *ServerHandlers) Init(serverCtx server.ServerCtx) error {
|
|
if err := sh.ServerHandlers.Init(serverCtx); nil != err {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sh *ServerHandlers) OnStart(serverCtx server.ServerCtx) error {
|
|
if err := sh.ServerHandlers.OnStart(serverCtx); nil != err {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sh *ServerHandlers) OnStop(serverCtx server.ServerCtx) {
|
|
for _, _service := range sh.Services {
|
|
as, err := oa.GetMethodAnnotationsByType(reflect.TypeOf(_service), oda.PreDestroyAnnotationType)
|
|
if nil == err || nil != as {
|
|
for k := range as {
|
|
ins := make([]reflect.Value, 0)
|
|
reflect.ValueOf(_service).MethodByName(k).Call(ins)
|
|
}
|
|
}
|
|
}
|
|
|
|
sh.PubSub.Shutdown()
|
|
|
|
sh.ServerHandlers.OnStop(serverCtx)
|
|
}
|
|
|
|
func (sh *ServerHandlers) Destroy(serverCtx server.ServerCtx) {
|
|
|
|
sh.ServerHandlers.Destroy(serverCtx)
|
|
}
|
|
|
|
func (sh *ServerHandlers) Listener(serverCtx server.ServerCtx) (net.Listener, error) {
|
|
for i := 60000; i < 61000; i++ {
|
|
addr := fmt.Sprintf("localhost:%d", i)
|
|
l, err := net.Listen("tcp", addr)
|
|
if nil == err {
|
|
olog.Logger().Debug("Listener", zap.Int("port", i))
|
|
// config.ProbePortNumber = int64(i)
|
|
return l, nil
|
|
}
|
|
}
|
|
|
|
return nil, fmt.Errorf("Cannot find availrable port")
|
|
}
|
|
|
|
func (sh *ServerHandlers) Validate() error {
|
|
if err := sh.ServerHandlers.Validate(); nil != err {
|
|
return err
|
|
}
|
|
|
|
if nil == sh.Services {
|
|
return fmt.Errorf("Services must be specified")
|
|
}
|
|
|
|
return nil
|
|
}
|