ledisdb/cmd/ledis-server/main.go

86 lines
1.7 KiB
Go
Raw Normal View History

2014-05-05 07:37:44 +04:00
package main
import (
"flag"
2014-09-18 06:25:15 +04:00
"fmt"
"github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/server"
2014-08-05 04:43:04 +04:00
"log"
"net/http"
_ "net/http/pprof"
2014-05-05 07:37:44 +04:00
"os"
"os/signal"
2014-05-09 05:17:28 +04:00
"runtime"
2014-05-05 07:37:44 +04:00
"syscall"
)
var configFile = flag.String("config", "", "ledisdb config file")
2014-07-26 14:39:54 +04:00
var dbName = flag.String("db_name", "", "select a db to use, it will overwrite the config's db name")
2014-09-18 06:25:15 +04:00
var usePprof = flag.Bool("pprof", false, "enable pprof")
var pprofPort = flag.Int("pprof_port", 6060, "pprof http port")
2014-10-10 05:49:16 +04:00
var slaveof = flag.String("slaveof", "", "make the server a slave of another instance")
var readonly = flag.Bool("readonly", false, "set readonly mode, salve server is always readonly")
2014-10-11 12:00:59 +04:00
var rpl = flag.Bool("rpl", false, "enable replication or not, slave server is always enabled")
2014-05-05 07:37:44 +04:00
func main() {
2014-05-09 05:17:28 +04:00
runtime.GOMAXPROCS(runtime.NumCPU())
2014-05-05 07:37:44 +04:00
flag.Parse()
var cfg *config.Config
var err error
2014-05-05 07:37:44 +04:00
if len(*configFile) == 0 {
println("no config set, using default config")
cfg = config.NewConfigDefault()
} else {
cfg, err = config.NewConfigWithFile(*configFile)
2014-05-05 07:37:44 +04:00
}
if err != nil {
2014-07-02 06:13:10 +04:00
println(err.Error())
return
2014-05-05 07:37:44 +04:00
}
2014-07-26 14:39:54 +04:00
if len(*dbName) > 0 {
cfg.DBName = *dbName
2014-07-25 20:46:03 +04:00
}
2014-10-10 05:49:16 +04:00
if len(*slaveof) > 0 {
cfg.SlaveOf = *slaveof
cfg.Readonly = true
2014-10-11 12:00:59 +04:00
cfg.UseReplication = true
2014-10-10 05:49:16 +04:00
} else {
cfg.Readonly = *readonly
2014-10-11 12:00:59 +04:00
cfg.UseReplication = *rpl
2014-10-10 05:49:16 +04:00
}
var app *server.App
app, err = server.NewApp(cfg)
2014-05-05 07:37:44 +04:00
if err != nil {
2014-07-02 06:13:10 +04:00
println(err.Error())
return
2014-05-05 07:37:44 +04:00
}
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
<-sc
app.Close()
}()
2014-09-18 06:25:15 +04:00
if *usePprof {
go func() {
log.Println(http.ListenAndServe(fmt.Sprintf(":%d", *pprofPort), nil))
}()
}
2014-08-05 04:43:04 +04:00
2014-05-05 07:37:44 +04:00
app.Run()
}