github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/status/status.go (about)

     1  package status
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/henvic/wedeploycli/apihelper"
     7  	"github.com/henvic/wedeploycli/config"
     8  )
     9  
    10  const (
    11  	// Up status
    12  	Up = "up"
    13  
    14  	// Down status
    15  	Down = "down"
    16  )
    17  
    18  // Client for the services
    19  type Client struct {
    20  	*apihelper.Client
    21  }
    22  
    23  // New Client
    24  func New(wectx config.Context) *Client {
    25  	return &Client{
    26  		apihelper.New(wectx),
    27  	}
    28  }
    29  
    30  // Domains of the infrastructure
    31  type Domains struct {
    32  	Infrastructure string `json:"infrastructure"`
    33  	Service        string `json:"service"`
    34  }
    35  
    36  // Status of the Backend
    37  type Status struct {
    38  	Status  string  `json:"status"`
    39  	Version string  `json:"version"`
    40  	Domains Domains `json:"domains"`
    41  }
    42  
    43  // IsUp is returned if the infrastructure is up
    44  func (c *Client) IsUp(ctx context.Context) bool {
    45  	var s, _ = c.get(ctx)
    46  	return s.Status == Up
    47  }
    48  
    49  // UnsafeGet is a unsafe getter for the status
    50  // See get() method below to understand why.
    51  // It is exported here to use for domains.
    52  func (c *Client) UnsafeGet(ctx context.Context) (s Status, err error) {
    53  	return c.get(ctx)
    54  }
    55  
    56  func (c *Client) get(ctx context.Context) (s Status, err error) {
    57  	// don't expose this method on the public API because
    58  	// the current endpoint in unreliable, returning 503
    59  	// with incompatible body responses by design
    60  	// whenever status = "down", while the error structure
    61  	// expects status to be a number and to have other info,
    62  	// therefore I am not exposing it as it might cause problems later on
    63  	var request = c.Client.URL(ctx, "/")
    64  	if err = apihelper.Validate(request, request.Get()); err != nil {
    65  		return s, err
    66  	}
    67  
    68  	err = apihelper.DecodeJSON(request, &s)
    69  	return s, err
    70  }