Move starting of coredns server into separate function

This commit is contained in:
Eugene Bujak 2018-09-06 02:00:44 +03:00
parent e122d9138b
commit 33fbccf0ba
1 changed files with 24 additions and 13 deletions

View File

@ -79,24 +79,22 @@ func isRunning() bool {
} }
return false return false
} }
func handleStart(w http.ResponseWriter, r *http.Request) {
func startDNSServer() error {
if isRunning() { if isRunning() {
http.Error(w, fmt.Sprintf("Unable to start coreDNS: Already running"), 400) return fmt.Errorf("Unable to start coreDNS: Already running")
return
} }
err := writeCoreDNSConfig() err := writeCoreDNSConfig()
if err != nil { 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) log.Println(errortext)
http.Error(w, errortext, http.StatusInternalServerError) return errortext
return
} }
err = writeFilterFile() err = writeFilterFile()
if err != nil { 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) log.Println(errortext)
http.Error(w, errortext, http.StatusInternalServerError) return errortext
return
} }
binarypath := filepath.Join(config.ourBinaryDir, config.CoreDNS.binaryFile) binarypath := filepath.Join(config.ourBinaryDir, config.CoreDNS.binaryFile)
configpath := filepath.Join(config.ourBinaryDir, config.CoreDNS.coreFile) configpath := filepath.Join(config.ourBinaryDir, config.CoreDNS.coreFile)
@ -105,14 +103,27 @@ func handleStart(w http.ResponseWriter, r *http.Request) {
coreDNSCommand.Stderr = os.Stderr coreDNSCommand.Stderr = os.Stderr
err = coreDNSCommand.Start() err = coreDNSCommand.Start()
if err != nil { if err != nil {
errortext := fmt.Sprintf("Unable to start coreDNS: %s", err) errortext := fmt.Errorf("Unable to start coreDNS: %s", err)
log.Println(errortext) log.Println(errortext)
http.Error(w, errortext, http.StatusInternalServerError) return errortext
return
} }
log.Printf("coredns PID: %v\n", coreDNSCommand.Process.Pid) log.Printf("coredns PID: %v\n", coreDNSCommand.Process.Pid)
fmt.Fprintf(w, "OK, PID %d\n", coreDNSCommand.Process.Pid)
go childwaiter() 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() { func childwaiter() {