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 {
|
||||
Port string
|
||||
Username string
|
||||
Password string
|
||||
Daemonize bool
|
||||
Pidfile Pidfile
|
||||
Logfile string
|
||||
Errfile string
|
||||
Processes []*Process `toml:"process"`
|
||||
}
|
||||
|
||||
|
41
goforever.go
41
goforever.go
@@ -14,10 +14,8 @@ 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])
|
||||
@@ -29,28 +27,28 @@ Process subcommands
|
||||
start <process> Start a process.
|
||||
stop <process> Stop a process.
|
||||
restart <process> 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,
|
||||
)
|
||||
}
|
||||
|
@@ -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"
|
||||
|
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"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -8,8 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestPidfile(t *testing.T) {
|
||||
c := &Config{"", "",
|
||||
[]*Process{&Process{
|
||||
c := &Config{Processes: []*Process{&Process{
|
||||
Name: "test",
|
||||
Pidfile: "test.pid",
|
||||
}},
|
||||
@@ -34,11 +33,10 @@ func TestPidfile(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProcessStart(t *testing.T) {
|
||||
c := &Config{
|
||||
"", "",
|
||||
[]*Process{&Process{
|
||||
c := &Config{Processes: []*Process{&Process{
|
||||
Name: "bash",
|
||||
Command: "/bin/bash",
|
||||
Args: []string{"foo", "bar"},
|
||||
Pidfile: "echo.pid",
|
||||
Logfile: "debug.log",
|
||||
Errfile: "error.log",
|
||||
|
Reference in New Issue
Block a user