freifunkmanager/main.go

106 lines
2.6 KiB
Go
Raw Normal View History

2017-05-06 14:37:24 +02:00
package main
import (
"flag"
"net/http"
"os"
"os/signal"
"syscall"
2017-05-07 03:37:30 +02:00
"time"
2017-05-06 14:37:24 +02:00
2018-07-26 14:14:23 +02:00
"dev.sum7.eu/genofire/golang-lib/file"
2017-05-06 14:37:24 +02:00
"github.com/NYTimes/gziphandler"
2018-06-30 01:45:51 +02:00
log "github.com/sirupsen/logrus"
respondYanic "github.com/FreifunkBremen/yanic/respond"
runtimeYanic "github.com/FreifunkBremen/yanic/runtime"
2017-05-06 14:37:24 +02:00
2018-07-26 14:14:23 +02:00
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/FreifunkBremen/freifunkmanager/runtime"
2017-05-06 14:37:24 +02:00
"github.com/FreifunkBremen/freifunkmanager/ssh"
2017-05-08 19:13:29 +02:00
"github.com/FreifunkBremen/freifunkmanager/websocket"
2017-05-06 14:37:24 +02:00
)
var (
2018-06-30 01:45:51 +02:00
configFile string
config = &runtime.Config{}
collector *respondYanic.Collector
verbose bool
2017-05-06 14:37:24 +02:00
)
func main() {
flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)")
2018-06-30 01:45:51 +02:00
flag.BoolVar(&verbose, "v", false, "verbose logging")
2017-05-06 14:37:24 +02:00
flag.Parse()
2018-06-30 01:45:51 +02:00
if verbose {
log.SetLevel(log.DebugLevel)
}
2017-05-06 14:37:24 +02:00
2018-06-30 01:45:51 +02:00
if err := file.ReadTOML(configFile, config); err != nil {
log.Panicf("Error during read config: %s", err)
}
2017-05-06 14:37:24 +02:00
2018-06-30 01:45:51 +02:00
log.Info("starting...")
2017-05-06 14:37:24 +02:00
2018-07-26 14:14:23 +02:00
db, err := gorm.Open(config.DatabaseType, config.DatabaseConnection)
if err != nil {
log.Panic("failed to connect database")
}
db.AutoMigrate(&runtime.Node{}, &websocket.Auth{})
2018-07-24 15:07:11 +02:00
sshmanager := ssh.NewManager(config.SSHPrivateKey, config.SSHTimeout.Duration)
2018-06-30 01:45:51 +02:00
nodesYanic := runtimeYanic.NewNodes(&runtimeYanic.NodesConfig{})
2017-05-07 03:37:30 +02:00
2018-08-10 13:46:18 +02:00
ws := websocket.NewWebsocketServer(config.Secret, config.SSHIPAddressPrefix, db, nodesYanic)
2018-08-10 13:46:18 +02:00
yanic := runtime.NewYanicDB(db, sshmanager, ws.SendNode, ws.SendStats, config.SSHIPAddressPrefix)
2017-05-15 21:59:48 +02:00
if config.YanicEnable {
if duration := config.YanicSynchronize.Duration; duration > 0 {
now := time.Now()
delay := duration - now.Sub(now.Truncate(duration))
log.Printf("delaying %0.1f seconds", delay.Seconds())
time.Sleep(delay)
}
2018-07-26 14:14:23 +02:00
collector = respondYanic.NewCollector(yanic, nodesYanic, make(map[string][]string), []respondYanic.InterfaceConfig{config.Yanic})
if duration := config.YanicCollectInterval.Duration; duration > 0 {
collector.Start(config.YanicCollectInterval.Duration)
}
2018-06-30 01:45:51 +02:00
defer collector.Close()
log.Info("started Yanic collector")
}
2017-05-06 14:37:24 +02:00
2017-05-08 19:13:29 +02:00
// Startwebserver
http.Handle("/", gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot))))
2017-05-06 14:37:24 +02:00
srv := &http.Server{
2017-05-08 19:13:29 +02:00
Addr: config.WebserverBind,
2017-05-06 14:37:24 +02:00
}
2017-05-08 19:13:29 +02:00
2017-05-06 14:37:24 +02:00
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
2018-06-30 01:45:51 +02:00
log.Panic(err)
2017-05-06 14:37:24 +02:00
}
}()
2018-06-30 01:45:51 +02:00
log.Info("started")
2017-05-06 14:37:24 +02:00
// Wait for system signal
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
2018-07-26 14:14:23 +02:00
log.Debug("stop recieve:", sig)
ws.Close()
2018-06-30 01:45:51 +02:00
// Stop services
2017-05-06 14:37:24 +02:00
srv.Close()
2018-07-26 14:14:23 +02:00
db.Close()
log.Info("stop freifunkmanager")
2017-05-06 14:37:24 +02:00
}