Logging
This commit is contained in:
parent
bd865fd55d
commit
ab955d4d1c
15 changed files with 161 additions and 65 deletions
|
@ -28,7 +28,7 @@ var cmdAccess = &cli.Command{
|
|||
Usage: "Grant/revoke access to a topic, or show access",
|
||||
UsageText: "ntfy access [USERNAME [TOPIC [PERMISSION]]]",
|
||||
Flags: flagsAccess,
|
||||
Before: initLogFunc(initConfigFileInputSourceFunc("config", flagsAccess)),
|
||||
Before: initConfigFileInputSourceFunc("config", flagsAccess, initLogFunc),
|
||||
Action: execUserAccess,
|
||||
Category: categoryServer,
|
||||
Description: `Manage the access control list for the ntfy server.
|
||||
|
|
24
cmd/app.go
24
cmd/app.go
|
@ -3,6 +3,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/urfave/cli/v2/altsrc"
|
||||
"heckel.io/ntfy/log"
|
||||
"os"
|
||||
)
|
||||
|
@ -16,7 +17,7 @@ var commands = make([]*cli.Command, 0)
|
|||
|
||||
var flagsDefault = []cli.Flag{
|
||||
&cli.BoolFlag{Name: "debug", Aliases: []string{"d"}, EnvVars: []string{"NTFY_DEBUG"}, Usage: "enable debug logging"},
|
||||
&cli.StringFlag{Name: "log-level", Aliases: []string{"log_level"}, Value: log.InfoLevel.String(), EnvVars: []string{"NTFY_LOG_LEVEL"}, Usage: "set log level"},
|
||||
altsrc.NewStringFlag(&cli.StringFlag{Name: "log-level", Aliases: []string{"log_level"}, Value: log.InfoLevel.String(), EnvVars: []string{"NTFY_LOG_LEVEL"}, Usage: "set log level"}),
|
||||
}
|
||||
|
||||
// New creates a new CLI application
|
||||
|
@ -32,22 +33,15 @@ func New() *cli.App {
|
|||
ErrWriter: os.Stderr,
|
||||
Commands: commands,
|
||||
Flags: flagsDefault,
|
||||
Before: initLogFunc(nil),
|
||||
Before: initLogFunc,
|
||||
}
|
||||
}
|
||||
|
||||
func initLogFunc(next cli.BeforeFunc) cli.BeforeFunc {
|
||||
return func(c *cli.Context) error {
|
||||
if c.Bool("debug") {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
} else {
|
||||
log.SetLevel(log.ToLevel(c.String("log-level")))
|
||||
}
|
||||
if next != nil {
|
||||
if err := next(c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
func initLogFunc(c *cli.Context) error {
|
||||
if c.Bool("debug") {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
} else {
|
||||
log.SetLevel(log.ToLevel(c.String("log-level")))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
// initConfigFileInputSourceFunc is like altsrc.InitInputSourceWithContext and altsrc.NewYamlSourceFromFlagFunc, but checks
|
||||
// if the config flag is exists and only loads it if it does. If the flag is set and the file exists, it fails.
|
||||
func initConfigFileInputSourceFunc(configFlag string, flags []cli.Flag) cli.BeforeFunc {
|
||||
func initConfigFileInputSourceFunc(configFlag string, flags []cli.Flag, next cli.BeforeFunc) cli.BeforeFunc {
|
||||
return func(context *cli.Context) error {
|
||||
configFile := context.String(configFlag)
|
||||
if context.IsSet(configFlag) && !util.FileExists(configFile) {
|
||||
|
@ -23,7 +23,15 @@ func initConfigFileInputSourceFunc(configFlag string, flags []cli.Flag) cli.Befo
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return altsrc.ApplyInputSourceValues(context, inputSource, flags)
|
||||
if err := altsrc.ApplyInputSourceValues(context, inputSource, flags); err != nil {
|
||||
return err
|
||||
}
|
||||
if next != nil {
|
||||
if err := next(context); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ var cmdPublish = &cli.Command{
|
|||
Action: execPublish,
|
||||
Category: categoryClient,
|
||||
Flags: flagsPublish,
|
||||
Before: initLogFunc(nil),
|
||||
Before: initLogFunc,
|
||||
Description: `Publish a message to a ntfy server.
|
||||
|
||||
Examples:
|
||||
|
|
45
cmd/serve.go
45
cmd/serve.go
|
@ -8,7 +8,10 @@ import (
|
|||
"heckel.io/ntfy/log"
|
||||
"math"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
|
@ -21,9 +24,13 @@ func init() {
|
|||
commands = append(commands, cmdServe)
|
||||
}
|
||||
|
||||
const (
|
||||
defaultServerConfigFile = "/etc/ntfy/server.yml"
|
||||
)
|
||||
|
||||
var flagsServe = append(
|
||||
flagsDefault,
|
||||
&cli.StringFlag{Name: "config", Aliases: []string{"c"}, EnvVars: []string{"NTFY_CONFIG_FILE"}, Value: "/etc/ntfy/server.yml", DefaultText: "/etc/ntfy/server.yml", Usage: "config file"},
|
||||
&cli.StringFlag{Name: "config", Aliases: []string{"c"}, EnvVars: []string{"NTFY_CONFIG_FILE"}, Value: defaultServerConfigFile, DefaultText: defaultServerConfigFile, Usage: "config file"},
|
||||
altsrc.NewStringFlag(&cli.StringFlag{Name: "base-url", Aliases: []string{"base_url", "B"}, EnvVars: []string{"NTFY_BASE_URL"}, Usage: "externally visible base URL for this host (e.g. https://ntfy.sh)"}),
|
||||
altsrc.NewStringFlag(&cli.StringFlag{Name: "listen-http", Aliases: []string{"listen_http", "l"}, EnvVars: []string{"NTFY_LISTEN_HTTP"}, Value: server.DefaultListenHTTP, Usage: "ip:port used to as HTTP listen address"}),
|
||||
altsrc.NewStringFlag(&cli.StringFlag{Name: "listen-https", Aliases: []string{"listen_https", "L"}, EnvVars: []string{"NTFY_LISTEN_HTTPS"}, Usage: "ip:port used to as HTTPS listen address"}),
|
||||
|
@ -69,7 +76,7 @@ var cmdServe = &cli.Command{
|
|||
Action: execServe,
|
||||
Category: categoryServer,
|
||||
Flags: flagsServe,
|
||||
Before: initLogFunc(initConfigFileInputSourceFunc("config", flagsServe)),
|
||||
Before: initConfigFileInputSourceFunc("config", flagsServe, initLogFunc),
|
||||
Description: `Run the ntfy server and listen for incoming requests
|
||||
|
||||
The command will load the configuration from /etc/ntfy/server.yml. Config options can
|
||||
|
@ -86,6 +93,7 @@ func execServe(c *cli.Context) error {
|
|||
}
|
||||
|
||||
// Read all the options
|
||||
config := c.String("config")
|
||||
baseURL := c.String("base-url")
|
||||
listenHTTP := c.String("listen-http")
|
||||
listenHTTPS := c.String("listen-https")
|
||||
|
@ -241,11 +249,15 @@ func execServe(c *cli.Context) error {
|
|||
conf.VisitorEmailLimitReplenish = visitorEmailLimitReplenish
|
||||
conf.BehindProxy = behindProxy
|
||||
conf.EnableWeb = enableWeb
|
||||
|
||||
// Set up hot-reloading of config
|
||||
go sigHandlerConfigReload(config)
|
||||
|
||||
// Run server
|
||||
s, err := server.New(conf)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := s.Run(); err != nil {
|
||||
} else if err := s.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Info("Exiting.")
|
||||
|
@ -262,3 +274,28 @@ func parseSize(s string, defaultValue int64) (v int64, err error) {
|
|||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func sigHandlerConfigReload(config string) {
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGHUP)
|
||||
for range sigs {
|
||||
log.Info("Partially hot reloading configuration ...")
|
||||
inputSource, err := newYamlSourceFromFile(config, flagsServe)
|
||||
if err != nil {
|
||||
log.Warn("Hot reload failed: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
reloadLogLevel(inputSource)
|
||||
}
|
||||
}
|
||||
|
||||
func reloadLogLevel(inputSource altsrc.InputSourceContext) {
|
||||
newLevelStr, err := inputSource.String("log-level")
|
||||
if err != nil {
|
||||
log.Warn("Cannot load log level: %s", err.Error())
|
||||
return
|
||||
}
|
||||
newLevel := log.ToLevel(newLevelStr)
|
||||
log.SetLevel(newLevel)
|
||||
log.Info("Log level is %s", newLevel.String())
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ var cmdSubscribe = &cli.Command{
|
|||
Action: execSubscribe,
|
||||
Category: categoryClient,
|
||||
Flags: flagsSubscribe,
|
||||
Before: initLogFunc(nil),
|
||||
Before: initLogFunc,
|
||||
Description: `Subscribe to a topic from a ntfy server, and either print or execute a command for
|
||||
every arriving message. There are 3 modes in which the command can be run:
|
||||
|
||||
|
@ -253,7 +253,7 @@ func loadConfig(c *cli.Context) (*client.Config, error) {
|
|||
if filename != "" {
|
||||
return client.LoadConfig(filename)
|
||||
}
|
||||
configFile := defaultConfigFile()
|
||||
configFile := defaultClientConfigFile()
|
||||
if s, _ := os.Stat(configFile); s != nil {
|
||||
return client.LoadConfig(configFile)
|
||||
}
|
||||
|
@ -261,7 +261,7 @@ func loadConfig(c *cli.Context) (*client.Config, error) {
|
|||
}
|
||||
|
||||
//lint:ignore U1000 Conditionally used in different builds
|
||||
func defaultConfigFileUnix() string {
|
||||
func defaultClientConfigFileUnix() string {
|
||||
u, _ := user.Current()
|
||||
configFile := clientRootConfigFileUnixAbsolute
|
||||
if u.Uid != "0" {
|
||||
|
@ -272,7 +272,7 @@ func defaultConfigFileUnix() string {
|
|||
}
|
||||
|
||||
//lint:ignore U1000 Conditionally used in different builds
|
||||
func defaultConfigFileWindows() string {
|
||||
func defaultClientConfigFileWindows() string {
|
||||
homeDir, _ := os.UserConfigDir()
|
||||
return filepath.Join(homeDir, clientUserConfigFileWindowsRelative)
|
||||
}
|
||||
|
|
|
@ -11,6 +11,6 @@ var (
|
|||
scriptLauncher = []string{"sh", "-c"}
|
||||
)
|
||||
|
||||
func defaultConfigFile() string {
|
||||
return defaultConfigFileUnix()
|
||||
func defaultClientConfigFile() string {
|
||||
return defaultClientConfigFileUnix()
|
||||
}
|
||||
|
|
|
@ -11,6 +11,6 @@ var (
|
|||
scriptLauncher = []string{"sh", "-c"}
|
||||
)
|
||||
|
||||
func defaultConfigFile() string {
|
||||
return defaultConfigFileUnix()
|
||||
func defaultClientConfigFile() string {
|
||||
return defaultClientConfigFileUnix()
|
||||
}
|
||||
|
|
|
@ -11,5 +11,5 @@ var (
|
|||
)
|
||||
|
||||
func defaultConfigFile() string {
|
||||
return defaultConfigFileWindows()
|
||||
return defaultClientConfigFileWindows()
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ var cmdUser = &cli.Command{
|
|||
Usage: "Manage/show users",
|
||||
UsageText: "ntfy user [list|add|remove|change-pass|change-role] ...",
|
||||
Flags: flagsUser,
|
||||
Before: initLogFunc(initConfigFileInputSourceFunc("config", flagsUser)),
|
||||
Before: initConfigFileInputSourceFunc("config", flagsUser, initLogFunc),
|
||||
Category: categoryServer,
|
||||
Subcommands: []*cli.Command{
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue