diff --git a/config.go b/config.go index 26a85e7..9b9c8f5 100644 --- a/config.go +++ b/config.go @@ -11,8 +11,13 @@ import ( ) type Config struct { + Port string Username string Password string + Daemonize bool + Pidfile Pidfile + Logfile string + Errfile string Processes []*Process `toml:"process"` } diff --git a/goforever.go b/goforever.go index 55224d8..7a7926b 100644 --- a/goforever.go +++ b/goforever.go @@ -14,43 +14,41 @@ import ( var d = flag.Bool("d", false, "Daemonize goforever. Must be first flag") var conf = flag.String("conf", "goforever.toml", "Path to config file.") -var port = flag.Int("port", 8080, "Port for the server.") -var username = flag.String("username", "demo", "Username for basic auth.") -var password = flag.String("password", "test", "Password for basic auth.") var config *Config +var daemon *Process var Usage = func() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) flag.PrintDefaults() usage := ` Process subcommands - list List processes. + list List processes. show Show a process. start Start a process. stop Stop a process. restart Restart a process. + end Stop goforever and all processes ` fmt.Fprintln(os.Stderr, usage) } func init() { + flag.Parse() setConfig() setHost() daemon = &Process{ Name: "goforever", - Args: []string{"./goforever"}, + Args: []string{}, Command: "goforever", - Pidfile: "goforever.pid", - Logfile: "goforever.debug.log", - Errfile: "goforever.errors.log", + Pidfile: config.Pidfile, + Logfile: config.Logfile, + Errfile: config.Errfile, Respawn: 1, } flag.Usage = Usage } func main() { - flag.Parse() - daemon.Name = "goforever" if *d == true { daemon.Args = append(daemon.Args, os.Args[2:]...) daemon.start(daemon.Name) @@ -95,7 +93,6 @@ func Cli() string { } func RunDaemon() { - fmt.Printf("Running %s.\n", daemon.Name) daemon.children = make(map[string]*Process, 0) for _, name := range config.Keys() { daemon.children[name] = config.Get(name) @@ -104,18 +101,22 @@ func RunDaemon() { } func setConfig() { - c, err := LoadConfig(*conf) + var err error + config, err = LoadConfig(*conf) if err != nil { - log.Fatalf("Config error: %s", err) + log.Fatalf("%s", err) return } - config = c - - if config.Username != "" { - username = &config.Username + if config.Username == "" { + log.Fatalf("Config error: %s", "Please provide a username.") + return } - if config.Password != "" { - password = &config.Password + if config.Password == "" { + log.Fatalf("Config error: %s", "Please provide a password.") + return + } + if config.Port == "" { + config.Port = "2224" } } @@ -124,5 +125,7 @@ func setHost() { if isHttps() == false { scheme = "http" } - greq.Host = fmt.Sprintf("%s://%s:%s@0.0:%d", scheme, *username, *password, *port) + greq.Host = fmt.Sprintf("%s://%s:%s@0.0:%s", + scheme, config.Username, config.Password, config.Port, + ) } diff --git a/goforever.toml b/goforever.toml index 9d80c70..489beb6 100644 --- a/goforever.toml +++ b/goforever.toml @@ -1,6 +1,9 @@ +port = "2224" username = "go" password = "forever" - +pidfile = "goforever.pid" +logfile = "goforever.log" +errfile = "goforever.log" [[process]] name = "example-panic" @@ -15,6 +18,7 @@ ping = "30s" [[process]] name = "example" command = "./example/example" +args = ["-name=foo"] pidfile = "example/example.pid" logfile = "example/logs/example.debug.log" errfile = "example/logs/example.errors.log" diff --git a/goforever_test.go b/goforever_test.go new file mode 100644 index 0000000..eb07cfb --- /dev/null +++ b/goforever_test.go @@ -0,0 +1,24 @@ +// goforever - processes management +// Copyright (c) 2013 Garrett Woodworth (https://github.com/gwoo). + +package main + +import ( + //"fmt" + "os" + "testing" +) + +func Test_main(t *testing.T) { + if daemon.Name != "goforever" { + t.Error("Daemon name is not goforever") + } + os.Args = []string{"./goforever", "-d", "foo"} + dize := true + d = &dize + main() + if daemon.Args[0] != "foo" { + t.Error("First arg not foo") + } + daemon.stop() +} diff --git a/http.go b/http.go index d0d29cd..aea053f 100644 --- a/http.go +++ b/http.go @@ -13,19 +13,16 @@ import ( "strings" ) -var daemon *Process - func HttpServer() { http.HandleFunc("/favicon.ico", http.NotFound) http.HandleFunc("/", AuthHandler(Handler)) - fmt.Printf("goforever serving port %d\n", *port) - + fmt.Printf("goforever serving port %s\n", config.Port) if isHttps() == false { - http.ListenAndServe(fmt.Sprintf(":%d", *port), nil) + http.ListenAndServe(fmt.Sprintf(":%s", config.Port), nil) return } log.Printf("SSL enabled.\n") - http.ListenAndServeTLS(fmt.Sprintf(":%d", *port), "cert.pem", "key.pem", nil) + http.ListenAndServeTLS(fmt.Sprintf(":%s", config.Port), "cert.pem", "key.pem", nil) } func isHttps() bool { @@ -143,7 +140,7 @@ func AuthHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc { w.WriteHeader(http.StatusBadRequest) return } - if parts[0] == *username && parts[1] == *password { + if parts[0] == config.Username && parts[1] == config.Password { fn(w, r) return } diff --git a/process.go b/process.go index 8440a5d..7b89447 100644 --- a/process.go +++ b/process.go @@ -94,10 +94,8 @@ func (p *Process) start(name string) { NewLog(p.Errfile), }, } - if p.Args == nil { - p.Args = []string{} - } - process, err := os.StartProcess(p.Command, p.Args, proc) + args := append([]string{p.Name}, p.Args...) + process, err := os.StartProcess(p.Command, args, proc) if err != nil { log.Fatalf("%s failed. %s", p.Name, err) return diff --git a/process_test.go b/process_test.go index 7d91eb4..66f7820 100644 --- a/process_test.go +++ b/process_test.go @@ -8,11 +8,10 @@ import ( ) func TestPidfile(t *testing.T) { - c := &Config{"", "", - []*Process{&Process{ - Name: "test", - Pidfile: "test.pid", - }}, + c := &Config{Processes: []*Process{&Process{ + Name: "test", + Pidfile: "test.pid", + }}, } p := c.Get("test") err := p.Pidfile.write(100) @@ -34,16 +33,15 @@ func TestPidfile(t *testing.T) { } func TestProcessStart(t *testing.T) { - c := &Config{ - "", "", - []*Process{&Process{ - Name: "bash", - Command: "/bin/bash", - Pidfile: "echo.pid", - Logfile: "debug.log", - Errfile: "error.log", - Respawn: 3, - }}, + c := &Config{Processes: []*Process{&Process{ + Name: "bash", + Command: "/bin/bash", + Args: []string{"foo", "bar"}, + Pidfile: "echo.pid", + Logfile: "debug.log", + Errfile: "error.log", + Respawn: 3, + }}, } p := c.Get("bash") p.start("bash")