2018-04-12 11:54:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.loafle.net/commons/configuration-go"
|
2018-04-14 08:57:01 +00:00
|
|
|
cdr "git.loafle.net/commons/di-go/registry"
|
2018-04-12 11:54:56 +00:00
|
|
|
"git.loafle.net/commons/logging-go"
|
2018-05-03 11:24:07 +00:00
|
|
|
cssw "git.loafle.net/commons/server-go/socket/web"
|
2018-04-26 08:39:32 +00:00
|
|
|
occp "git.loafle.net/overflow/commons-go/config/probe"
|
2018-04-12 11:54:56 +00:00
|
|
|
occi "git.loafle.net/overflow/commons-go/core/interfaces"
|
|
|
|
"git.loafle.net/overflow/probe/auth"
|
|
|
|
"git.loafle.net/overflow/probe/config"
|
2018-04-17 14:11:13 +00:00
|
|
|
"git.loafle.net/overflow/probe/probe"
|
2018-04-12 11:54:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-04-18 14:56:13 +00:00
|
|
|
logging.InitializeLogger(config.ProbeLoggingConfigFilePath())
|
2018-04-12 11:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
_config := &config.Config{}
|
2018-04-18 14:56:13 +00:00
|
|
|
if err := configuration.Load(_config, config.ProbeConfigFilePath()); nil != err {
|
2018-04-12 11:54:56 +00:00
|
|
|
logging.Logger().Panic(err)
|
|
|
|
}
|
2018-04-14 11:04:07 +00:00
|
|
|
|
|
|
|
cdr.RegisterResource(config.ConfigKey, _config)
|
2018-04-12 11:54:56 +00:00
|
|
|
|
|
|
|
var instance interface{}
|
2018-05-03 11:24:07 +00:00
|
|
|
var err error
|
2018-04-12 11:54:56 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-04-26 08:39:32 +00:00
|
|
|
if occp.ProbeStateTypeNotAuthorized == _config.Probe.State() {
|
2018-04-18 14:56:13 +00:00
|
|
|
instance = &auth.Authenticator{}
|
2018-04-12 11:54:56 +00:00
|
|
|
doneChan, err := instance.(occi.EndableStarter).EndableStart()
|
|
|
|
if nil != err {
|
2018-04-17 14:11:13 +00:00
|
|
|
logging.Logger().Error(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
err, ok = <-doneChan
|
|
|
|
if !ok {
|
2018-04-14 08:57:01 +00:00
|
|
|
return
|
2018-04-12 11:54:56 +00:00
|
|
|
}
|
|
|
|
if nil != err {
|
2018-04-17 14:11:13 +00:00
|
|
|
logging.Logger().Error(err)
|
|
|
|
os.Exit(1)
|
2018-04-12 11:54:56 +00:00
|
|
|
}
|
2018-04-14 08:57:01 +00:00
|
|
|
if err := instance.(occi.Stopper).Stop(context.Background()); err != nil {
|
|
|
|
logging.Logger().Errorf("error: %v", err)
|
|
|
|
}
|
2018-04-12 11:54:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 11:24:07 +00:00
|
|
|
p := probe.New()
|
|
|
|
instance = p
|
|
|
|
err = p.ListenAndServe()
|
2018-04-17 14:11:13 +00:00
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-05-03 11:24:07 +00:00
|
|
|
|
|
|
|
if err := p.Shutdown(context.Background()); err != nil {
|
2018-04-17 14:11:13 +00:00
|
|
|
logging.Logger().Errorf("error: %v", err)
|
|
|
|
}
|
2018-04-12 11:54:56 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt,
|
|
|
|
syscall.SIGKILL,
|
|
|
|
syscall.SIGSTOP,
|
|
|
|
syscall.SIGHUP,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGQUIT)
|
|
|
|
|
|
|
|
<-interrupt
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
2018-05-03 11:24:07 +00:00
|
|
|
|
|
|
|
switch v := instance.(type) {
|
|
|
|
case *auth.Authenticator:
|
|
|
|
err = v.Stop(ctx)
|
|
|
|
case *cssw.Server:
|
|
|
|
err = v.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2018-04-12 11:54:56 +00:00
|
|
|
logging.Logger().Errorf("error: %v", err)
|
|
|
|
}
|
|
|
|
}
|