Skip to content

Commit

Permalink
fix: sort otes configuration (#108)
Browse files Browse the repository at this point in the history
* fix: sort redis configuration

* fix: yaml serialization

* fix: add comment

* fix: add comment

* fix: doc

* fix: doc

* fix: sort otes config

* test: fix different config

* fix: test flakes

* fix: test flakes

* fix: test flakes

* fix: test flakes

Co-authored-by: marvin <lingwei0604@gmail.com>
  • Loading branch information
Reasno and lingwei0604 committed Mar 25, 2021
1 parent d810b38 commit 14233d6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ jobs:
discovery.type: single-node
ports:
- 9200:9200
minio:
image: bitnami/minio:latest
env:
MINIO_ACCESS_KEY: minioadmin
MINIO_SECRET_KEY: minioadmin
ports:
- 9000:9000
options: --name minio-server
steps:
- uses: actions/checkout@v2

Expand Down
13 changes: 13 additions & 0 deletions otes/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package otes

// Config is the type of elasticsearch configurations.
type Config struct {
URL []string `json:"url" yaml:"url"`
Index string `json:"index" yaml:"index"`
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
Shards int `json:"shards" yaml:"shards"`
Replicas int `json:"replicas" yaml:"replicas"`
Sniff *bool `json:"sniff" yaml:"sniff"`
Healthcheck *bool `json:"healthcheck" yaml:"healthcheck"`
}
23 changes: 8 additions & 15 deletions otes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/olivere/elastic/v7"
esConfig "github.com/olivere/elastic/v7/config"
"github.com/opentracing/opentracing-go"
"go.uber.org/dig"
)
Expand All @@ -34,7 +33,7 @@ func Providers() di.Deps {

// EsConfigInterceptor is an injector type hint that allows user to do
// last minute modification to es configurations.
type EsConfigInterceptor func(name string, opt *esConfig.Config)
type EsConfigInterceptor func(name string, opt *Config)

// Maker models Factory
type Maker interface {
Expand Down Expand Up @@ -80,22 +79,22 @@ type out struct {
// package core.
func provideEsFactory(p in) (out, func()) {
var err error
var esConfigs map[string]esConfig.Config
var esConfigs map[string]Config
err = p.Conf.Unmarshal("es", &esConfigs)
if err != nil {
level.Warn(p.Logger).Log("err", err)
}
factory := di.NewFactory(func(name string) (di.Pair, error) {
var (
ok bool
conf esConfig.Config
conf Config
options []elastic.ClientOptionFunc
)
if conf, ok = esConfigs[name]; !ok {
if name != "default" {
return di.Pair{}, fmt.Errorf("elastic configuration %s not valid", name)
}
conf.URL = "http://localhost:9200"
conf.URL = []string{"http://localhost:9200"}
}
if p.Interceptor != nil {
p.Interceptor(name, &conf)
Expand All @@ -120,7 +119,7 @@ func provideEsFactory(p in) (out, func()) {
}
logger := log.With(p.Logger, "tag", "es")
options = append(options,
elastic.SetURL(conf.URL),
elastic.SetURL(conf.URL...),
elastic.SetBasicAuth(conf.Username, conf.Password),
elastic.SetInfoLog(esLogAdapter{level.Info(logger)}),
elastic.SetErrorLog(esLogAdapter{level.Error(logger)}),
Expand Down Expand Up @@ -163,16 +162,10 @@ func provideConfig() configOut {
{
Owner: "otes",
Data: map[string]interface{}{
"es": map[string]map[string]interface{}{
"es": map[string]Config{
"default": {
"url": "http://localhost:9200",
"index": "",
"username": "",
"password": "",
"shards": 1,
"replicas": 0,
"sniff": false,
"healthCheck": false,
URL: []string{"http://localhost:9200"},
Shards: 1,
},
},
},
Expand Down
11 changes: 5 additions & 6 deletions otes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import (
"github.com/DoNewsCode/core/config"
"github.com/go-kit/kit/log"
"github.com/olivere/elastic/v7"
esConfig "github.com/olivere/elastic/v7/config"
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewEsFactory(t *testing.T) {
esFactory, cleanup := provideEsFactory(in{
Conf: config.MapAdapter{"es": map[string]esConfig.Config{
"default": {URL: "http://localhost:9200"},
"alternative": {URL: "http://localhost:9200"},
Conf: config.MapAdapter{"es": map[string]Config{
"default": {URL: []string{"http://localhost:9200"}},
"alternative": {URL: []string{"http://localhost:9200"}},
}},
Logger: log.NewNopLogger(),
Tracer: nil,
Expand All @@ -33,8 +32,8 @@ func TestNewEsFactory(t *testing.T) {
func TestNewEsFactoryWithOptions(t *testing.T) {
var called bool
esFactory, cleanup := provideEsFactory(in{
Conf: config.MapAdapter{"es": map[string]esConfig.Config{
"default": {URL: "http://localhost:9200"},
Conf: config.MapAdapter{"es": map[string]Config{
"default": {URL: []string{"http://localhost:9200"}},
}},
Logger: log.NewNopLogger(),
Options: []elastic.ClientOptionFunc{
Expand Down
12 changes: 6 additions & 6 deletions otes/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ package otes

import (
"context"
"net/http"
"testing"

"github.com/DoNewsCode/core/config"
"github.com/go-kit/kit/log"
esConfig "github.com/olivere/elastic/v7/config"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestTracing(t *testing.T) {
tracer := mocktracer.New()
opentracing.SetGlobalTracer(tracer)
factory, cleanup := provideEsFactory(in{
Conf: config.MapAdapter{"es": map[string]esConfig.Config{
"default": {URL: "http://localhost:9200"},
"alternative": {URL: "http://localhost:9200"},
Conf: config.MapAdapter{"es": map[string]Config{
"default": {URL: []string{"http://localhost:9200"}},
"alternative": {URL: []string{"http://localhost:9200"}},
}},
Logger: log.NewNopLogger(),
Tracer: tracer,
Expand Down
4 changes: 2 additions & 2 deletions ots3/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func Example() {
panic(err)
}
defer file.Close()
uploader := NewManager("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "https://play.minio.io:9000", "asia", "mybucket")
uploader := NewManager("minioadmin", "minioadmin", "http://localhost:9000", "asia", "mybucket")
_ = uploader.CreateBucket(context.Background(), "mybucket")
url, _ := uploader.Upload(context.Background(), "foo", file)
fmt.Println(url)

// Output:
// https://play.minio.io:9000/mybucket/foo.png
// http://localhost:9000/mybucket/foo.png
}
10 changes: 5 additions & 5 deletions ots3/uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestNewManager(t *testing.T) {
}

func TestMain(m *testing.M) {
manager := NewManager("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", "https://play.minio.io:9000", "asia", "mybucket")
manager := NewManager("minioadmin", "minioadmin", "http://localhost:9000", "asia", "mybucket")
_ = manager.CreateBucket(context.Background(), "foo")
os.Exit(m.Run())
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestManager_UploadFromUrl(t *testing.T) {
tracer := mocktracer.New()
m := setupManagerWithTracer(tracer)
_ = m.CreateBucket(context.Background(), "mybucket")
newURL, err := m.UploadFromUrl(context.Background(), "https://www.donews.com/static/v2/images/full-logo.png")
newURL, err := m.UploadFromUrl(context.Background(), "https://avatars.githubusercontent.com/u/43054062")
assert.NoError(t, err)
assert.NotEmpty(t, newURL)
assert.Len(t, tracer.FinishedSpans(), 2)
Expand All @@ -77,9 +77,9 @@ func setupManager() *Manager {

func setupManagerWithTracer(tracer opentracing.Tracer) *Manager {
m := NewManager(
"Q3AM3UQ867SPQQA43P2F",
"zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
"https://play.minio.io:9000",
"minioadmin",
"minioadmin",
"http://localhost:9000",
"asia",
"mybucket",
WithTracer(tracer),
Expand Down

0 comments on commit 14233d6

Please sign in to comment.