photoprism/internal/commands/status.go
Eng Zer Jun 44f7700c0c
Enable module graph pruning and deprecate io/ioutil (#1600)
* Backend: Enable Go module graph pruning and lazy module loading

This commit applies the changes by running `go mod tidy -go=1.17` to
enable module graph pruning and lazy module loading supported by Go 1.17
or higher.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* Backend: Move from io/ioutil to io and os package

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-10-06 07:10:50 +02:00

56 lines
1.2 KiB
Go

package commands
import (
"fmt"
"io"
"net/http"
"time"
"github.com/tidwall/gjson"
"github.com/urfave/cli"
"github.com/photoprism/photoprism/internal/config"
)
// StatusCommand registers the status command.
var StatusCommand = cli.Command{
Name: "status",
Usage: "Checks if the web server is running",
Action: statusAction,
}
// statusAction checks if the web server is running.
func statusAction(ctx *cli.Context) error {
conf := config.NewConfig(ctx)
client := &http.Client{Timeout: 10 * time.Second}
url := fmt.Sprintf("http://%s:%d/api/v1/status", conf.HttpHost(), conf.HttpPort())
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
var status string
if resp, err := client.Do(req); err != nil {
return fmt.Errorf("can't connect to %s:%d", conf.HttpHost(), conf.HttpPort())
} else if resp.StatusCode != 200 {
return fmt.Errorf("server running at %s:%d, bad status %d\n", conf.HttpHost(), conf.HttpPort(), resp.StatusCode)
} else if body, err := io.ReadAll(resp.Body); err != nil {
return err
} else {
status = string(body)
}
message := gjson.Get(status, "status").String()
if message != "" {
fmt.Println(message)
} else {
fmt.Println("unknown")
}
return nil
}