2018-08-23 09:21:48 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"reflect"
|
|
|
|
|
2018-09-04 07:07:45 +00:00
|
|
|
oa "git.loafle.net/overflow/annotation-go"
|
|
|
|
oda "git.loafle.net/overflow/di-go/annotation"
|
2018-08-23 10:00:34 +00:00
|
|
|
olog "git.loafle.net/overflow/log-go"
|
2018-08-23 09:21:48 +00:00
|
|
|
"git.loafle.net/overflow/server-go"
|
|
|
|
ossw "git.loafle.net/overflow/server-go/socket/web"
|
2018-09-04 10:09:15 +00:00
|
|
|
"git.loafle.net/overflow_scanner/probe/internal/pubsub"
|
2018-09-04 07:07:45 +00:00
|
|
|
"go.uber.org/zap"
|
2018-08-23 09:21:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerHandler interface {
|
|
|
|
ossw.ServerHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
|
|
|
ossw.ServerHandlers
|
|
|
|
|
2018-09-04 10:09:15 +00:00
|
|
|
Services []interface{}
|
|
|
|
|
|
|
|
PubSub *pubsub.PubSub
|
2018-08-23 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-09-04 07:07:45 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 09:21:48 +00:00
|
|
|
|
2018-09-04 10:09:15 +00:00
|
|
|
sh.PubSub.Shutdown()
|
|
|
|
|
2018-08-23 09:21:48 +00:00
|
|
|
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 {
|
2018-08-23 10:00:34 +00:00
|
|
|
olog.Logger().Debug("Listener", zap.Int("port", i))
|
2018-08-23 09:21:48 +00:00
|
|
|
// 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
|
|
|
|
}
|