Skip to content

Commit

Permalink
Merge pull request #38 from sljeff/feat-flatten-config
Browse files Browse the repository at this point in the history
flatten config
  • Loading branch information
HeathLee authored Jan 2, 2020
2 parents 7abd7b9 + 3aae35b commit 0d27186
Show file tree
Hide file tree
Showing 23 changed files with 130 additions and 193 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@

1. 实现`gobay.Extension`
2. 每一个ext目录都是你的例子
3. ext读取配置的规则请参考这个[issue](https://github.com/shanbay/gobay/issues/8#issuecomment-558023666)
16 changes: 16 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"

"github.com/spf13/viper"
Expand Down Expand Up @@ -151,3 +152,18 @@ func CreateApp(rootPath string, env string, exts map[Key]Extension) (*Applicatio
}
return app, nil
}

func GetConfigByPrefix(config *viper.Viper, prefix string, trimPrefix bool) *viper.Viper {
subConfig := viper.New()
for k, v := range config.AllSettings() {
if !strings.HasPrefix(k, prefix) {
continue
}
key := k
if trimPrefix {
key = k[len(prefix):]
}
subConfig.SetDefault(key, v)
}
return subConfig
}
8 changes: 5 additions & 3 deletions asynctaskext/asynctaskext.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package asynctaskext

