Files
photoprism/internal/commands/status.go
Michael Mayer 1f4f65e988 Server: Add "force" and "mode" flags for sockets #4673 #4767 #4765 #4467
These changes allow you to force the re-creation of existing Unix domain
sockets and set the permissions of sockets after they have been created.

The flag or variable value for this must be formatted as follows:
--http-host="unix:/var/run/photoprism.sock?force=true&mode=660"

Signed-off-by: Michael Mayer <michael@photoprism.app>
2025-02-04 12:03:00 +01:00

76 lines
1.9 KiB
Go

package commands
import (
"context"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/tidwall/gjson"
"github.com/urfave/cli/v2"
"github.com/photoprism/photoprism/internal/config"
)
// StatusCommand configures the command name, flags, and action.
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)
// 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.
client := &http.Client{Timeout: 10 * time.Second}
// Connect to unix socket?
if unixSocket := conf.HttpSocket(); unixSocket != nil {
client.Transport = &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial(unixSocket.Scheme, unixSocket.Path)
},
}
}
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("cannot 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
}