Skip to content

Commit 57d06b3

Browse files
committed
Add process extended config format
1 parent d4b420f commit 57d06b3

File tree

6 files changed

+41
-25
lines changed

6 files changed

+41
-25
lines changed

src/conf/conf.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ func (conf *Conf) readConfig() {
4343
val.Source, _ = conf.Util.AbsPath(val.Source)
4444
val.SourceType = conf.fileOrFolder(val.Source)
4545
}
46-
if val.SourceType == "url" && val.HTTPMethod == "" {
47-
val.HTTPMethod = "get"
46+
if val.SourceType == "url" && val.RequestMethod == "" {
47+
val.RequestMethod = "get"
4848
}
49-
val.HTTPMethod = strings.ToUpper(val.HTTPMethod)
49+
val.RequestMethod = strings.ToUpper(val.RequestMethod)
50+
val.RequestMethod = conf.Util.RxReplaceAll(
51+
val.RequestMethod, "^HTTP_", "",
52+
)
5053
var v datasize.ByteSize
5154
if val.MaxReturnSize == "" {
5255
val.MaxReturnSize = "10K"

src/conf/struct.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@ type Endpoint struct {
2424
Source string `yaml:"source"`
2525
SourceType string
2626
URL string `yaml:"url"`
27-
HTTPMethod string `yaml:"http_method"`
27+
RequestMethod string `yaml:"request_method"`
2828
RxFilter string `yaml:"regex_filter"`
2929
SortFileName string `yaml:"sort_file_name"`
3030
IgnoreList []string `yaml:"regex_ignore_list"`
3131
MaxReturnSize string `yaml:"max_return_size"`
3232
MaxReturnSizeBytes uint64
33-
ReturnValues ReturnValues `yaml:"return_values"`
33+
Return ReturnValues `yaml:"return"`
34+
}
35+
36+
type Process struct {
37+
Strategy string `yaml:"strategy"`
38+
RequestMethod string `yaml:"request_method"`
39+
JSONPath string `yaml:"json_path"`
40+
RegexMatch []string `yaml:"regex_match"`
3441
}
3542

3643
type ReturnValues struct {
@@ -39,6 +46,7 @@ type ReturnValues struct {
3946
Content bool `yaml:"content"`
4047
UnmarshalContent bool `yaml:"unmarshal_content"`
4148
RegexReplace [][]string `yaml:"regex_replace"`
49+
RegexMatch string `yaml:"regex_match"`
4250
JSONPath string `yaml:"json_path"`
4351
Size bool `yaml:"size"`
4452
LastMod bool `yaml:"lastmod"`

src/indexer/init_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func prepareTests(folder, sortBy string, asc bool) (Indexer, JoinerIndex, Params
2323
}
2424

2525
func newTestEndpoint() conf.Endpoint {
26-
return conf.Endpoint{ReturnValues: conf.ReturnValues{
26+
return conf.Endpoint{Return: conf.ReturnValues{
2727
Created: true,
2828
LastMod: true,
2929
Content: true,

src/indexer/ji.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func (ind Indexer) MakeJoinerIndex(params Params) (ji JoinerIndex) {
4747
}
4848
case "url":
4949
je := JoinerEntry{Path: params.Endpoint.Source}
50-
if params.Endpoint.ReturnValues.Content {
50+
if params.Endpoint.Return.Content {
5151
resp, err := ind.req(
5252
params.Endpoint.Source,
53-
params.Endpoint.HTTPMethod,
53+
params.Endpoint.RequestMethod,
5454
)
5555
// TODO: maybe later add the possibility to encode base64
5656
je.Content = ind.byteToBody(resp)

src/indexer/read.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func (ind Indexer) readDataFile(filename string, ps conf.Endpoint, chin chan str
3131
Path: pth,
3232
}
3333
fileSize := ind.Util.GetFileSize(filename)
34-
if ps.ReturnValues.Size {
34+
if ps.Return.Size {
3535
je.Size = fileSize
3636
}
3737
if ps.MaxReturnSizeBytes > fileSize {
38-
if ps.ReturnValues.Content || ps.ReturnValues.SplitMarkdownFrontMatter {
38+
if ps.Return.Content || ps.Return.SplitMarkdownFrontMatter {
3939
je.Content = ind.readFileContent(filename, ps)
4040
}
4141
} else {
@@ -48,13 +48,13 @@ func (ind Indexer) readDataFile(filename string, ps conf.Endpoint, chin chan str
4848
},
4949
)
5050
}
51-
if ps.ReturnValues.SplitPath {
51+
if ps.Return.SplitPath {
5252
je.SplitPath = strings.Split(pth, string(filepath.Separator))
5353
}
54-
if ps.ReturnValues.Created {
54+
if ps.Return.Created {
5555
je.Created = ind.Util.GetFileCreated(filename)
5656
}
57-
if ps.ReturnValues.LastMod {
57+
if ps.Return.LastMod {
5858
je.LastMod = ind.Util.GetFileLastMod(filename)
5959
}
6060
chout <- je
@@ -68,7 +68,7 @@ func (ind Indexer) readFileContent(filename string, ps conf.Endpoint) (content F
6868
case ".json", ".toml", ".yaml", ".yml":
6969
content = ind.unmarshal(by, ps)
7070
case ".md":
71-
content = ind.readMarkdown(by, ps.ReturnValues)
71+
content = ind.readMarkdown(by, ps.Return)
7272
default:
7373
content = ind.byteToBody(by)
7474
}
@@ -95,7 +95,7 @@ func (ind Indexer) byteToBody(by []byte) (content FileContent) {
9595
func (ind Indexer) unmarshal(by []byte, ps conf.Endpoint) (content FileContent) {
9696
if ind.Util.IsTextData(by) {
9797

98-
for _, el := range ps.ReturnValues.RegexReplace {
98+
for _, el := range ps.Return.RegexReplace {
9999
by = []byte(
100100
ind.Util.RxReplaceAll(
101101
string(by),
@@ -131,11 +131,11 @@ func (ind Indexer) unmarshal(by []byte, ps conf.Endpoint) (content FileContent)
131131
logseal.F{"content": string(by), "err": content.Error},
132132
)
133133
}
134-
if ps.ReturnValues.JSONPath != "" {
134+
if ps.Return.JSONPath != "" {
135135
ind.Lg.Trace(
136136
"parse unmarshalled data using json path",
137137
logseal.F{
138-
"json_path": ps.ReturnValues.JSONPath,
138+
"json_path": ps.Return.JSONPath,
139139
},
140140
)
141141
marsh, err := json.Marshal(content.Body)
@@ -144,11 +144,11 @@ func (ind Indexer) unmarshal(by []byte, ps conf.Endpoint) (content FileContent)
144144
logseal.F{"error": err},
145145
)
146146
if err == nil {
147-
result := gjson.GetBytes(marsh, ps.ReturnValues.JSONPath)
147+
result := gjson.GetBytes(marsh, ps.Return.JSONPath)
148148
if len(result.String()) < 1 {
149149
ind.Lg.Warn(
150150
"json path result is empty",
151-
logseal.F{"json_path": ps.ReturnValues.JSONPath},
151+
logseal.F{"json_path": ps.Return.JSONPath},
152152
)
153153
} else {
154154
content = ind.unmarshalJSON([]byte(result.String()))

testdata/http.yaml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@ port: 17777
44
api:
55
repos.json:
66
source: http://localhost/repos
7-
http_method: get
8-
return_values:
7+
request_method: http_get
8+
return:
99
created: true
1010
content: true
1111
unmarshal_content: true
1212
json_path: "#.readme_url"
1313
regex_replace:
1414
- ["README\\.md", "public.yaml"]
1515

16-
1716
meta.json:
1817
source: http://localhost:17777/repos.json
19-
http_method: get
20-
return_values:
18+
request_method: http_get
19+
process:
20+
- strategy: use_as_url_list
21+
request_method: http_get
22+
json_path: "#.content.body"
23+
regex_match:
24+
- "http:\\/\\//*?\""
25+
return:
2126
content: true
22-
json_path: "#.content.body"
27+
unmarshal_content: true

0 commit comments

Comments
 (0)