Skip to content

Commit

Permalink
Merge pull request #203 from yoyofx/dev
Browse files Browse the repository at this point in the history
v1.8.1
  • Loading branch information
yoyofx authored Apr 25, 2022
2 parents 47bf239 + 3824b8f commit 4485272
Show file tree
Hide file tree
Showing 30 changed files with 234 additions and 98 deletions.
20 changes: 11 additions & 9 deletions abstractions/configurationbuilder.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package abstractions

import (
"github.com/spf13/viper"
"github.com/yoyofx/yoyogo/abstractions/hostenv"
)

type ConfigurationContext struct {
enableFlag bool
enableEnv bool
configDir string
ConfigType string
configName string
profile string
configFile string
EnableRemote bool
RemoteProvider IConfigurationRemoteProvider
enableFlag bool
enableEnv bool
configDir string
ConfigType string
configName string
profile string
configFile string
EnableRemote bool
RemoteProvider IConfigurationRemoteProvider
decoderConfigOption viper.DecoderConfigOption
}

type ConfigurationBuilder struct {
Expand Down
84 changes: 77 additions & 7 deletions abstractions/default_viper_configuration.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package abstractions

import (
"errors"
"flag"
"github.com/jinzhu/copier"
"github.com/mitchellh/mapstructure"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/yoyofx/yoyogo/abstractions/xlog"
"github.com/yoyofx/yoyogo/utils"
"path"
"reflect"
"strings"
"sync"
)

Expand Down Expand Up @@ -91,10 +95,26 @@ func NewConfiguration(configContext *ConfigurationContext) *Configuration {
log.Error("remote config is not ready , switch local.")
}
}

configuration.Initialize()
return configuration
}

func (c *Configuration) Initialize() {
c.context.decoderConfigOption = func(config *mapstructure.DecoderConfig) {
config.TagName = "config"
config.DecodeHook = func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if f.Kind() != reflect.String && t.Kind() != reflect.String {
return data, nil
}
fromStr := data.(string)
if c.assertDSL(fromStr) {
return c.bindEnvDSL(fromStr, "")
}
return data, nil
}
}
}

