forked from grafana/grafana-plugin-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
85 lines (72 loc) · 2.06 KB
/
types.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
package config
import (
"encoding/json"
"os"
)
// Config is the configuration for the proxy.
type Config struct {
// Storage is the storage configuration(s).
Storage []*StorageConfig `json:"storage"`
// Address is the address for the proxy server to listen on.
Address string `json:"address"`
// Hosts is a list of hosts that are allowed to be recorded and played back.
Hosts []string `json:"hosts"`
// CAConfig is the paths to the Certificate Authority key pair.
CAConfig CAConfig `json:"ca_keypair"`
}
// StorageConfig defines the storage configuration for the proxy.
type StorageConfig struct {
// Type is the type of storage.
Type StorageType `json:"type"`
// Path is the path to the storage file (valid for HAR and OpenAPI).
Path string `json:"path"`
}
// CAConfig is the paths to the Certificate Authority key pair.
type CAConfig struct {
// Cert is the PEM encoded certificate.
Cert string `json:"cert"`
// PrivateKey is the PEM encoded private key.
PrivateKey string `json:"private_key"`
}
// StorageType defines the type of storage used by the proxy.
type StorageType string
const (
// StorageTypeHAR is the HAR file storage type.
StorageTypeHAR StorageType = "har"
// StorageTypeOpenAPI is the OpenAPI file storage type (JSON or YAML).
StorageTypeOpenAPI StorageType = "openapi"
)
// LoadConfig loads the configuration from a JSON file path.
func LoadConfig(path string) (*Config, error) {
if path == "" {
path = "proxy.json"
}
raw, err := os.ReadFile(path)
if err != nil {
return &Config{
Storage: []*StorageConfig{{
Type: StorageTypeHAR,
Path: "fixtures/e2e.har",
}},
Address: "127.0.0.1:9999",
Hosts: make([]string, 0),
}, nil
}
var cfg Config
if err := json.Unmarshal(raw, &cfg); err != nil {
return nil, err
}
if cfg.Address == "" {
cfg.Address = "127.0.0.1:9999"
}
if cfg.Storage == nil || len(cfg.Storage) == 0 {
cfg.Storage = []*StorageConfig{{
Type: StorageTypeHAR,
Path: "fixtures/e2e.har",
}}
}
if cfg.Hosts == nil || len(cfg.Hosts) == 0 {
cfg.Hosts = make([]string, 0)
}
return &cfg, nil
}