general improvements

This commit is contained in:
ston1th 2018-10-27 21:28:19 +02:00
commit ddb11f0619
25 changed files with 305 additions and 140 deletions

View file

@ -28,27 +28,27 @@ const (
)
var (
conf core.Config
dumpFile string
config string
config core.Config
dumpFile string
configFile string
)
func initServer(conf core.Config) (err error) {
func initServer(cfg core.Config) (err error) {
if err = godrop.PledgePromises("stdio rpath wpath cpath inet fattr flock proc exec id unveil"); err != nil {
return
}
dropCfg := godrop.Config{
User: conf.User,
Group: conf.Group,
Foreground: conf.Foreground,
User: cfg.User,
Group: cfg.Group,
Foreground: cfg.Foreground,
}
err = godrop.Drop(dropCfg, func() (net.Listener, error) {
return net.Listen("tcp", conf.ListenAddr)
return net.Listen("tcp", cfg.ListenAddr)
})
if err != nil {
return
}
if err = os.Chdir(conf.DataDir); err != nil {
if err = os.Chdir(cfg.DataDir); err != nil {
return
}
@ -61,14 +61,14 @@ func initServer(conf core.Config) (err error) {
return
}
log.InitLogger(conf.DataDir, conf.LogFile, conf.Debug)
log.InitLogger(cfg)
srv := server.NewHTTPServer(l, conf)
srv := server.NewHTTPServer(cfg, l)
err = srv.Start()
if err != nil {
return
}
log.Println("gowiki " + conf.Version + " started")
log.Println("gowiki " + cfg.Version + " started")
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
@ -86,14 +86,14 @@ func initServer(conf core.Config) (err error) {
}
func loadConfig() error {
if config == "" {
if configFile == "" {
return nil
}
file, err := os.Open(config)
file, err := os.Open(configFile)
if err != nil {
return err
}
err = json.NewDecoder(file).Decode(&conf)
err = json.NewDecoder(file).Decode(&config)
if err != nil {
return err
}
@ -114,8 +114,8 @@ func Run(version string) {
if err := loadConfig(); err != nil {
stdlog.Fatal("server: ", err)
}
conf.Version = app.Version
if err := initServer(conf); err != nil {
config.Version = app.Version
if err := initServer(config); err != nil {
stdlog.Fatal("server: ", err)
}
return nil
@ -139,7 +139,7 @@ func Run(version string) {
stdlog.Fatal("dump: ", err)
}
}
DB, err := db.New(conf.DataDir)
DB, err := db.New(config)
if err != nil {
stdlog.Fatal("dump: ", err)
}
@ -172,7 +172,7 @@ func Run(version string) {
stdlog.Fatal("restore: ", err)
}
}
DB, err := db.New(conf.DataDir)
DB, err := db.New(config)
if err != nil {
stdlog.Fatal("restore: ", err)
}
@ -197,45 +197,50 @@ func serverFlags() []cli.Flag {
Name: "user, u",
Value: defRunUser,
Usage: "drop privileges to user",
Destination: &conf.User,
Destination: &config.User,
},
cli.StringFlag{
Name: "group, g",
Value: defRunGroup,
Usage: "drop privileges to group",
Destination: &conf.Group,
Destination: &config.Group,
},
cli.StringFlag{
Name: "listen, l",
Value: defListen,
Usage: "listening <address>:<port>",
Destination: &conf.ListenAddr,
Destination: &config.ListenAddr,
},
cli.StringFlag{
Name: "log",
Value: defLogFile,
Usage: "log file (use - for stdout, this only works in combination with -f)",
Destination: &conf.LogFile,
Destination: &config.LogFile,
},
cli.StringFlag{
Name: "secret",
Usage: "static hmac secret (at least 64 chars, used for JWT signing)",
Destination: &conf.Secret,
Destination: &config.Secret,
},
cli.BoolTFlag{
Name: "admin, a",
Usage: "use -a=false to disable the admin user",
Destination: &config.Admin,
},
cli.BoolFlag{
Name: "foreground, f",
Usage: "do not fork into the background",
Destination: &conf.Foreground,
Destination: &config.Foreground,
},
cli.BoolFlag{
Name: "secure, s",
Usage: "enable secure cookie",
Destination: &conf.SecureCookie,
Destination: &config.SecureCookie,
},
cli.BoolFlag{
Name: "debug",
Usage: "enable debugging",
Destination: &conf.Debug,
Destination: &config.Debug,
},
}
}
@ -256,13 +261,13 @@ func defaultFlags(f []cli.Flag) []cli.Flag {
cli.StringFlag{
Name: "config, c",
Usage: "config file",
Destination: &config,
Destination: &configFile,
},
cli.StringFlag{
Name: "data, d",
Value: defDataDir,
Usage: "data directory",
Destination: &conf.DataDir,
Destination: &config.DataDir,
},
}, f...)
}