1
|
|
|
package config |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"io/ioutil" |
6
|
|
|
"strings" |
7
|
|
|
|
8
|
|
|
"github.com/muonsoft/validation" |
9
|
|
|
"github.com/muonsoft/validation/it" |
10
|
|
|
"github.com/muonsoft/validation/validator" |
11
|
|
|
"gopkg.in/yaml.v3" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
type fileConfiguration struct { |
15
|
|
|
OpenAPI openapiConfiguration `json:"openapi" yaml:"openapi"` |
16
|
|
|
HTTP httpConfiguration `json:"http" yaml:"http"` |
17
|
|
|
Application applicationConfiguration `json:"application" yaml:"application"` |
18
|
|
|
Generation generationConfiguration `json:"generation" yaml:"generation"` |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
type openapiConfiguration struct { |
22
|
|
|
SpecificationURL string `json:"specification_url" yaml:"specification_url"` |
23
|
|
|
urlFromEnv bool |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
type httpConfiguration struct { |
27
|
|
|
Port *uint16 `json:"port" yaml:"port"` |
28
|
|
|
CORSEnabled bool `json:"cors_enabled" yaml:"cors_enabled"` |
29
|
|
|
ResponseTimeout float64 `json:"response_timeout" yaml:"response_timeout"` |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
type applicationConfiguration struct { |
33
|
|
|
Debug bool `json:"debug" yaml:"debug"` |
34
|
|
|
LogFormat string `json:"log_format" yaml:"log_format"` |
35
|
|
|
LogLevel string `json:"log_level" yaml:"log_level"` |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
type generationConfiguration struct { |
39
|
|
|
DefaultMinFloat *float64 `json:"default_min_float" yaml:"default_min_float"` |
40
|
|
|
DefaultMaxFloat *float64 `json:"default_max_float" yaml:"default_max_float"` |
41
|
|
|
DefaultMinInt *int64 `json:"default_min_int" yaml:"default_min_int"` |
42
|
|
|
DefaultMaxInt *int64 `json:"default_max_int" yaml:"default_max_int"` |
43
|
|
|
NullProbability *float64 `json:"null_probability" yaml:"null_probability"` |
44
|
|
|
SuppressErrors bool `json:"suppress_errors" yaml:"suppress_errors"` |
45
|
|
|
UseExamples string `json:"use_examples" yaml:"use_examples"` |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
func loadFileConfiguration(filename string) (*fileConfiguration, error) { |
49
|
1 |
|
data, err := ioutil.ReadFile(filename) |
50
|
1 |
|
if err != nil { |
51
|
1 |
|
return nil, &LoadingFailedError{Previous: err} |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
var fileConfig fileConfiguration |
55
|
1 |
|
err = yaml.Unmarshal(data, &fileConfig) |
56
|
1 |
|
if err != nil { |
57
|
1 |
|
return nil, &LoadingFailedError{Previous: err} |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return &fileConfig, nil |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
var logFormats = []string{"tty", "json"} |
64
|
|
|
var logLevels = []string{"panic", "fatal", "error", "warn", "warning", "info", "debug", "trace"} |
65
|
|
|
var useExampleOptions = []string{"no", "if_present", "exclusively"} |
66
|
|
|
|
67
|
|
|
var invalidLogFormat = fmt.Sprintf("must be one of: %s", strings.Join(logFormats, ", ")) |
68
|
|
|
var invalidLogLevel = fmt.Sprintf("must be one of: %s", strings.Join(logLevels, ", ")) |
69
|
|
|
var invalidUseExample = fmt.Sprintf("must be one of: %s", strings.Join(useExampleOptions, ", ")) |
70
|
|
|
|
71
|
|
|
func (config *fileConfiguration) Validate() error { |
72
|
1 |
|
return validator.Validate( |
73
|
|
|
validation.StringProperty( |
74
|
|
|
"application.log_format", |
75
|
|
|
&config.Application.LogFormat, |
76
|
|
|
it.IsOneOfStrings(logFormats...).Message(invalidLogFormat), |
77
|
|
|
), |
78
|
|
|
validation.StringProperty( |
79
|
|
|
"application.log_level", |
80
|
|
|
&config.Application.LogLevel, |
81
|
|
|
it.IsOneOfStrings(logLevels...).Message(invalidLogLevel), |
82
|
|
|
), |
83
|
|
|
validation.StringProperty( |
84
|
|
|
"generation.use_examples", |
85
|
|
|
&config.Generation.UseExamples, |
86
|
|
|
it.IsOneOfStrings(useExampleOptions...).Message(invalidUseExample), |
87
|
|
|
), |
88
|
|
|
validation.NumberProperty( |
89
|
|
|
"http.port", |
90
|
|
|
config.HTTP.Port, |
91
|
|
|
it.IsBetweenIntegers(1, 65535). |
92
|
|
|
When(config.HTTP.Port != nil). |
93
|
|
|
Message("value should be between {{ min }} and {{ max }} if present"), |
94
|
|
|
), |
95
|
|
|
) |
96
|
|
|
} |
97
|
|
|
|