Skip to content

Commit c4e3e7f

Browse files
authored
Refactoring (#78)
* Refactoring
1 parent 8130ddb commit c4e3e7f

36 files changed

+178
-338
lines changed

clients/aws.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package clients
2+
3+
import (
4+
"context"
5+
"net/url"
6+
"os"
7+
8+
"github.com/aws/aws-sdk-go-v2/aws"
9+
"github.com/aws/aws-sdk-go-v2/config"
10+
"github.com/aws/aws-sdk-go-v2/credentials"
11+
)
12+
13+
func NewAwsConfigWithUri(uri string) (aws.Config, error) {
14+
ctx := context.Background()
15+
16+
cfg := aws.Config{}
17+
18+
// Parse URI.
19+
u, err := url.Parse(uri)
20+
if err != nil {
21+
return cfg, err
22+
}
23+
24+
profile := os.Getenv("AWS_PROFILE")
25+
if str := u.Query().Get("profile"); str != "" {
26+
profile = str
27+
}
28+
if profile != "" {
29+
cfg, err = config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(profile))
30+
if err != nil {
31+
return cfg, err
32+
}
33+
} else {
34+
cfg, err = config.LoadDefaultConfig(ctx)
35+
if err != nil {
36+
return cfg, err
37+
}
38+
}
39+
40+
useCredentials := false
41+
accessKeyId := os.Getenv("AWS_ACCESS_KEY_ID")
42+
if str := u.Query().Get("access_key_id"); str != "" {
43+
accessKeyId = str
44+
}
45+
if accessKeyId != "" {
46+
useCredentials = true
47+
}
48+
49+
secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
50+
if str := u.Query().Get("secret_access_key"); str != "" {
51+
secretAccessKey = str
52+
}
53+
if secretAccessKey != "" {
54+
useCredentials = true
55+
}
56+
57+
sessionToken := os.Getenv("AWS_SESSION_TOKEN")
58+
if str := u.Query().Get("session_token"); str != "" {
59+
sessionToken = str
60+
}
61+
if sessionToken != "" {
62+
useCredentials = true
63+
}
64+
65+
if useCredentials {
66+
cfg.Credentials = aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(accessKeyId, secretAccessKey, sessionToken))
67+
}
68+
69+
region := os.Getenv("AWS_DEFAULT_REGION")
70+
if str := u.Query().Get("region"); str != "" {
71+
region = str
72+
}
73+
if region != "" {
74+
cfg.Region = region
75+
}
76+
77+
endpoint := os.Getenv("AWS_ENDPOINT_URL")
78+
if str := u.Query().Get("endpoint_url"); str != "" {
79+
endpoint = str
80+
}
81+
if endpoint != "" {
82+
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
83+
return aws.Endpoint{
84+
PartitionID: "aws",
85+
URL: endpoint,
86+
SigningRegion: cfg.Region,
87+
}, nil
88+
})
89+
cfg.EndpointResolverWithOptions = customResolver
90+
}
91+
92+
return cfg, nil
93+
}

clients/dynamodb.go

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package clients
22

33
import (
4-
"context"
54
"net/url"
6-
"os"
75

8-
"github.com/aws/aws-sdk-go-v2/aws"
9-
"github.com/aws/aws-sdk-go-v2/config"
10-
"github.com/aws/aws-sdk-go-v2/credentials"
116
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
127
"github.com/mosuka/phalanx/errors"
138
)
@@ -23,76 +18,10 @@ func NewDynamoDBClientWithUri(uri string) (*dynamodb.Client, error) {
2318
return nil, errors.ErrInvalidUri
2419
}
2520

26-
ctx := context.Background()
27-
28-
var cfg aws.Config
29-
30-
profile := os.Getenv("AWS_PROFILE")
31-
if str := u.Query().Get("profile"); str != "" {
32-
profile = str
33-
}
34-
if profile != "" {
35-
cfg, err = config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(profile))
36-
if err != nil {
37-
return nil, err
38-
}
39-
} else {
40-
cfg, err = config.LoadDefaultConfig(ctx)
41-
if err != nil {
42-
return nil, err
43-
}
44-
}
45-
46-
useCredentials := false
47-
accessKeyId := os.Getenv("AWS_ACCESS_KEY_ID")
48-
if str := u.Query().Get("access_key_id"); str != "" {
49-
accessKeyId = str
50-
}
51-
if accessKeyId != "" {
52-
useCredentials = true
53-
}
54-
55-
secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
56-
if str := u.Query().Get("secret_access_key"); str != "" {
57-
secretAccessKey = str
58-
}
59-
if secretAccessKey != "" {
60-
useCredentials = true
61-
}
62-
63-
sessionToken := os.Getenv("AWS_SESSION_TOKEN")
64-
if str := u.Query().Get("session_token"); str != "" {
65-
sessionToken = str
66-
}
67-
if sessionToken != "" {
68-
useCredentials = true
69-
}
70-
71-
if useCredentials {
72-
cfg.Credentials = aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(accessKeyId, secretAccessKey, sessionToken))
73-
}
74-
75-
region := os.Getenv("AWS_DEFAULT_REGION")
76-
if str := u.Query().Get("region"); str != "" {
77-
region = str
78-
}
79-
if region != "" {
80-
cfg.Region = region
81-
}
82-
83-
endpoint := os.Getenv("AWS_ENDPOINT_URL")
84-
if str := u.Query().Get("endpoint_url"); str != "" {
85-
endpoint = str
86-
}
87-
if endpoint != "" {
88-
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
89-
return aws.Endpoint{
90-
PartitionID: "aws",
91-
URL: endpoint,
92-
SigningRegion: cfg.Region,
93-
}, nil
94-
})
95-
cfg.EndpointResolverWithOptions = customResolver
21+
// Create AWS config.
22+
cfg, err := NewAwsConfigWithUri(uri)
23+
if err != nil {
24+
return nil, err
9625
}
9726

