mirror of https://github.com/curusarn/resh
parent
2799a51376
commit
45c66208a1
@ -0,0 +1,28 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"io/ioutil" |
||||||
|
"log" |
||||||
|
"os/exec" |
||||||
|
"strconv" |
||||||
|
"strings" |
||||||
|
) |
||||||
|
|
||||||
|
func killDaemon(pidfile string) error { |
||||||
|
dat, err := ioutil.ReadFile(pidfile) |
||||||
|
if err != nil { |
||||||
|
log.Println("Reading pid file failed", err) |
||||||
|
} |
||||||
|
log.Print(string(dat)) |
||||||
|
pid, err := strconv.Atoi(strings.TrimSuffix(string(dat), "\n")) |
||||||
|
if err != nil { |
||||||
|
log.Fatal("Pidfile contents are malformed", err) |
||||||
|
} |
||||||
|
cmd := exec.Command("kill", "-s", "sigint", strconv.Itoa(pid)) |
||||||
|
err = cmd.Run() |
||||||
|
if err != nil { |
||||||
|
log.Printf("Command finished with error: %v", err) |
||||||
|
return err |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
||||||
@ -0,0 +1,39 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
"log" |
||||||
|
"net/http" |
||||||
|
"strconv" |
||||||
|
|
||||||
|
"github.com/curusarn/resh/pkg/msg" |
||||||
|
) |
||||||
|
|
||||||
|
func statusHandler(w http.ResponseWriter, r *http.Request) { |
||||||
|
log.Println("/status START") |
||||||
|
resp := msg.StatusResponse{ |
||||||
|
Status: true, |
||||||
|
Version: version, |
||||||
|
Commit: commit, |
||||||
|
} |
||||||
|
jsn, err := json.Marshal(&resp) |
||||||
|
if err != nil { |
||||||
|
log.Println("Encoding error:", err) |
||||||
|
log.Println("Response:", resp) |
||||||
|
return |
||||||
|
} |
||||||
|
w.Write(jsn) |
||||||
|
log.Println("/status END") |
||||||
|
} |
||||||
|
|
||||||
|
func isDaemonRunning(port int) (bool, error) { |
||||||
|
url := "http://localhost:" + strconv.Itoa(port) + "/status" |
||||||
|
resp, err := http.Get(url) |
||||||
|
if err != nil { |
||||||
|
log.Println("Error while checking daemon status - "+ |
||||||
|
"it's probably not running!", err) |
||||||
|
return false, err |
||||||
|
} |
||||||
|
defer resp.Body.Close() |
||||||
|
return true, nil |
||||||
|
} |
||||||
@ -0,0 +1,12 @@ |
|||||||
|
package httpclient |
||||||
|
|
||||||
|
import ( |
||||||
|
"net/http" |
||||||
|
"time" |
||||||
|
) |
||||||
|
|
||||||
|
func New() *http.Client { |
||||||
|
return &http.Client{ |
||||||
|
Timeout: 100 * time.Millisecond, |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue