Merge branch 'master' of github.com:bilibili/kratos into go-common/testing_20199197322

pull/364/head
Otokaze 5 years ago
commit 1cc95ce29e
  1. 75
      doc/wiki-cn/config-paladin.md
  2. 2
      pkg/naming/discovery/discovery.go
  3. 9
      tool/kratos-protoc/protoc.go
  4. 2
      tool/kratos/new.go
  5. 4
      tool/kratos/template.go
  6. 9
      tool/kratos/tool.go

@ -46,9 +46,10 @@ func TestMain(t *testing.M) {
### example main ### example main
```go ```go
# main.go // main.go
func main() { func main() {
# 初始化paladin flag.Parse()
// 初始化paladin
if err := paladin.Init(); err != nil { if err := paladin.Init(); err != nil {
panic(err) panic(err)
} }
@ -58,28 +59,31 @@ func main() {
``` ```
### example HTTP/gRPC ### example HTTP/gRPC
```go ```toml
# http.toml # http.toml
[server] [server]
addr = "0.0.0.0:9000" addr = "0.0.0.0:9000"
timeout = "1s" timeout = "1s"
# server.go ```
```go
// server.go
func NewServer() { func NewServer() {
# 默认配置用nil,这时读取HTTP/gRPC构架中的flag或者环境变量(可能是docker注入的环境变量,默认端口:8000/9000) // 默认配置用nil,这时读取HTTP/gRPC构架中的flag或者环境变量(可能是docker注入的环境变量,默认端口:8000/9000)
engine := bm.DefaultServer(nil) engine := bm.DefaultServer(nil)
# 除非自己要替换了配置,用http.toml // 除非自己要替换了配置,用http.toml
var bc struct { var bc struct {
Server *bm.ServerConfig Server *bm.ServerConfig
} }
if err = paladin.Get("http.toml").UnmarshalTOML("server", &bc); err != nil { if err := paladin.Get("http.toml").UnmarshalTOML(&bc); err != nil {
// 不存在时,将会为nil使用默认配置 // 不存在时,将会为nil使用默认配置
if err != paladin.ErrNotExist { if err != paladin.ErrNotExist {
panic(err) panic(err)
} }
} }
engine := bm.DefaultServer(conf) engine := bm.DefaultServer(bc.Server)
} }
``` ```
@ -87,25 +91,28 @@ func NewServer() {
```go ```go
# service.go # service.go
type Service struct { type Service struct {
ac *paladin.Map ac *paladin.Map
} }
func New() *Service { func New() *Service {
# paladin.Map 通过atomic.Value支持自动热加载 // paladin.Map 通过atomic.Value支持自动热加载
var ac = new(paladin.TOML) var ac = new(paladin.TOML)
if err := paladin.Watch("application.toml", ac); err != nil { if err := paladin.Watch("application.toml", ac); err != nil {
panic(err) panic(err)
} }
s := &Service{ s := &Service{
ac : ac; ac: ac,
} }
return s return s
} }
func (s *Service) Test() { func (s *Service) Test() {
switch, err := s.ac.Bool("switch") sw, err := s.ac.Get("switch").Bool()
if err != nil { if err != nil {
// TODO // TODO
} }
# or use default value
switch := paladin.Bool(s.ac.Value("switch"), false) // or use default value
sw := paladin.Bool(s.ac.Get("switch"), false)
} }
``` ```

@ -102,7 +102,7 @@ type appInfo struct {
} }
func fixConfig(c *Config) error { func fixConfig(c *Config) error {
if len(c.Nodes) == 0 { if len(c.Nodes) == 0 && env.DiscoveryNodes != "" {
c.Nodes = strings.Split(env.DiscoveryNodes, ",") c.Nodes = strings.Split(env.DiscoveryNodes, ",")
} }
if c.Region == "" { if c.Region == "" {

@ -144,14 +144,7 @@ func latestKratos() (string, error) {
} }
func gopath() (gp string) { func gopath() (gp string) {
var gopaths []string gopaths := strings.Split(os.Getenv("GOPATH"), string(filepath.ListSeparator))
switch runtime.GOOS {
case "windows":
gopaths = strings.Split(os.Getenv("GOPATH"), ";")
default:
gopaths = strings.Split(os.Getenv("GOPATH"), ":")
}
if len(gopaths) == 1 && gopaths[0] != "" { if len(gopaths) == 1 && gopaths[0] != "" {
return gopaths[0] return gopaths[0]

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"path/filepath"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -25,6 +26,7 @@ func runNew(ctx *cli.Context) error {
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
p.Path = path.Join(pwd, p.Name) p.Path = path.Join(pwd, p.Name)
} }
p.Path = filepath.FromSlash(p.Path)
// Create a project // Create a project
if err := create(); err != nil { if err := create(); err != nil {
return err return err

@ -76,6 +76,8 @@ import (
"{{.ModuleName}}/internal/service" "{{.ModuleName}}/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log" "github.com/bilibili/kratos/pkg/log"
_ "go.uber.org/automaxprocs"
) )
func main() { func main() {
@ -127,6 +129,8 @@ import (
"{{.ModuleName}}/internal/service" "{{.ModuleName}}/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log" "github.com/bilibili/kratos/pkg/log"
_ "go.uber.org/automaxprocs"
) )
func main() { func main() {

@ -186,14 +186,7 @@ func (t Tool) installed() bool {
} }
func gopath() (gp string) { func gopath() (gp string) {
var gopaths []string gopaths := strings.Split(os.Getenv("GOPATH"), string(filepath.ListSeparator))
switch runtime.GOOS {
case "windows":
gopaths = strings.Split(os.Getenv("GOPATH"), ";")
default:
gopaths = strings.Split(os.Getenv("GOPATH"), ":")
}
if len(gopaths) == 1 && gopaths[0] != "" { if len(gopaths) == 1 && gopaths[0] != "" {
return gopaths[0] return gopaths[0]

Loading…
Cancel
Save