9827
return dynamodb.NewFromConfig(cfg), nil

clients/grpc_client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/mosuka/phalanx/proto"
88
"google.golang.org/grpc"
99
"google.golang.org/grpc/credentials"
10+
"google.golang.org/grpc/credentials/insecure"
1011
"google.golang.org/grpc/keepalive"
1112
)
1213

@@ -41,7 +42,7 @@ func NewGRPCIndexClientWithTLS(address string, certFile string, commonName strin
4142
}
4243
dialOpts = append(dialOpts, grpc.WithTransportCredentials(creds))
4344
} else {
44-
dialOpts = append(dialOpts, grpc.WithInsecure())
45+
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
4546
}
4647

4748
conn, err := grpc.Dial(address, dialOpts...)

clients/s3.go

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package clients
22

33
import (
4-
"context"
5-
"fmt"
64
"net/url"
75
"os"
86

9-
"github.com/aws/aws-sdk-go-v2/aws"
10-
"github.com/aws/aws-sdk-go-v2/config"
11-
"github.com/aws/aws-sdk-go-v2/credentials"
127
"github.com/aws/aws-sdk-go-v2/service/s3"
138
"github.com/mosuka/phalanx/errors"
149
)
@@ -24,76 +19,10 @@ func NewS3ClientWithUri(uri string) (*s3.Client, error) {
2419
return nil, errors.ErrInvalidUri
2520
}
2621

27-
ctx := context.Background()
28-
29-
var cfg aws.Config
30-
31-
profile := os.Getenv("AWS_PROFILE")
32-
if str := u.Query().Get("profile"); str != "" {
33-
profile = str
34-
}
35-
if profile != "" {
36-
cfg, err = config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(profile))
37-
if err != nil {
38-
return nil, err
39-
}
40-
} else {
41-
cfg, err = config.LoadDefaultConfig(ctx)
42-
if err != nil {
43-
return nil, err
44-
}
45-
}
46-
47-
useCredentials := false
48-
accessKeyId := os.Getenv("AWS_ACCESS_KEY_ID")
49-
if str := u.Query().Get("access_key_id"); str != "" {
50-
accessKeyId = str
51-
}
52-
if accessKeyId != "" {
53-
useCredentials = true
54-
}
55-
56-
secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
57-
if str := u.Query().Get("secret_access_key"); str != "" {
58-
secretAccessKey = str
59-
}
60-
if secretAccessKey != "" {
61-
useCredentials = true
62-
}
63-
64-
sessionToken := os.Getenv("AWS_SESSION_TOKEN")
65-
if str := u.Query().Get("session_token"); str != "" {
66-
sessionToken = str
67-
}
68-
if sessionToken != "" {
69-
useCredentials = true
70-
}
71-
72-
if useCredentials {
73-
cfg.Credentials = aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(accessKeyId, secretAccessKey, sessionToken))
74-
}
75-
76-
region := os.Getenv("AWS_DEFAULT_REGION")
77-
if str := u.Query().Get("region"); str != "" {
78-
region = str
79-
}
80-
if region != "" {
81-
cfg.Region = region
82-
}
83-
84-
endpoint := os.Getenv("AWS_ENDPOINT_URL")
85-
if str := u.Query().Get("endpoint_url"); str != "" {
86-
endpoint = str
87-
}
88-
if endpoint != "" {
89-
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
90-
return aws.Endpoint{
91-
PartitionID: "aws",
92-
URL: endpoint,
93-
SigningRegion: cfg.Region,
94-
}, nil
95-
})
96-
cfg.EndpointResolverWithOptions = customResolver
22+
// Create AWS config.
23+
cfg, err := NewAwsConfigWithUri(uri)
24+
if err != nil {
25+
return nil, err
9726
}
9827

