2017-09-17 01:26:19 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/database"
|
2017-05-20 12:46:29 +00:00
|
|
|
allDatabase "github.com/FreifunkBremen/yanic/database/all"
|
|
|
|
"github.com/FreifunkBremen/yanic/output"
|
|
|
|
allOutput "github.com/FreifunkBremen/yanic/output/all"
|
2017-09-17 01:26:19 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/respond"
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
"github.com/FreifunkBremen/yanic/webserver"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// serveCmd represents the serve command
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Short: "Runs the yanic server",
|
2017-10-01 22:44:21 +00:00
|
|
|
Example: "yanic serve --config /etc/yanic.toml",
|
2017-09-17 01:26:19 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
config := loadConfig()
|
|
|
|
|
2017-05-20 12:46:29 +00:00
|
|
|
connections, err := allDatabase.Connect(config.Database.Connection)
|
2017-09-17 01:26:19 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
database.Start(connections, config)
|
|
|
|
defer database.Close(connections)
|
|
|
|
|
|
|
|
nodes = runtime.NewNodes(config)
|
|
|
|
nodes.Start()
|
2017-05-20 12:46:29 +00:00
|
|
|
|
|
|
|
outputs, err := allOutput.Register(config.Nodes.Output)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
output.Start(outputs, nodes, config)
|
|
|
|
defer output.Close()
|
2017-09-17 01:26:19 +00:00
|
|
|
|
|
|
|
if config.Webserver.Enable {
|
|
|
|
log.Println("starting webserver on", config.Webserver.Bind)
|
|
|
|
srv := webserver.New(config.Webserver.Bind, config.Webserver.Webroot)
|
2017-10-12 13:25:00 +00:00
|
|
|
go webserver.Start(srv)
|
|
|
|
defer srv.Close()
|
2017-09-17 01:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Respondd.Enable {
|
|
|
|
// Delaying startup to start at a multiple of `duration` since the zero time.
|
|
|
|
if duration := config.Respondd.Synchronize.Duration; duration > 0 {
|
|
|
|
now := time.Now()
|
|
|
|
delay := duration - now.Sub(now.Truncate(duration))
|
|
|
|
log.Printf("delaying %0.1f seconds", delay.Seconds())
|
|
|
|
time.Sleep(delay)
|
|
|
|
}
|
|
|
|
|
2017-10-18 16:22:14 +00:00
|
|
|
collector = respond.NewCollector(connections, nodes, config.Respondd.Interfaces, config.Respondd.Port)
|
2017-09-17 01:26:19 +00:00
|
|
|
collector.Start(config.Respondd.CollectInterval.Duration)
|
|
|
|
defer collector.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for INT/TERM
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
|
|
|
log.Println("received", sig)
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(serveCmd)
|
|
|
|
serveCmd.Flags().StringVarP(&configPath, "config", "c", "config.toml", "Path to configuration file")
|
|
|
|
}
|