|
1
|
|
|
package config |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"os" |
|
5
|
|
|
"path/filepath" |
|
6
|
|
|
"strings" |
|
7
|
|
|
"time" |
|
8
|
|
|
|
|
9
|
|
|
"github.com/muonsoft/openapi-mock/internal/openapi/generator/data" |
|
10
|
|
|
"github.com/sirupsen/logrus" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
func Load(filename string) (*Configuration, error) { |
|
14
|
1 |
|
fileConfig, err := loadConfigurationFromFileOrCreateEmptyConfig(filename) |
|
15
|
1 |
|
if err != nil { |
|
16
|
1 |
|
return nil, err |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
1 |
|
updateConfigFromEnvironment(fileConfig) |
|
20
|
1 |
|
autocorrectValues(filename, fileConfig) |
|
21
|
|
|
|
|
22
|
1 |
|
err = fileConfig.Validate() |
|
23
|
1 |
|
if err != nil { |
|
24
|
1 |
|
return nil, &InvalidConfigurationError{ValidationError: err} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
return createApplicationConfiguration(fileConfig), nil |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
func loadConfigurationFromFileOrCreateEmptyConfig(filename string) (*fileConfiguration, error) { |
|
31
|
1 |
|
fileConfig := &fileConfiguration{} |
|
32
|
1 |
|
filename = detectDefaultConfigurationFile(filename) |
|
33
|
|
|
|
|
34
|
1 |
|
if filename != "" { |
|
35
|
1 |
|
var err error |
|
36
|
1 |
|
fileConfig, err = loadFileConfiguration(filename) |
|
37
|
1 |
|
if err != nil { |
|
38
|
1 |
|
return nil, err |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
return fileConfig, nil |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
func detectDefaultConfigurationFile(filename string) string { |
|
46
|
1 |
|
if filename == "" { |
|
47
|
1 |
|
defaultFilenames := []string{"openapi-mock.yaml", "openapi-mock.yml", "openapi-mock.json"} |
|
48
|
1 |
|
for _, defaultFilename := range defaultFilenames { |
|
49
|
1 |
|
if _, err := os.Stat(defaultFilename); err == nil { |
|
50
|
|
|
filename = defaultFilename |
|
51
|
|
|
break |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return filename |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
func autocorrectValues(filename string, fileConfig *fileConfiguration) { |
|
60
|
1 |
|
fileConfig.Application.LogLevel = defaultOnEmptyString(fileConfig.Application.LogLevel, DefaultLogLevel.String()) |
|
61
|
1 |
|
fileConfig.Application.LogFormat = defaultOnEmptyString(fileConfig.Application.LogFormat, "tty") |
|
62
|
1 |
|
fileConfig.Generation.UseExamples = defaultOnEmptyString(fileConfig.Generation.UseExamples, "no") |
|
63
|
|
|
|
|
64
|
1 |
|
if fileConfig.Application.Debug { |
|
65
|
1 |
|
fileConfig.Application.LogLevel = logrus.TraceLevel.String() |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
if fileConfig.HTTP.ResponseTimeout <= 0 { |
|
69
|
1 |
|
fileConfig.HTTP.ResponseTimeout = DefaultResponseTimeout.Seconds() |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
if specificationURLIsRelativeFilename(filename, fileConfig) { |
|
73
|
1 |
|
fileConfig.OpenAPI.SpecificationURL = filepath.Dir(filename) + "/" + fileConfig.OpenAPI.SpecificationURL |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
func specificationURLIsRelativeFilename(filename string, fileConfig *fileConfiguration) bool { |
|
78
|
1 |
|
return filename != "" && |
|
79
|
|
|
fileConfig.OpenAPI.SpecificationURL != "" && |
|
80
|
|
|
!fileConfig.OpenAPI.urlFromEnv && |
|
81
|
|
|
strings.HasPrefix(fileConfig.OpenAPI.SpecificationURL, "./") |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
func createApplicationConfiguration(fileConfig *fileConfiguration) *Configuration { |
|
85
|
1 |
|
return &Configuration{ |
|
86
|
|
|
SpecificationURL: fileConfig.OpenAPI.SpecificationURL, |
|
87
|
|
|
|
|
88
|
|
|
CORSEnabled: fileConfig.HTTP.CORSEnabled, |
|
89
|
|
|
Port: defaultOnNilUint16(fileConfig.HTTP.Port, DefaultPort), |
|
90
|
|
|
ResponseTimeout: time.Duration(fileConfig.HTTP.ResponseTimeout * float64(time.Second)), |
|
91
|
|
|
|
|
92
|
|
|
Debug: fileConfig.Application.Debug, |
|
93
|
|
|
LogFormat: fileConfig.Application.LogFormat, |
|
94
|
|
|
LogLevel: parseLogLevel(fileConfig.Application.LogLevel), |
|
95
|
|
|
|
|
96
|
|
|
UseExamples: parseUseExamples(fileConfig.Generation.UseExamples), |
|
97
|
|
|
NullProbability: defaultOnNilFloat(fileConfig.Generation.NullProbability, DefaultNullProbability), |
|
98
|
|
|
DefaultMinInt: defaultOnNilInt64(fileConfig.Generation.DefaultMinInt, 0), |
|
99
|
|
|
DefaultMaxInt: defaultOnNilInt64(fileConfig.Generation.DefaultMaxInt, DefaultMaxInt), |
|
100
|
|
|
DefaultMinFloat: defaultOnNilFloat(fileConfig.Generation.DefaultMinFloat, DefaultMinFloat), |
|
101
|
|
|
DefaultMaxFloat: defaultOnNilFloat(fileConfig.Generation.DefaultMaxFloat, DefaultMaxFloat), |
|
102
|
|
|
SuppressErrors: fileConfig.Generation.SuppressErrors, |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
func parseLogLevel(rawLogLevel string) logrus.Level { |
|
107
|
1 |
|
var logLevel logrus.Level |
|
108
|
1 |
|
var err error |
|
109
|
|
|
|
|
110
|
1 |
|
if rawLogLevel == "" { |
|
111
|
|
|
logLevel = DefaultLogLevel |
|
112
|
|
|
} else { |
|
113
|
1 |
|
logLevel, err = logrus.ParseLevel(rawLogLevel) |
|
114
|
|
|
|
|
115
|
1 |
|
if err != nil { |
|
116
|
|
|
panic(err) |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
return logLevel |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
func parseUseExamples(useExamples string) data.UseExamplesEnum { |
|
124
|
1 |
|
if useExamples == "if_present" { |
|
125
|
1 |
|
return data.IfPresent |
|
126
|
|
|
} |
|
127
|
1 |
|
if useExamples == "exclusively" { |
|
128
|
|
|
return data.Exclusively |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
return data.No |
|
132
|
|
|
} |
|
133
|
|
|
|