2020-04-22 10:27:48 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-04-22 16:51:33 +02:00
|
|
|
"time"
|
2020-04-22 10:27:48 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StatusCommand performs a server health check.
|
|
|
|
var StatusCommand = cli.Command{
|
|
|
|
Name: "status",
|
|
|
|
Usage: "Performs a server health check",
|
|
|
|
Action: statusAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
// statusAction shows the server health status
|
|
|
|
func statusAction(ctx *cli.Context) error {
|
|
|
|
conf := config.NewConfig(ctx)
|
2020-04-22 16:51:33 +02:00
|
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
2020-04-22 10:27:48 +02:00
|
|
|
|
2020-12-18 09:11:42 +01:00
|
|
|
url := fmt.Sprintf("http://%s:%d/api/v1/status", conf.HttpHost(), conf.HttpPort())
|
2020-04-22 10:27:48 +02:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var status string
|
|
|
|
|
|
|
|
if resp, err := client.Do(req); err != nil {
|
2020-12-18 09:11:42 +01:00
|
|
|
return fmt.Errorf("can't connect to %s:%d", conf.HttpHost(), conf.HttpPort())
|
2020-04-22 10:27:48 +02:00
|
|
|
} else if resp.StatusCode != 200 {
|
2020-12-18 09:11:42 +01:00
|
|
|
return fmt.Errorf("server running at %s:%d, bad status %d\n", conf.HttpHost(), conf.HttpPort(), resp.StatusCode)
|
2020-04-22 10:27:48 +02:00
|
|
|
} else if body, err := ioutil.ReadAll(resp.Body); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
status = string(body)
|
|
|
|
}
|
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
message := gjson.Get(status, "status").String()
|
2020-04-22 10:27:48 +02:00
|
|
|
|
2020-10-08 08:52:03 +02:00
|
|
|
if message != "" {
|
|
|
|
fmt.Println(message)
|
|
|
|
} else {
|
|
|
|
fmt.Println("unknown")
|
|
|
|
}
|
2020-04-22 10:27:48 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|