-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstatic_config.go
More file actions
319 lines (278 loc) · 7.84 KB
/
static_config.go
File metadata and controls
319 lines (278 loc) · 7.84 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package static
import (
"bytes"
"encoding/json"
"fmt"
"mokapi/config/tls"
"net/url"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
type Config struct {
Log MokApiLog `json:"log" yaml:"log"`
ConfigFile string `json:"-" yaml:"-" flag:"config-file"`
Providers Providers `json:"providers" yaml:"providers"`
Api Api `json:"api" yaml:"api"`
Health Health `json:"health" yaml:"health"`
Mcp Mcp `json:"mcp" yaml:"mcp"`
RootCaCert tls.FileOrContent `json:"rootCaCert" yaml:"rootCaCert" name:"root-ca-cert"`
RootCaKey tls.FileOrContent `json:"rootCaKey" yaml:"rootCaKey" name:"root-ca-cert"`
Configs Configs `json:"configs" yaml:"configs" explode:"config"`
Help bool `json:"-" yaml:"-" aliases:"h"`
GenerateSkeleton interface{} `json:"-" yaml:"-" flag:"generate-cli-skeleton"`
Features []string `json:"-" yaml:"-" explode:"feature"`
Version bool `json:"-" yaml:"-" aliases:"v"`
Event Event `json:"event" yaml:"event"`
Certificates CertificateStore `json:"certificates" yaml:"certificates"`
DataGen DataGen `json:"data-gen" yaml:"data-gen" name:"data-gen"`
Args []string `json:"args" yaml:"-" aliases:"args"` // positional arguments
}
func NewConfig() *Config {
cfg := &Config{}
cfg.Log = MokApiLog{Level: "info", Format: "text"}
cfg.Api.Port = 8080
cfg.Api.Dashboard = true
cfg.Api.Search.Enabled = true
cfg.Health.Enabled = true
cfg.Health.Port = 8080
cfg.Health.Path = "/health"
cfg.Mcp = Mcp{
Server: McpServer{
Enabled: false,
Port: 8080,
Path: "/mcp",
},
}
cfg.Providers.File.SkipPrefix = []string{"_"}
cfg.Event.Store = map[string]Store{"default": {Size: 100}}
cfg.DataGen.OptionalProperties = "0.85"
return cfg
}
type MokApiLog struct {
Level string
Format string
}
type Providers struct {
File FileProvider
Git GitProvider
Http HttpProvider
Npm NpmProvider
}
type Api struct {
Port int
Path string
Base string
Dashboard bool
Search Search
}
type Search struct {
Enabled bool
IndexPath string `yaml:"indexPath" json:"indexPath" flag:"index-path"`
InMemory bool `yaml:"inMemory" json:"inMemory" flag:"in-memory"`
}
type FileProvider struct {
Filenames []string `aliases:"filename" explode:"filename"`
Directories []FileConfig `aliases:"directory" explode:"directory"`
SkipPrefix []string `yaml:"skipPrefix" json:"skipPrefix" flag:"skip-prefix"`
Include []string
Exclude []string
}
type FileConfig struct {
Path string
Include []string
Exclude []string
}
type GitProvider struct {
Urls []string `explode:"url"`
PullInterval string `yaml:"pullInterval" json:"pullInterval" name:"pull-interval"`
TempDir string `yaml:"tempDir" json:"tempDir" name:"temp-dir"`
Repositories []GitRepo `explode:"repository"`
}
type GitRepo struct {
Url string
// Specifies an allow list of files to include in mokapi
Files []string `explode:"file"`
// Specifies an array of filenames pr pattern to include in mokapi
Include []string
Auth *GitAuth
PullInterval string `yaml:"pullInterval" name:"pull-interval"`
}
type GitAuth struct {
GitHub *GitHubAuth `yaml:"gitHub,github"`
}
type GitHubAuth struct {
AppId int64 `yaml:"appId"`
InstallationId int64 `yaml:"installationId"`
PrivateKey tls.FileOrContent `yaml:"privateKey"`
}
type HttpProvider struct {
Urls []string `explode:"url"`
PollInterval string `yaml:"pollInterval" name:"poll-interval"`
PollTimeout string `yaml:"pollTimeout" name:"poll-timeout"`
Proxy string
TlsSkipVerify bool `yaml:"tlsSkipVerify" flag:"tls-skip-verify"`
Ca tls.FileOrContent `yaml:"ca"`
}
type NpmProvider struct {
GlobalFolders []string `yaml:"globalFolders" name:"global-folders" explode:"global-folder"`
Packages []NpmPackage `explode:"package"`
}
type NpmPackage struct {
Name string
// Specifies an allow list of files to include in mokapi
Files []string `explode:"file"`
// Specifies an array of filenames pr pattern to include in mokapi
Include []string
}
type Event struct {
Store map[string]Store
}
type Store struct {
Size int64
}
type Configs []string
type CertificateStore struct {
Static []Certificate
}
type Certificate struct {
Cert tls.FileOrContent `yaml:"cert" json:"cert"`
Key tls.FileOrContent `yaml:"key" json:"key"`
}
type DataGen struct {
OptionalProperties string `yaml:"optionalProperties" json:"optionalProperties" name:"optional-properties"`
}
func (c *Configs) UnmarshalJSON(b []byte) error {
dec := json.NewDecoder(bytes.NewReader(b))
token, err := dec.Token()
if err != nil {
return err
}
if delim, ok := token.(json.Delim); ok && delim != '[' {
return fmt.Errorf("unexpected token %s; expected '['", token)
}
for {
msg := json.RawMessage{}
err = dec.Decode(&msg)
if err != nil {
return err
}
*c = append(*c, string(msg))
token, err = dec.Token()
if err != nil {
return err
}
if delim, ok := token.(json.Delim); ok && delim == ']' {
return nil
}
}
}
func (c *Config) Parse() error {
for _, arg := range c.Args {
u, err := url.Parse(arg)
if err != nil {
return err
}
switch u.Scheme {
case "http", "https":
c.Providers.Http.Urls = append(c.Providers.Http.Urls, u.String())
case "git+https", "git+http":
c.Providers.Git.Urls = append(c.Providers.Git.Urls, strings.TrimPrefix(u.String(), "git+"))
case "npm":
c.Providers.Npm.Packages = append(c.Providers.Npm.Packages, NpmPackage{Name: u.String()})
case "":
c.Providers.File.Filenames = append(c.Providers.File.Filenames, arg)
default:
if u.Opaque != "" {
c.Providers.File.Filenames = append(c.Providers.File.Filenames, arg)
} else {
return fmt.Errorf("positional argument is not supported: %v", arg)
}
}
}
return nil
}
func (d *DataGen) OptionalPropertiesProbability() float64 {
defaultValue := 0.85
if d.OptionalProperties == "" {
return defaultValue
}
switch strings.ToLower(d.OptionalProperties) {
case "always":
return 1.0
case "never":
return 0.0
case "often":
return 0.85
case "sometimes":
return 0.5
case "rarely":
return 0.15
}
f, err := strconv.ParseFloat(d.OptionalProperties, 64)
if err != nil {
log.Warnf("Invalid data generator optional properties value '%v': using %v", d.OptionalProperties, defaultValue)
return defaultValue
}
return f
}
func (fc *FileConfig) UnmarshalYAML(value *yaml.Node) error {
if value.Kind == yaml.ScalarNode {
fc.Path = value.Value
return nil
}
type alias FileConfig
tmp := alias(*fc)
err := value.Decode(&tmp)
if err != nil {
return err
}
*fc = FileConfig(tmp)
return nil
}
func (fc *FileConfig) UnmarshalJSON(b []byte) error {
switch b[0] {
case '"':
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
fc.Path = s
return nil
case '{':
dec := json.NewDecoder(bytes.NewReader(b))
type alias FileConfig
tmp := alias(*fc)
err := dec.Decode(&tmp)
if err != nil {
return err
}
*fc = FileConfig(tmp)
return nil
default:
return fmt.Errorf("unexpected JSON type: %s", string(b))
}
}
func (fc *FileConfig) Set(v any) error {
switch x := v.(type) {
case string:
fc.Path = x
return nil
}
return fmt.Errorf("expected string, got %T", v)
}
type Health struct {
Enabled bool `yaml:"enabled" json:"enabled"`
Path string `yaml:"path" json:"path"`
Port int `yaml:"port" json:"port"`
Log bool `yaml:"log" json:"log"`
}
type Mcp struct {
Server McpServer `yaml:"server" json:"server"`
}
type McpServer struct {
Enabled bool `yaml:"enabled" json:"enabled"`
Path string `yaml:"path" json:"path"`
Port int `yaml:"port" json:"port"`
}