freifunkmanager/main.go

118 lines
3.0 KiB
Go
Raw Permalink 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"
2019-06-10 02:57:36 +02:00
"github.com/bdlm/log"
2018-06-30 01:45:51 +02:00
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"
2019-06-29 03:13:08 +02:00
_ "github.com/jinzhu/gorm/dialects/postgres"
2018-07-26 14:14:23 +02:00
_ "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 {
2019-06-29 03:08:01 +02:00
log.Panicf("failed to connect database: %s", err)
2018-07-26 14:14:23 +02:00
}
2018-08-10 17:33:39 +02:00
db.AutoMigrate(&runtime.Node{}, &websocket.Session{})
2018-07-26 14:14:23 +02:00
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 17:17:00 +02:00
ws := websocket.NewWebsocketServer(config.Secret, config.SSHIPAddressPrefix, db, config.BlacklistFor.Duration, nodesYanic)
2018-08-10 17:17:00 +02:00
yanic := runtime.NewYanicDB(db, sshmanager, config.BlacklistFor.Duration, ws.SendNode, ws.SendStats, config.SSHIPAddressPrefix)
if config.Pinger.Enable {
pinger, err := runtime.NewPinger(db, &config.Pinger, config.BlacklistFor.Duration, ws.SendPing)
if err != nil {
log.Panic(err)
}
go pinger.Start()
2019-06-03 15:38:46 +02:00
}
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)
}
2020-05-18 00:16:01 +02:00
collector = respondYanic.NewCollector(yanic, nodesYanic, &respondYanic.Config{
Enable: true,
Interfaces: []respondYanic.InterfaceConfig{config.Yanic},
Sites: make(map[string]respondYanic.SiteConfig),
})
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
2019-06-23 03:02:29 +02:00
log.Debug("stop receive:", sig)
2018-07-26 14:14:23 +02:00
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
}