110 lines
2.8 KiB
Go
110 lines
2.8 KiB
Go
//////////////////////////////////////////////////////////////////////////
|
|
// lgregmapper, a small utility to provide DN42 registry data to bird-lg
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
package main
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
import (
|
|
// "fmt"
|
|
log "github.com/sirupsen/logrus"
|
|
flag "github.com/spf13/pflag"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
)
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// 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("LG Reg Mapper Server Starting")
|
|
|
|
// declare cmd line options
|
|
var (
|
|
logLevel = flag.StringP("LogLevel", "l", "Info", "Log level")
|
|
refreshInterval = flag.StringP("Refresh", "i", "10m", "Refresh interval")
|
|
bindAddress = flag.StringP("BindAddress", "b", "localhost:11211",
|
|
"Server bind address")
|
|
apiAddress = flag.StringP("APIAddress", "a", "https://explorer.burble.dn42/",
|
|
"DN42 API server address")
|
|
)
|
|
flag.Parse()
|
|
|
|
// now initialise logging properly based on the cmd line options
|
|
setLogLevel(*logLevel)
|
|
|
|
// parse the refreshInterval and start data collection
|
|
interval, err := time.ParseDuration(*refreshInterval)
|
|
if err != nil {
|
|
log.WithFields(log.Fields{
|
|
"error": err,
|
|
"interval": *refreshInterval,
|
|
}).Fatal("Unable to parse registry refresh interval")
|
|
}
|
|
|
|
// fetch initial registry data and start regular updates
|
|
regc := NewRegClient(*apiAddress)
|
|
regc.Update()
|
|
go regc.Start(interval)
|
|
|
|
listener, err := net.Listen("tcp", *bindAddress)
|
|
if err != nil {
|
|
log.WithFields(log.Fields{
|
|
"BindAddress": *bindAddress,
|
|
"Error": err,
|
|
}).Fatal("Failed to bind listener")
|
|
}
|
|
log.WithFields(log.Fields{
|
|
"BindAddress": *bindAddress,
|
|
}).Info("Listening")
|
|
|
|
// create a new server and start it working on the listen port
|
|
fmc := NewFakeMemcache(regc)
|
|
go fmc.Start(listener)
|
|
|
|
// sigint will perform a graceful exit
|
|
csig := make(chan os.Signal)
|
|
signal.Notify(csig, os.Interrupt)
|
|
|
|
// and block
|
|
<-csig
|
|
|
|
log.Info("Server shutting down")
|
|
regc.Shutdown()
|
|
fmc.Shutdown()
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// end of code
|