github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/cmd/status.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  	"os"
     8  
     9  	"github.com/cozy/cozy-stack/pkg/config/config"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  // statusCmd represents the status command
    14  var statusCmd = &cobra.Command{
    15  	Use:   "status",
    16  	Short: "Check if the HTTP server is running",
    17  	Long:  `Check if the HTTP server has been started and answer 200 for /status.`,
    18  	RunE: func(cmd *cobra.Command, args []string) error {
    19  		url := &url.URL{
    20  			Scheme: "http",
    21  			Host:   config.ServerAddr(),
    22  			Path:   "status",
    23  		}
    24  		resp, err := http.Get(url.String())
    25  		if err != nil {
    26  			fmt.Println("Error the HTTP server is not running:", err)
    27  			os.Exit(1)
    28  		}
    29  		defer resp.Body.Close()
    30  		if resp.StatusCode != 200 {
    31  			fmt.Println("Error, unexpected HTTP status code:", resp.Status)
    32  			os.Exit(1)
    33  		}
    34  
    35  		fmt.Println("OK, the HTTP server is ready.")
    36  		return nil
    37  	},
    38  }
    39  
    40  func init() {
    41  	RootCmd.AddCommand(statusCmd)
    42  }