mirror of
https://github.com/gwoo/goforever.git
synced 2025-09-26 19:41:10 +08:00
Fixing args. Updating config to accept the port, pidfile, errfile, logfile.
This commit is contained in:
@@ -11,8 +11,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
Port string
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
|
Daemonize bool
|
||||||
|
Pidfile Pidfile
|
||||||
|
Logfile string
|
||||||
|
Errfile string
|
||||||
Processes []*Process `toml:"process"`
|
Processes []*Process `toml:"process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
43
goforever.go
43
goforever.go
@@ -14,43 +14,41 @@ import (
|
|||||||
|
|
||||||
var d = flag.Bool("d", false, "Daemonize goforever. Must be first flag")
|
var d = flag.Bool("d", false, "Daemonize goforever. Must be first flag")
|
||||||
var conf = flag.String("conf", "goforever.toml", "Path to config file.")
|
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 config *Config
|
||||||
|
var daemon *Process
|
||||||
|
|
||||||
var Usage = func() {
|
var Usage = func() {
|
||||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
usage := `
|
usage := `
|
||||||
Process subcommands
|
Process subcommands
|
||||||
list List processes.
|
list List processes.
|
||||||
show <process> Show a process.
|
show <process> Show a process.
|
||||||
start <process> Start a process.
|
start <process> Start a process.
|
||||||
stop <process> Stop a process.
|
stop <process> Stop a process.
|
||||||
restart <process> Restart a process.
|
restart <process> Restart a process.
|
||||||
|
end Stop goforever and all processes
|
||||||
`
|
`
|
||||||
fmt.Fprintln(os.Stderr, usage)
|
fmt.Fprintln(os.Stderr, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
flag.Parse()
|
||||||
setConfig()
|
setConfig()
|
||||||
setHost()
|
setHost()
|
||||||
daemon = &Process{
|
daemon = &Process{
|
||||||
Name: "goforever",
|
Name: "goforever",
|
||||||
Args: []string{"./goforever"},
|
Args: []string{},
|
||||||
Command: "goforever",
|
Command: "goforever",
|
||||||
Pidfile: "goforever.pid",
|
Pidfile: config.Pidfile,
|
||||||
Logfile: "goforever.debug.log",
|
Logfile: config.Logfile,
|
||||||
Errfile: "goforever.errors.log",
|
Errfile: config.Errfile,
|
||||||
Respawn: 1,
|
Respawn: 1,
|
||||||
}
|
}
|
||||||
flag.Usage = Usage
|
flag.Usage = Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
|
||||||
daemon.Name = "goforever"
|
|
||||||
if *d == true {
|
if *d == true {
|
||||||
daemon.Args = append(daemon.Args, os.Args[2:]...)
|
daemon.Args = append(daemon.Args, os.Args[2:]...)
|
||||||
daemon.start(daemon.Name)
|
daemon.start(daemon.Name)
|
||||||
@@ -95,7 +93,6 @@ func Cli() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RunDaemon() {
|
func RunDaemon() {
|
||||||
fmt.Printf("Running %s.\n", daemon.Name)
|
|
||||||
daemon.children = make(map[string]*Process, 0)
|
daemon.children = make(map[string]*Process, 0)
|
||||||
for _, name := range config.Keys() {
|
for _, name := range config.Keys() {
|
||||||
daemon.children[name] = config.Get(name)
|
daemon.children[name] = config.Get(name)
|
||||||
@@ -104,18 +101,22 @@ func RunDaemon() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setConfig() {
|
func setConfig() {
|
||||||
c, err := LoadConfig(*conf)
|
var err error
|
||||||
|
config, err = LoadConfig(*conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Config error: %s", err)
|
log.Fatalf("%s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
config = c
|
if config.Username == "" {
|
||||||
|
log.Fatalf("Config error: %s", "Please provide a username.")
|
||||||
if config.Username != "" {
|
return
|
||||||
username = &config.Username
|
|
||||||
}
|
}
|
||||||
if config.Password != "" {
|
if config.Password == "" {
|
||||||
password = &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 {
|
if isHttps() == false {
|
||||||
scheme = "http"
|
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,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
|
port = "2224"
|
||||||
username = "go"
|
username = "go"
|
||||||
password = "forever"
|
password = "forever"
|
||||||
|
pidfile = "goforever.pid"
|
||||||
|
logfile = "goforever.log"
|
||||||
|
errfile = "goforever.log"
|
||||||
|
|
||||||
[[process]]
|
[[process]]
|
||||||
name = "example-panic"
|
name = "example-panic"
|
||||||
@@ -15,6 +18,7 @@ ping = "30s"
|
|||||||
[[process]]
|
[[process]]
|
||||||
name = "example"
|
name = "example"
|
||||||
command = "./example/example"
|
command = "./example/example"
|
||||||
|
args = ["-name=foo"]
|
||||||
pidfile = "example/example.pid"
|
pidfile = "example/example.pid"
|
||||||
logfile = "example/logs/example.debug.log"
|
logfile = "example/logs/example.debug.log"
|
||||||
errfile = "example/logs/example.errors.log"
|
errfile = "example/logs/example.errors.log"
|
||||||
|
24
goforever_test.go
Normal file
24
goforever_test.go
Normal file
@@ -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()
|
||||||
|
}
|
11
http.go
11
http.go
@@ -13,19 +13,16 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var daemon *Process
|
|
||||||
|
|
||||||
func HttpServer() {
|
func HttpServer() {
|
||||||
http.HandleFunc("/favicon.ico", http.NotFound)
|
http.HandleFunc("/favicon.ico", http.NotFound)
|
||||||
http.HandleFunc("/", AuthHandler(Handler))
|
http.HandleFunc("/", AuthHandler(Handler))
|
||||||
fmt.Printf("goforever serving port %d\n", *port)
|
fmt.Printf("goforever serving port %s\n", config.Port)
|
||||||
|
|
||||||
if isHttps() == false {
|
if isHttps() == false {
|
||||||
http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
|
http.ListenAndServe(fmt.Sprintf(":%s", config.Port), nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("SSL enabled.\n")
|
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 {
|
func isHttps() bool {
|
||||||
@@ -143,7 +140,7 @@ func AuthHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
|
|||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if parts[0] == *username && parts[1] == *password {
|
if parts[0] == config.Username && parts[1] == config.Password {
|
||||||
fn(w, r)
|
fn(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@@ -94,10 +94,8 @@ func (p *Process) start(name string) {
|
|||||||
NewLog(p.Errfile),
|
NewLog(p.Errfile),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if p.Args == nil {
|
args := append([]string{p.Name}, p.Args...)
|
||||||
p.Args = []string{}
|
process, err := os.StartProcess(p.Command, args, proc)
|
||||||
}
|
|
||||||
process, err := os.StartProcess(p.Command, p.Args, proc)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%s failed. %s", p.Name, err)
|
log.Fatalf("%s failed. %s", p.Name, err)
|
||||||
return
|
return
|
||||||
|
@@ -8,11 +8,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestPidfile(t *testing.T) {
|
func TestPidfile(t *testing.T) {
|
||||||
c := &Config{"", "",
|
c := &Config{Processes: []*Process{&Process{
|
||||||
[]*Process{&Process{
|
Name: "test",
|
||||||
Name: "test",
|
Pidfile: "test.pid",
|
||||||
Pidfile: "test.pid",
|
}},
|
||||||
}},
|
|
||||||
}
|
}
|
||||||
p := c.Get("test")
|
p := c.Get("test")
|
||||||
err := p.Pidfile.write(100)
|
err := p.Pidfile.write(100)
|
||||||
@@ -34,16 +33,15 @@ func TestPidfile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessStart(t *testing.T) {
|
func TestProcessStart(t *testing.T) {
|
||||||
c := &Config{
|
c := &Config{Processes: []*Process{&Process{
|
||||||
"", "",
|
Name: "bash",
|
||||||
[]*Process{&Process{
|
Command: "/bin/bash",
|
||||||
Name: "bash",
|
Args: []string{"foo", "bar"},
|
||||||
Command: "/bin/bash",
|
Pidfile: "echo.pid",
|
||||||
Pidfile: "echo.pid",
|
Logfile: "debug.log",
|
||||||
Logfile: "debug.log",
|
Errfile: "error.log",
|
||||||
Errfile: "error.log",
|
Respawn: 3,
|
||||||
Respawn: 3,
|
}},
|
||||||
}},
|
|
||||||
}
|
}
|
||||||
p := c.Get("bash")
|
p := c.Get("bash")
|
||||||
p.start("bash")
|
p.start("bash")
|
||||||
|
Reference in New Issue
Block a user