import (
"errors"
"os"

"github.com/RichardKnop/machinery/v1"
Expand Down Expand Up @@ -30,11 +31,12 @@ func (t *AsyncTaskExt) Application() *gobay.Application {
}

func (t *AsyncTaskExt) Init(app *gobay.Application) error {
if t.NS == "" {
return errors.New("lack of NS")
}
t.app = app
config := app.Config()
if t.NS != "" {
config = config.Sub(t.NS)
}
config = gobay.GetConfigByPrefix(config, t.NS, true)
t.config = &machineryConfig.Config{}
if err := config.Unmarshal(t.config, func(config *mapstructure.
DecoderConfig) {
Expand Down
10 changes: 5 additions & 5 deletions asynctaskext/asynctaskext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var (
)

func init() {
task = AsyncTaskExt{NS: "asynctask"}
task = AsyncTaskExt{NS: "asynctask_"}

app, _ = gobay.CreateApp(
app, _ := gobay.CreateApp(
"../testdata",
"testing",
map[gobay.Key]gobay.Extension{
Expand All @@ -44,15 +44,15 @@ func TestPushConsume(t *testing.T) {
Name: "add",
Args: []tasks.Arg{
{
Type: "int64",
Type: "int64",
Value: 1,
},
{
Type: "int64",
Type: "int64",
Value: 2,
},
{
Type: "int64",
Type: "int64",
Value: 3,
},
},
Expand Down
9 changes: 5 additions & 4 deletions busext/amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ func (b *BusExt) Application() *gobay.Application {
}

func (b *BusExt) Init(app *gobay.Application) error {
b.app = app
b.config = app.Config()
if b.NS != "" {
b.config = b.config.Sub(b.NS)
if b.NS == "" {
return errors.New("lack of NS")
}
b.app = app
config := app.Config()
b.config = gobay.GetConfigByPrefix(config, b.NS, true)
setDefaultConfig(b.config)
b.consumers = make(map[string]Handler)
b.consumeChannels = make(map[string]<-chan amqp.Delivery)
Expand Down
2 changes: 1 addition & 1 deletion busext/amqp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
)

func init() {
bus = BusExt{NS: "bus"}
bus = BusExt{NS: "bus_"}

app, _ = gobay.CreateApp(
"../testdata",
Expand Down
6 changes: 3 additions & 3 deletions cachext/backend/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ type redisBackend struct {
}

func (b *redisBackend) Init(config *viper.Viper) error {
host := config.GetString("cache_host")
password := config.GetString("cache_password")
dbNum := config.GetInt("cache_db")
host := config.GetString("host")
password := config.GetString("password")
dbNum := config.GetInt("db")
redisClient := redis.NewClient(&redis.Options{
Addr: host,
Password: password,
Expand Down
11 changes: 6 additions & 5 deletions cachext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type CacheBackend interface {

// Init init a cache extension
func (c *CacheExt) Init(app *gobay.Application) error {
if c.NS == "" {
return errors.New("lack of NS")
}
mu.Lock()
defer mu.Unlock()

Expand All @@ -57,11 +60,9 @@ func (c *CacheExt) Init(app *gobay.Application) error {
c.cachedFuncName = make(map[string]void)
config := app.Config()
c.enableApm = config.GetBool("elastic_apm_enable")
if c.NS != "" {
config = config.Sub(c.NS)
}
c.prefix = config.GetString("cache_prefix")
backendConfig := config.GetString("cache_backend")
config = gobay.GetConfigByPrefix(config, c.NS, true)
c.prefix = config.GetString("prefix")
backendConfig := config.GetString("backend")
if backend, exist := backendMap[backendConfig]; exist {
c.backend = backend
if err := c.backend.Init(config); err != nil {
Expand Down
18 changes: 9 additions & 9 deletions cachext/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func ExampleCacheExt_Set() {
cache := &cachext.CacheExt{}
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
Expand All @@ -33,7 +33,7 @@ func ExampleCacheExt_Set() {
}

func ExampleCacheExt_Cached() {
cache := &cachext.CacheExt{}
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func ExampleCacheExt_Cached() {
}

func ExampleCacheExt_SetMany() {
cache := &cachext.CacheExt{}
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func ExampleCacheExt_SetMany() {
}

func TestCacheExt_Operation(t *testing.T) {
cache := &cachext.CacheExt{}
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestCacheExt_Operation(t *testing.T) {

func TestCacheExt_Cached_Common(t *testing.T) {
// 准备数据
cache := &cachext.CacheExt{}
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
Expand Down Expand Up @@ -412,7 +412,7 @@ func TestCacheExt_Cached_Common(t *testing.T) {

func TestCacheExt_Cached_Struct(t *testing.T) {
// 准备数据
cache := &cachext.CacheExt{}
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
Expand Down Expand Up @@ -461,7 +461,7 @@ func TestCacheExt_Cached_Struct(t *testing.T) {
}

func Benchmark_SetMany(b *testing.B) {
cache := &cachext.CacheExt{}
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
Expand All @@ -487,7 +487,7 @@ func Benchmark_SetMany(b *testing.B) {
}

func Benchmark_GetMany(b *testing.B) {
cache := &cachext.CacheExt{}
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
Expand Down Expand Up @@ -530,7 +530,7 @@ func Benchmark_GetMany(b *testing.B) {
}

func Benchmark_Cached(b *testing.B) {
cache := &cachext.CacheExt{}
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
Expand Down
13 changes: 7 additions & 6 deletions entext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package entext

import (
"database/sql"
"errors"
"github.com/facebookincubator/ent/dialect"
entsql "github.com/facebookincubator/ent/dialect/sql"
"github.com/shanbay/gobay"
Expand Down Expand Up @@ -32,15 +33,15 @@ func (d *EntExt) Object() interface{} { return d.client }
func (d *EntExt) Application() *gobay.Application { return d.app }

func (d *EntExt) Init(app *gobay.Application) error {
d.app = app
config := app.Config()
if d.NS != "" {
config = config.Sub(d.NS)
if d.NS == "" {
return errors.New("lack of NS")
}
d.app = app
config := gobay.GetConfigByPrefix(app.Config(), d.NS, true)
config.SetDefault("max_open_conns", defaultMaxOpenConns)
config.SetDefault("max_idle_conns", defaultMaxIdleConns)
dbURL := config.GetString("db_url")
dbDriver := config.GetString("db_driver")
dbURL := config.GetString("url")
dbDriver := config.GetString("driver")

var db *sql.DB
var err error
Expand Down
2 changes: 1 addition & 1 deletion entext/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var entclient *ent.Client
func setup() func() error {
exts := map[gobay.Key]gobay.Extension{
"entext": &EntExt{
NS: "ent",
NS: "db_",
NewClient: func(drvopt interface{}) Client {
return ent.NewClient(drvopt.(ent.Option))
},
Expand Down
3 changes: 1 addition & 2 deletions esapmext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func (e *EsApmExt) Application() *gobay.Application {

// https://www.elastic.co/guide/en/apm/agent/go/current/configuration.html
func (e *EsApmExt) Init(app *gobay.Application) error {

config := app.Config()
config := gobay.GetConfigByPrefix(app.Config(), "elastic_", false)
if !config.GetBool("elastic_apm_enable") {
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ go.elastic.co/apm/module/apmgoredis v1.6.0 h1:E/4nyzB8yo1zX1BduAzFpvs/45fq8csMXZ
go.elastic.co/apm/module/apmgoredis v1.6.0/go.mod h1:2A858ngLiX0/IqNEPPLoUVXT5v527UK0MD7JMEVrWJM=
go.elastic.co/apm/module/apmgorm v1.6.0 h1:E1CANwa6+xdRUwMBSqgYILIK1mvrFzqIsW/6QYJXjsk=
go.elastic.co/apm/module/apmgorm v1.6.0/go.mod h1:pnpRtjjQ7CJbq9c3OIIcpDq1vbumitJQFhKvOUkSbdw=
go.elastic.co/apm/module/apmgrpc v1.6.0 h1:LRJO/C2yxCTKSr3FW/0TxLzBW68dBQNm8XX/cq53OdU=
go.elastic.co/apm/module/apmgrpc v1.6.0/go.mod h1:OnV9gT0x24g6R7w/eHIS8CFgRgJGvNUWRIrS3Qtd4Jk=
go.elastic.co/apm/module/apmhttp v1.6.0 h1:Y67VwDNnUyq4akeiem8Wtpb222RbZCRfY3FBI0swoJk=
go.elastic.co/apm/module/apmhttp v1.6.0/go.mod h1:pf6GS5vDxdrSF0Qy6wESRBZCRZqATSZpr658ehD5QA4=
go.elastic.co/apm/module/apmsql v1.6.0 h1:0vMQDYFtA7G6QgGai38CmYY5ki9Q6g/ub/jCQp8/7qU=
go.elastic.co/apm/module/apmsql v1.6.0/go.mod h1:cmMxjn6l+tjYM9+/sYnlhyGOFcj5Ye+P2NW2yslp/N8=
Expand Down
66 changes: 0 additions & 66 deletions gormext/ext.go

This file was deleted.

15 changes: 8 additions & 7 deletions redisext/ext.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redisext

import (
"errors"
"github.com/go-redis/redis"
"github.com/shanbay/gobay"
)
Expand All @@ -16,14 +17,14 @@ var _ gobay.Extension = (*RedisExt)(nil)

// Init
func (c *RedisExt) Init(app *gobay.Application) error {
c.app = app
config := app.Config()
if c.NS != "" {
config = config.Sub(c.NS)
if c.NS == "" {
return errors.New("lack of NS")
}
host := config.GetString("redis_host")
password := config.GetString("redis_password")
dbNum := config.GetInt("redis_db")
c.app = app
config := gobay.GetConfigByPrefix(app.Config(), c.NS, true)
host := config.GetString("host")
password := config.GetString("password")
dbNum := config.GetInt("db")
c.client = redis.NewClient(&redis.Options{
Addr: host,
Password: password,
Expand Down
Loading

0 comments on commit 0d27186

Please sign in to comment.