2020-04-22 10:27:48 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-06 07:10:50 +02:00
|
|
|
"io"
|
2023-08-14 10:00:35 +02:00
|
|
|
"net"
|
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
|
|
|
)
|
|
|
|
|
2023-02-14 11:37:22 +01:00
|
|
|
// StatusCommand configures the command name, flags, and action.
|
2020-04-22 10:27:48 +02:00
|
|
|
var StatusCommand = cli.Command{
|
|
|
|
Name: "status",
|
2022-10-19 05:09:09 +02:00
|
|
|
Usage: "Checks if the Web server is running",
|
2020-04-22 10:27:48 +02:00
|
|
|
Action: statusAction,
|
|
|
|
}
|
|
|
|
|
2023-09-24 18:05:49 +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
|
|
|
|
2023-08-14 10:43:05 +02:00
|
|
|
// Connect to unix socket?
|
|
|
|
if unixSocket := conf.HttpSocket(); unixSocket != "" {
|
2023-08-14 10:00:35 +02:00
|
|
|
client.Transport = &http.Transport{
|
|
|
|
Dial: func(network, addr string) (net.Conn, error) {
|
2023-08-14 10:43:05 +02:00
|
|
|
return net.Dial("unix", unixSocket)
|
2023-08-14 10:00:35 +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
|
|
|
|
}
|