func (c *Configuration) OnWatchRemoteConfigChanged() {
respChan := c.context.RemoteProvider.WatchRemoteConfigOnChannel(c.config)
go func(rc <-chan bool) {
Expand All @@ -107,32 +127,43 @@ func (c *Configuration) OnWatchRemoteConfigChanged() {
}

func (c *Configuration) Get(name string) interface{} {
if c.assertDSL(c.config.GetString(name)) {
_, _ = c.bindEnvDSL(c.config.GetString(name), name)
}
return c.config.Get(name)
}

func (c *Configuration) GetString(name string) string {
if c.assertDSL(c.config.GetString(name)) {
_, _ = c.bindEnvDSL(c.config.GetString(name), name)
}
return c.config.GetString(name)
}

func (c *Configuration) GetBool(name string) bool {
if c.assertDSL(c.config.GetString(name)) {
_, _ = c.bindEnvDSL(c.config.GetString(name), name)
}
return c.config.GetBool(name)
}

func (c *Configuration) GetInt(name string) int {
if c.assertDSL(c.config.GetString(name)) {
_, _ = c.bindEnvDSL(c.config.GetString(name), name)
}
return c.config.GetInt(name)
}

func (c *Configuration) GetSection(name string) IConfiguration {
section := c.config.Sub(name)

if section != nil {
return &Configuration{config: section}
return &Configuration{config: section, log: c.log, context: c.context, configMap: c.configMap}
}
return nil
}

func (c *Configuration) Unmarshal(obj interface{}) {
err := c.config.Unmarshal(obj)
err := c.config.Unmarshal(obj, c.context.decoderConfigOption)
if err != nil {
c.log.Error("unmarshal config is failed, err:", err)
}
Expand All @@ -159,19 +190,58 @@ func (c *Configuration) GetConfigObject(configTag string, configObject interface
c.configMap[configTag] = object
c.gRWLock.Unlock()
} else {
_ = copier.Copy(configObject, object)
_ = copier.CopyWithOption(configObject, object, copier.Option{IgnoreEmpty: true, DeepCopy: true})
}

}

func (c *Configuration) RefreshAll() {
c.gRWLock.Lock()
defer c.gRWLock.Unlock()
c.configMap = make(map[string]interface{})
c.gRWLock.Unlock()
}

func (c *Configuration) RefreshBy(name string) {
c.gRWLock.Lock()
defer c.gRWLock.Unlock()
delete(c.configMap, name)
}

/**
bindEnvDSL 读取DSL:环境变量 -> ${ENV:DEFAULT}
*/
func (c *Configuration) bindEnvDSL(dslKey string, originalKey string) (interface{}, error) {
dslKey = dslKey[2 : len(dslKey)-1]
envKeyDefaultValue := strings.Split(dslKey, ":")
if len(envKeyDefaultValue) > 2 {
return nil, errors.New("can't read environment for illegal key:" + dslKey)
}
envKey := envKeyDefaultValue[0] // ${ENV:DEFAULT} [0] is key of the env
c.gRWLock.Lock()
// written lock
_ = viper.BindEnv(envKey) // binding environment for the env key
c.gRWLock.Unlock()
envValue := viper.Get(envKey) // get value of env
dslValue := envValue // dsl value is env value by default
// if env value is nil and dsl parameters length > 1
if envValue == nil && len(envKeyDefaultValue) > 1 {
dslValue = envKeyDefaultValue[1]
}
if originalKey != "" {
c.gRWLock.Lock()
c.config.Set(originalKey, dslValue)
c.gRWLock.Unlock()
}
return dslValue, nil
}

func (c *Configuration) assertDSL(key string) bool {
if len(key) < 2 {
return false
}
prefix := key[0:2]
last := key[len(key)-1:]
if !(prefix == "${" && last == "}") {
return false
}
return true
}
6 changes: 3 additions & 3 deletions abstractions/hostenv/host_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hostenv

type HostConfig struct {
Name string `mapstructure:"name"`
Metadata string `mapstructure:"metadata"`
Server WebServerConfig `mapstructure:"server"`
Name string `mapstructure:"name" config:"name"`
Metadata string `mapstructure:"metadata" config:"metadata"`
Server WebServerConfig `mapstructure:"server" config:"server"`
}
4 changes: 2 additions & 2 deletions abstractions/hostenv/httpserver_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ package hostenv
type HttpServerConfig struct {
IsTLS bool
Addr string
CertFile string `mapstructure:"cert"`
KeyFile string `mapstructure:"key"`
CertFile string `mapstructure:"cert" config:"cert"`
KeyFile string `mapstructure:"key" config:"key"`
}
14 changes: 7 additions & 7 deletions abstractions/hostenv/webserver_config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package hostenv

type WebServerConfig struct {
ServerType string `mapstructure:"type"`
Address string `mapstructure:"address"`
MaxRequestSize int64 `mapstructure:"max_request_size"`
Static StaticConfig `mapstructure:"static"`
Tls HttpServerConfig `mapstructure:"tls"`
ServerType string `mapstructure:"type" config:"type"`
Address string `mapstructure:"address" config:"address"`
MaxRequestSize int64 `mapstructure:"max_request_size" config:"max_request_size"`
Static StaticConfig `mapstructure:"static" config:"static"`
Tls HttpServerConfig `mapstructure:"tls" config:"tls"`
}

type StaticConfig struct {
Patten string `mapstructure:"patten"`
WebRoot string `mapstructure:"webroot"`
Patten string `mapstructure:"patten" config:"patten"`
WebRoot string `mapstructure:"webroot" config:"webroot"`
}
3 changes: 2 additions & 1 deletion examples/console/config_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ yoyogo:
name: console_demo
metadata: "dev"
server:
type: "console"
type: "console"
env: ${MAVEN_HOME}
4 changes: 3 additions & 1 deletion examples/simpleweb/config_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ yoyogo:
type: "fasthttp"
address: ":8080"
path: "app"
max_request_size: 31457280
max_request_size: 209615700
session:
name: "YOYOGO_SESSIONID"
timeout: 3600
Expand Down Expand Up @@ -101,8 +101,10 @@ yoyogo:
username: root
password: 1234abcd
debug: false
env: ${USER}
redis:
name: reids1
url: m.yoyogo.run:6379
password: 123.com
db: 0
env: ${MAVEN_HOME}
2 changes: 1 addition & 1 deletion examples/simpleweb/config_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ yoyogo:
type: "fasthttp"
address: ":8080"
path: "app"
max_request_size: 2096157
max_request_size: 209615700
session:
name: "YOYOGO_SESSIONID"
timeout: 3600
Expand Down
2 changes: 1 addition & 1 deletion examples/simpleweb/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ yoyogo:
type: "fasthttp"
address: ":8080"
path: "app"
max_request_size: 2096157
max_request_size: 209615700
session:
name: "YOYOGO_SESSIONID"
timeout: 3600
Expand Down
7 changes: 7 additions & 0 deletions examples/simpleweb/contollers/dbcontroller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package contollers

import (
"fmt"
"github.com/yoyofx/yoyogo/abstractions"
"github.com/yoyofx/yoyogo/pkg/cache/redis"
"github.com/yoyofx/yoyogo/pkg/configuration"
Expand All @@ -24,6 +25,12 @@ func (controller DbController) TestConfigObject() mvc.ApiResult {
return mvc.Success(myconfig)
}

func (controller DbController) PostFile(ctx *context.HttpContext) mvc.ApiResult {
file, _, _ := ctx.Input.FormFile("file")
fmt.Println(file)
return mvc.ApiResult{}
}

func (controller DbController) GetMysql(ctx *context.HttpContext) mvc.ApiResult {
var db *gorm.DB
_ = ctx.RequiredServices.GetService(&db)
Expand Down
8 changes: 7 additions & 1 deletion examples/simpleweb/contollers/usercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package contollers

import (
"fmt"
"github.com/yoyofx/yoyogo/abstractions"
"github.com/yoyofx/yoyogo/abstractions/servicediscovery"
"github.com/yoyofx/yoyogo/web/actionresult"
"github.com/yoyofx/yoyogo/web/binding"
Expand All @@ -17,6 +18,7 @@ type UserController struct {

userAction models.IUserAction
discoveryClient servicediscovery.IServiceDiscovery
config abstractions.IConfiguration
}

func NewUserController(userAction models.IUserAction, sd servicediscovery.IServiceDiscovery) *UserController {
Expand Down Expand Up @@ -124,13 +126,17 @@ func (controller UserController) JsonBinding(ctx *context.HttpContext) mvc.ApiRe
}

//FromQuery
func (controller UserController) QueryBinding(ctx *context.HttpContext) mvc.ApiResult {
func (controller UserController) GetQueryBinding(ctx *context.HttpContext) mvc.ApiResult {
fmt.Println("进入方法")
fmt.Println(controller.config.Get("env"))

userInfo := &UserInfo{}
err := ctx.BindWith(userInfo, binding.Query)
if err != nil {
return controller.Fail(err.Error())
}
return controller.OK(userInfo)

}

type UploadForm struct {
Expand Down
2 changes: 1 addition & 1 deletion examples/simpleweb/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/copier v0.3.2 // indirect
github.com/jinzhu/copier v0.3.5 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.2 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
Expand Down
2 changes: 2 additions & 0 deletions examples/simpleweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func main() {
func CreateCustomBuilder() *abstractions.HostBuilder {
//config := nacosconfig.RemoteConfig("config")
//config := apollo.RemoteConfig("config")

config := configuration.LocalConfig("config")

return web.NewWebHostBuilder().
UseConfiguration(config).
Configure(func(app *web.ApplicationBuilder) {
Expand Down
3 changes: 2 additions & 1 deletion examples/simpleweb/models/myconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ type MyConfig struct {
Name string
Url string
UserName string
Password string //`mapstructure:"password"
Password string
Debug bool
Env string
}

func (config MyConfig) GetSection() string {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/hashicorp/consul/api v1.1.0
github.com/hudl/fargo v1.3.0
github.com/jinzhu/copier v0.3.2
github.com/jinzhu/copier v0.3.5
github.com/magiconair/properties v1.8.5
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/nacos-group/nacos-sdk-go v1.0.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag=
github.com/jinzhu/copier v0.3.2 h1:QdBOCbaouLDYaIPFfi1bKv5F5tPpeTwXe4sD0jqtz5w=
github.com/jinzhu/copier v0.3.2/go.mod h1:24xnZezI2Yqac9J61UC6/dG/k76ttpq0DdJI3QmUvro=
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
Expand Down
4 changes: 2 additions & 2 deletions grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
type Server struct {
IsTLS bool
Addr string
CertFile string `mapstructure:"cert"`
KeyFile string `mapstructure:"key"`
CertFile string `mapstructure:"cert" config:"cert"`
KeyFile string `mapstructure:"key" config:"key"`
serverContext *ServerBuilderContext
}

Expand Down
Loading

0 comments on commit 4485272

Please sign in to comment.