go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

Better loading of configuration

Open a0v0 opened this issue 3 years ago • 11 comments

  • [x] update json tags
  • [ ] Update goctl template

a0v0 avatar Sep 13 '22 16:09 a0v0

Redis only works if specified from yml like this

Redis:
  Host: redis:6379

or

Redis:
  - Host: redis:6379

someone take a look at this prob

a0v0 avatar Sep 13 '22 17:09 a0v0

also default tag loading is working as expected for rest config but for redis config we have to specify manually each and every field in yaml. Otherwise they are filled with default null values

config.yml

Redis:
  Host: redis:6379
config output

```json
{
...
{
  "Redis": [
    {
      "Host": "redis:6379",
      "Type": "",
      "Pass": "",
      "Tls": false,
      "Weight": 0
    }
  ]
}
...
}

type, weight, have default values but not loaded

a0v0 avatar Sep 13 '22 17:09 a0v0

specifying explicitly like this working great.

Redis:
  Host: redis:6379
  Type: node
  Weight: 100

someone fix this to load default from tags. @kevwan can you take a look at this

a0v0 avatar Sep 13 '22 18:09 a0v0

i think goctl template needs to be changed with this

main.go

```go
import "github.com/num30/config"
func main() {
	var c cfg.Config

	// search for config file in the etc folder without caring for the file extension
	// Prority: flags > env > config file
	err := config.NewConfReader("config").WithSearchDirs("etc", ".").Read(&c)
	if err != nil {
		log.Fatalf("error reading config: %v", err)
	}

// ....
}

a0v0 avatar Sep 14 '22 18:09 a0v0

also it would be nice if goctl generates a config.yml file instead of <servicename>.yaml file

a0v0 avatar Sep 14 '22 18:09 a0v0

I don't think the change works well for the existing code.

kevwan avatar Sep 17 '22 11:09 kevwan

I don't think the change works well for the existing code.

why not? only thing to change is just the tags and main.go code. Everything works just as before

a0v0 avatar Sep 17 '22 15:09 a0v0

@kevwan can you explain what is the usage of optional=CertFile.

CertFile           string `json:",optional"`
CertKeyFile        string `json:",optional=CertFile"`
CACertFile         string `json:",optional=CertFile"`

a0v0 avatar Sep 17 '22 16:09 a0v0

@kevwan can you explain what is the usage of optional=CertFile.

CertFile           string `json:",optional"`
CertKeyFile        string `json:",optional=CertFile"`
CACertFile         string `json:",optional=CertFile"`

It means that if CertFile has value, then CertKeyFile and CACertFile have values.

kevwan avatar Sep 18 '22 06:09 kevwan

@kevwan can you explain what is the usage of optional=CertFile.

CertFile           string `json:",optional"`
CertKeyFile        string `json:",optional=CertFile"`
CACertFile         string `json:",optional=CertFile"`

It means that if CertFile has value, then CertKeyFile and CACertFile have values.

So, If I provide certfile, then certkeyfile and cacertfile are also mandatory or optional.

a0v0 avatar Sep 18 '22 06:09 a0v0

@kevwan Fixed linux lint error. Please try and run the workflow again

a0v0 avatar Sep 18 '22 07:09 a0v0

@kevwan this is complete

a0v0 avatar Oct 04 '22 02:10 a0v0

@kevwan please take a look

a0v0 avatar Oct 20 '22 18:10 a0v0

This PR is important for "12 Twelve Factor" compliance. Please approve this PR or close it, so I know whether I can adopt this framework. 谢谢

Minger avatar Oct 21 '22 04:10 Minger

@kevwan please try to merge it in the next release. I've been waiting for a month for better config loading

a0v0 avatar Oct 21 '22 18:10 a0v0