92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
//////////////////////////////////////////////////////////////////////////
|
|
// DN42 GRC Daemon
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
package main
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
import (
|
|
"context"
|
|
log "github.com/sirupsen/logrus"
|
|
flag "github.com/spf13/pflag"
|
|
"os"
|
|
"os/signal"
|
|
)
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// utility function to set the log level
|
|
|
|
func setLogLevel(levelStr string) {
|
|
|
|
if level, err := log.ParseLevel(levelStr); err != nil {
|
|
// failed to set the level
|
|
|
|
// set a sensible default and, of course, log the error
|
|
log.SetLevel(log.InfoLevel)
|
|
log.WithFields(log.Fields{
|
|
"loglevel": levelStr,
|
|
"error": err,
|
|
}).Error("Failed to set requested log level")
|
|
|
|
} else {
|
|
|
|
// set the requested level
|
|
log.SetLevel(level)
|
|
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// everything starts here
|
|
|
|
func main() {
|
|
|
|
// set a default log level, so that logging can be used immediately
|
|
// the level will be overidden later on once the command line
|
|
// options are loaded
|
|
log.SetLevel(log.InfoLevel)
|
|
log.Info("DN42 GRC Daemon Starting")
|
|
|
|
var (
|
|
logLevel = flag.StringP("LogLevel", "l", "Info", "Log level")
|
|
socketPath = flag.StringP("SockPath", "p", "bird.sock", "Path to bird fifo")
|
|
bindAddress = flag.StringP("BindAddress", "b", "[::]:8050", "Server bind address")
|
|
staticRoot = flag.StringP("StaticRoot", "s", "StaticRoot", "Static page directory")
|
|
)
|
|
flag.Parse()
|
|
|
|
// now initialise logging properly based on the cmd line options
|
|
setLogLevel(*logLevel)
|
|
|
|
// start the API server and begin ingesting data
|
|
data := StartData()
|
|
server := StartAPIServer(*bindAddress, data, *staticRoot)
|
|
ingest := StartIngesting(*socketPath, data)
|
|
|
|
// graceful shutdown via SIGINT (^C)
|
|
csig := make(chan os.Signal, 1)
|
|
signal.Notify(csig, os.Interrupt)
|
|
|
|
// and block
|
|
<-csig
|
|
|
|
log.Info("Server shutting down")
|
|
|
|
// deadline for server to shutdown
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10)
|
|
defer cancel()
|
|
|
|
// shutdown the server and stop ingesting data
|
|
ingest.Shutdown()
|
|
server.Shutdown(ctx)
|
|
data.Shutdown()
|
|
|
|
// nothing left to do
|
|
log.Info("Shutdown complete, all done")
|
|
os.Exit(0)
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// end of file
|