9928
usePathStyle := false
@@ -102,7 +31,6 @@ func NewS3ClientWithUri(uri string) (*s3.Client, error) {
10231
usePathStyleStr = str
10332
}
10433
if usePathStyleStr == "true" {
105-
fmt.Println("AWS_USE_PATH_STYLE is set to true. Using path style.")
10634
usePathStyle = true
10735
}
10836

docs/aggregations.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ <h1 class="search-results-title">No results matching "<span class='search-query'
636636
<script>
637637
var gitbook = gitbook || [];
638638
gitbook.push(function() {
639-
gitbook.page.hasChanged({"page":{"title":"Aggregations","level":"1.13","depth":1,"next":{"title":"RESTful API","level":"1.14","depth":1,"path":"restful_api.md","ref":"restful_api.md","articles":[{"title":"Create Index API","level":"1.14.1","depth":2,"path":"restful_api/create_index_api.md","ref":"restful_api/create_index_api.md","articles":[]},{"title":"Delete Index API","level":"1.14.2","depth":2,"path":"restful_api/delete_index_api.md","ref":"restful_api/delete_index_api.md","articles":[]},{"title":"Add Documents API","level":"1.14.3","depth":2,"path":"restful_api/add_documents_api.md","ref":"restful_api/add_documents_api.md","articles":[]},{"title":"Delete Documents API","level":"1.14.4","depth":2,"path":"restful_api/delete_documents_api.md","ref":"restful_api/delete_documents_api.md","articles":[]},{"title":"Search API","level":"1.14.5","depth":2,"path":"restful_api/search_api.md","ref":"restful_api/search_api.md","articles":[]}]},"previous":{"title":"Token filters","level":"1.12.3","depth":2,"path":"analyzer/token_filters.md","ref":"analyzer/token_filters.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-published-with"],"pluginsConfig":{"hide-published-with":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"aggregations.md","mtime":"2022-01-13T13:10:47.906Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-01-17T08:03:59.688Z"},"basePath":".","book":{"language":""}});
639+
gitbook.page.hasChanged({"page":{"title":"Aggregations","level":"1.13","depth":1,"next":{"title":"RESTful API","level":"1.14","depth":1,"path":"restful_api.md","ref":"restful_api.md","articles":[{"title":"Create Index API","level":"1.14.1","depth":2,"path":"restful_api/create_index_api.md","ref":"restful_api/create_index_api.md","articles":[]},{"title":"Delete Index API","level":"1.14.2","depth":2,"path":"restful_api/delete_index_api.md","ref":"restful_api/delete_index_api.md","articles":[]},{"title":"Add Documents API","level":"1.14.3","depth":2,"path":"restful_api/add_documents_api.md","ref":"restful_api/add_documents_api.md","articles":[]},{"title":"Delete Documents API","level":"1.14.4","depth":2,"path":"restful_api/delete_documents_api.md","ref":"restful_api/delete_documents_api.md","articles":[]},{"title":"Search API","level":"1.14.5","depth":2,"path":"restful_api/search_api.md","ref":"restful_api/search_api.md","articles":[]}]},"previous":{"title":"Token filters","level":"1.12.3","depth":2,"path":"analyzer/token_filters.md","ref":"analyzer/token_filters.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-published-with"],"pluginsConfig":{"hide-published-with":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"aggregations.md","mtime":"2022-01-18T13:00:14.678Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-01-20T06:50:37.135Z"},"basePath":".","book":{"language":""}});
640640
});
641641
</script>
642642
</div>

docs/analyzer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ <h1 class="search-results-title">No results matching "<span class='search-query'
533533
<script>
534534
var gitbook = gitbook || [];
535535
gitbook.push(function() {
536-
gitbook.page.hasChanged({"page":{"title":"Analyzer","level":"1.12","depth":1,"next":{"title":"Char filters","level":"1.12.1","depth":2,"path":"analyzer/char_filters.md","ref":"analyzer/char_filters.md","articles":[]},"previous":{"title":"Index Mapping","level":"1.11","depth":1,"path":"index_mapping.md","ref":"index_mapping.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-published-with"],"pluginsConfig":{"hide-published-with":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"analyzer.md","mtime":"2022-01-13T13:51:45.177Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-01-17T08:03:59.688Z"},"basePath":".","book":{"language":""}});
536+
gitbook.page.hasChanged({"page":{"title":"Analyzer","level":"1.12","depth":1,"next":{"title":"Char filters","level":"1.12.1","depth":2,"path":"analyzer/char_filters.md","ref":"analyzer/char_filters.md","articles":[]},"previous":{"title":"Index Mapping","level":"1.11","depth":1,"path":"index_mapping.md","ref":"index_mapping.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-published-with"],"pluginsConfig":{"hide-published-with":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"analyzer.md","mtime":"2022-01-18T13:00:14.678Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-01-20T06:50:37.135Z"},"basePath":".","book":{"language":""}});
537537
});
538538
</script>
539539
</div>

0 commit comments

Comments
 (0)