github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/config/relay.go (about)

     1  package config
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  )
     6  
     7  // Relay config.
     8  type Relay struct {
     9  	// Command run to start your server.
    10  	Command string `json:"command"`
    11  
    12  	// Timeout in seconds to wait for a response.
    13  	Timeout int `json:"timeout"`
    14  
    15  	// ListenTimeout in seconds when waiting for the app to bind to PORT.
    16  	ListenTimeout int `json:"listen_timeout"`
    17  }
    18  
    19  // Default implementation.
    20  func (r *Relay) Default() error {
    21  	if r.Command == "" {
    22  		r.Command = "./server"
    23  	}
    24  
    25  	if r.Timeout == 0 {
    26  		r.Timeout = 15
    27  	}
    28  
    29  	if r.ListenTimeout == 0 {
    30  		r.ListenTimeout = 15
    31  	}
    32  
    33  	return nil
    34  }
    35  
    36  // Validate will try to perform sanity checks for this Relay configuration.
    37  func (r *Relay) Validate() error {
    38  	if r.Command == "" {
    39  		err := errors.New("should not be empty")
    40  		return errors.Wrap(err, ".command")
    41  	}
    42  
    43  	if r.ListenTimeout <= 0 {
    44  		err := errors.New("should be greater than 0")
    45  		return errors.Wrap(err, ".listen_timeout")
    46  	}
    47  
    48  	if r.ListenTimeout > 25 {
    49  		err := errors.New("should be <= 25")
    50  		return errors.Wrap(err, ".listen_timeout")
    51  	}
    52  
    53  	if r.Timeout > 25 {
    54  		err := errors.New("should be <= 25")
    55  		return errors.Wrap(err, ".timeout")
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  // Override config.
    62  func (r *Relay) Override(c *Config) {
    63  	if r.Command != "" {
    64  		c.Proxy.Command = r.Command
    65  	}
    66  }