forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
63 lines (52 loc) · 1.73 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
package api
import (
"encoding/json"
"github.com/asaskevich/govalidator"
"github.com/hellofresh/janus/pkg/proxy"
)
// Spec Holds an api definition and basic options
type Spec struct {
*Definition
}
// 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"`
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"`
}
// 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
}