2020-04-22 10:27:48 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-06 07:10:50 +02:00
|
|
|
"io"
|
2020-04-22 10:27:48 +02:00
|
|
|
"net/http"
|
2020-04-22 16:51:33 +02:00
|
|
|
"time"
|
2020-04-22 10:27:48 +02:00
|
|
|
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/urfave/cli"
|
2021-09-22 19:33:41 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-04-22 10:27:48 +02:00
|
|
|
)
|
|
|
|
|
2021-10-05 23:13:06 +02:00
|
|
|
// StatusCommand registers the status command.
|
2020-04-22 10:27:48 +02:00
|
|
|
var StatusCommand = cli.Command{
|
|
|
|
Name: "status",
|
2021-10-05 23:13:06 +02:00
|
|
|
Usage: "Checks if the web server is running",
|
2020-04-22 10:27:48 +02:00
|
|
|
Action: statusAction,
|
|
|
|
}
|
|
|
|
|
2021-10-05 23:13:06 +02:00
|
|
|
// statusAction checks if the web server is running.
|
2020-04-22 10:27:48 +02:00
|
|
|
func statusAction(ctx *cli.Context) error {
|
|
|
|
conf := config.NewConfig(ctx)
|
2022-03-27 21:37:11 +02:00
|
|
|
|
|
|
|
// Create new http.Client instance.
|
|
|
|
//
|
|
|
|
// NOTE: Timeout specifies a time limit for requests made by
|
|
|
|
// this Client. The timeout includes connection time, any
|
|
|
|
// redirects, and reading the response body. The timer remains
|
|
|
|
// running after Get, Head, Post, or Do return and will
|
|
|
|
// interrupt reading of the Response.Body.
|
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 {
|
2022-01-05 11:40:44 +01:00
|
|
|
return fmt.Errorf("cannot 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)
|
2021-10-06 07:10:50 +02:00
|
|
|
} else if body, err := io.ReadAll(resp.Body); err != nil {
|
2020-04-22 10:27:48 +02:00
|
|
|
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
|
|
|
|
}
|