mirror of https://github.com/curusarn/resh
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.1 KiB
49 lines
1.1 KiB
package status
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/curusarn/resh/internal/httpclient"
|
|
"github.com/curusarn/resh/internal/msg"
|
|
)
|
|
|
|
func get(port int) (*http.Response, error) {
|
|
url := "http://localhost:" + strconv.Itoa(port) + "/status"
|
|
client := httpclient.New()
|
|
resp, err := client.Get(url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error while GET'ing daemon /status: %w", err)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func IsDaemonRunning(port int) (bool, error) {
|
|
resp, err := get(port)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer resp.Body.Close()
|
|
return true, nil
|
|
}
|
|
|
|
func GetDaemonStatus(port int) (*msg.StatusResponse, error) {
|
|
resp, err := get(port)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
jsn, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error while reading 'daemon /status' response: %w", err)
|
|
}
|
|
var msgResp msg.StatusResponse
|
|
err = json.Unmarshal(jsn, &msgResp)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error while decoding 'daemon /status' response: %w", err)
|
|
}
|
|
return &msgResp, nil
|
|
}
|
|
|