github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/api/api.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  
     7  	"github.com/asaskevich/govalidator"
     8  	"github.com/hellofresh/janus/pkg/proxy"
     9  )
    10  
    11  // Plugin represents the plugins for an API
    12  type Plugin struct {
    13  	Name    string                 `bson:"name" json:"name"`
    14  	Enabled bool                   `bson:"enabled" json:"enabled"`
    15  	Config  map[string]interface{} `bson:"config" json:"config"`
    16  }
    17  
    18  // Definition represents an API that you want to proxy
    19  type Definition struct {
    20  	Name        string            `bson:"name" json:"name" valid:"required~name is required,matches(^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$)~name cannot contain non-URL friendly characters"`
    21  	Active      bool              `bson:"active" json:"active"`
    22  	Proxy       *proxy.Definition `bson:"proxy" json:"proxy" valid:"required"`
    23  	Plugins     []Plugin          `bson:"plugins" json:"plugins"`
    24  	HealthCheck HealthCheck       `bson:"health_check" json:"health_check"`
    25  }
    26  
    27  // HealthCheck represents the health check configs
    28  type HealthCheck struct {
    29  	URL     string `bson:"url" json:"url" valid:"url"`
    30  	Timeout int    `bson:"timeout" json:"timeout"`
    31  }
    32  
    33  // Configuration represents all the api definitions
    34  type Configuration struct {
    35  	Definitions []*Definition
    36  }
    37  
    38  // EqualsTo compares two configurations and determines if they are the same
    39  func (c *Configuration) EqualsTo(c1 *Configuration) bool {
    40  	return reflect.DeepEqual(c, c1)
    41  }
    42  
    43  // ConfigurationChanged is the message that is sent when a database configuration has changed
    44  type ConfigurationChanged struct {
    45  	Configurations *Configuration
    46  }
    47  
    48  // ConfigurationOperation is the available operations that a configuration can have
    49  type ConfigurationOperation int
    50  
    51  // ConfigurationMessage is used to notify listeners about something that happened with a configuration
    52  type ConfigurationMessage struct {
    53  	Operation     ConfigurationOperation
    54  	Configuration *Definition
    55  }
    56  
    57  const (
    58  	// RemovedOperation means a definition was removed
    59  	RemovedOperation ConfigurationOperation = iota
    60  	// UpdatedOperation means a definition was updated
    61  	UpdatedOperation
    62  	// AddedOperation means a definition was added
    63  	AddedOperation
    64  )
    65  
    66  // NewDefinition creates a new API Definition with default values
    67  func NewDefinition() *Definition {
    68  	return &Definition{
    69  		Active:  true,
    70  		Plugins: make([]Plugin, 0),
    71  		Proxy:   proxy.NewDefinition(),
    72  	}
    73  }
    74  
    75  // Validate validates proxy data
    76  func (d *Definition) Validate() (bool, error) {
    77  	return govalidator.ValidateStruct(d)
    78  }
    79  
    80  // UnmarshalJSON api.Definition JSON.Unmarshaller implementation
    81  func (d *Definition) UnmarshalJSON(b []byte) error {
    82  	// Aliasing Definition to avoid recursive call of this method
    83  	type definitionAlias Definition
    84  	defAlias := definitionAlias(*NewDefinition())
    85  
    86  	if err := json.Unmarshal(b, &defAlias); err != nil {
    87  		return err
    88  	}
    89  
    90  	*d = Definition(defAlias)
    91  	return nil
    92  }