Make timeouts configurable for watchdog process. Make debug into container optional.

This commit is contained in:
Alex 2017-01-23 23:26:31 +00:00
parent 06291c33fe
commit b7d2845d6e

View File

@ -7,23 +7,53 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"time" "time"
) )
func main() { func main() {
readTimeoutStr := os.Getenv("read_timeout")
writeTimeoutStr := os.Getenv("write_timeout")
writeDebugStr := os.Getenv("write_debug")
process := os.Getenv("fprocess")
readTimeout := 5 * time.Second
writeTimeout := 5 * time.Second
writeDebug := true
if len(process) == 0 {
log.Panicln("Provide a valid process via fprocess environmental variable.")
return
}
if len(writeDebugStr) > 0 && writeDebugStr == "false" {
writeDebug = false
}
if len(readTimeoutStr) > 0 {
parsedVal, parseErr := strconv.Atoi(readTimeoutStr)
if parseErr == nil && parsedVal > 0 {
readTimeout = time.Duration(parsedVal) * time.Second
}
}
if len(writeTimeoutStr) > 0 {
parsedVal, parseErr := strconv.Atoi(writeTimeoutStr)
if parseErr == nil && parsedVal > 0 {
writeTimeout = time.Duration(parsedVal) * time.Second
}
}
s := &http.Server{ s := &http.Server{
Addr: ":8080", Addr: ":8080",
ReadTimeout: 5 * time.Second, ReadTimeout: readTimeout,
WriteTimeout: 5 * time.Second, WriteTimeout: writeTimeout,
MaxHeaderBytes: 1 << 20, MaxHeaderBytes: 1 << 20,
} }
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" { if r.Method == "POST" {
process := os.Getenv("fprocess")
parts := strings.Split(process, " ") parts := strings.Split(process, " ")
targetCmd := exec.Command(parts[0], parts[1:]...) targetCmd := exec.Command(parts[0], parts[1:]...)
@ -34,10 +64,13 @@ func main() {
writer.Write(res) writer.Write(res)
writer.Close() writer.Close()
out, err := targetCmd.Output() out, err := targetCmd.CombinedOutput()
targetCmd.CombinedOutput()
if err != nil { if err != nil {
log.Println(targetCmd, err) if writeDebug == true {
log.Println(targetCmd, err)
}
w.WriteHeader(500) w.WriteHeader(500)
response := bytes.NewBufferString(err.Error()) response := bytes.NewBufferString(err.Error())
w.Write(response.Bytes()) w.Write(response.Bytes())
@ -45,8 +78,10 @@ func main() {
} }
w.WriteHeader(200) w.WriteHeader(200)
// TODO: consider stdout to container as configurable via env-variable. if writeDebug == true {
os.Stdout.Write(out) os.Stdout.Write(out)
}
w.Write(out) w.Write(out)
} }
}) })