package main

import (
	"flag"
	"os"
	"os/signal"
	"syscall"

	"github.com/sjy-dv/scdb/cluster/config"
	"github.com/sjy-dv/scdb/cluster/server"
)

var (
	listen   string
	raftdir  string
	raftbind string
	node     string
	join     string
)

func init() {
	flag.StringVar(&listen, "listen", ":5051", "server listen address")
	flag.StringVar(&raftdir, "raftdir", "./", "raft data directory")
	flag.StringVar(&raftbind, "raftbind", ":15051", "raft bus transport bind address")
	flag.StringVar(&node, "id", "", "node id")
	flag.StringVar(&join, "join", "", "join to already exist cluster")
}

func main() {
	flag.Parse()
	var (
		c *config.Config
	)

	c = config.NewConfig(listen, raftdir, raftbind, node, join)

	app := server.NewApp(c)

	quitCh := make(chan os.Signal, 1)
	signal.Notify(quitCh, os.Kill, os.Interrupt, syscall.SIGHUP,
		syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

	go app.Run()

	<-quitCh
}
