From 33fbccf0badee8a4c5f790d850d85e161536779f Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Thu, 6 Sep 2018 02:00:44 +0300 Subject: [PATCH] Move starting of coredns server into separate function --- control.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/control.go b/control.go index 48e71bf9..e3d11119 100644 --- a/control.go +++ b/control.go @@ -79,24 +79,22 @@ func isRunning() bool { } return false } -func handleStart(w http.ResponseWriter, r *http.Request) { + +func startDNSServer() error { if isRunning() { - http.Error(w, fmt.Sprintf("Unable to start coreDNS: Already running"), 400) - return + return fmt.Errorf("Unable to start coreDNS: Already running") } err := writeCoreDNSConfig() if err != nil { - errortext := fmt.Sprintf("Unable to write coredns config: %s", err) + errortext := fmt.Errorf("Unable to write coredns config: %s", err) log.Println(errortext) - http.Error(w, errortext, http.StatusInternalServerError) - return + return errortext } err = writeFilterFile() if err != nil { - errortext := fmt.Sprintf("Couldn't write filter file: %s", err) + errortext := fmt.Errorf("Couldn't write filter file: %s", err) log.Println(errortext) - http.Error(w, errortext, http.StatusInternalServerError) - return + return errortext } binarypath := filepath.Join(config.ourBinaryDir, config.CoreDNS.binaryFile) configpath := filepath.Join(config.ourBinaryDir, config.CoreDNS.coreFile) @@ -105,14 +103,27 @@ func handleStart(w http.ResponseWriter, r *http.Request) { coreDNSCommand.Stderr = os.Stderr err = coreDNSCommand.Start() if err != nil { - errortext := fmt.Sprintf("Unable to start coreDNS: %s", err) + errortext := fmt.Errorf("Unable to start coreDNS: %s", err) log.Println(errortext) - http.Error(w, errortext, http.StatusInternalServerError) - return + return errortext } log.Printf("coredns PID: %v\n", coreDNSCommand.Process.Pid) - fmt.Fprintf(w, "OK, PID %d\n", coreDNSCommand.Process.Pid) go childwaiter() + return nil +} + +func handleStart(w http.ResponseWriter, r *http.Request) { + if isRunning() { + http.Error(w, fmt.Sprintf("Unable to start coreDNS: Already running"), 400) + return + } + err := startDNSServer() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + fmt.Fprintf(w, "OK, PID %d\n", coreDNSCommand.Process.Pid) } func childwaiter() {