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