Skip to content

Commit

Permalink
fix: update
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Mar 11, 2021
1 parent 1bb24e7 commit dd3be34
Show file tree
Hide file tree
Showing 75 changed files with 2,648 additions and 1,018 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ proto: clear gen
-I${GOPATH}/src/github.com/googleapis/googleapis \
-I${GOPATH}/src/github.com/gogo/protobuf \
--go_out=plugins=grpc:. \
--grpc-gateway_out=. \
--grpc-gateway_opt=paths=source_relative \
--grpc-gateway_opt=logtostderr=true \
--golug_out=. \
example/proto/hello/*

Expand All @@ -45,6 +48,9 @@ proto: clear gen
-I${GOPATH}/src/github.com/googleapis/googleapis \
-I${GOPATH}/src/github.com/gogo/protobuf \
--go_out=plugins=grpc:. \
--grpc-gateway_out=. \
--grpc-gateway_opt=paths=source_relative \
--grpc-gateway_opt=logtostderr=true \
--golug_out=. \
example/proto/login/*

Expand Down
11 changes: 5 additions & 6 deletions broker/abc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import (
"context"
)

type Factory func(cfg map[string]interface{}) (Broker, error)
type Broker interface {
Publish(topic string, msg *Message, opts *PubOpts) error
Subscribe(topic string, handler Handler, opts *SubOpts) error
Start() error
Stop() error
Name() string
Pub(topic string, msg *Message, opts *PubOpts) error
Sub(topic string, handler Handler, opts *SubOpts) error
String() string
}

type PubOpts struct {
Context context.Context
Ctx context.Context
}

type SubOpts struct {
Expand Down
3 changes: 3 additions & 0 deletions broker/broker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
broker:
default:
driver: "nsq"
11 changes: 11 additions & 0 deletions broker/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
package broker

var Name = "broker"
var cfgList = make(map[string]Cfg)

type Cfg struct {
Driver string `json:"driver"`
}

func GetDefaultCfg() Cfg {
return Cfg{
Driver: "nsq",
}
}
30 changes: 30 additions & 0 deletions broker/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package broker

import (
"github.com/pubgo/golug/config"
"github.com/pubgo/golug/plugin"
"github.com/pubgo/xerror"
)

func onInit(ent interface{}) {
config.Decode(Name, &cfgList)

for name, cfg := range cfgList {
driver := cfg.Driver
xerror.Assert(driver == "", "broker driver is null")
xerror.Assert(!factories.Has(driver), "broker driver %s not found", driver)

fc := factories.Get(driver).(Factory)
var brk = xerror.PanicErr(fc(config.Map(Name, name))).(Broker)

xerror.Assert(brokers.Has(name), "broker %s driver %s already exists", name, driver)
brokers.Set(name, brk)
}
}

func init() {
plugin.Register(&plugin.Base{
Name: Name,
OnInit: onInit,
})
}
20 changes: 9 additions & 11 deletions broker/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ package broker
import (
"github.com/pubgo/golug/consts"
"github.com/pubgo/golug/types"
"github.com/pubgo/x/stack"
"github.com/pubgo/xerror"
)

var brokerMap types.SMap
var factories types.SMap
var brokers types.SMap

func List() (dt map[string]Broker) { xerror.Panic(brokerMap.Map(&dt)); return }

func Register(name string, broker Broker) {
xerror.Assert(name == "" || broker == nil || broker.Name() == "", "[broker,name] should not be null")
xerror.Assert(brokerMap.Has(name), "[broker] %s already exists", name)

brokerMap.Set(name, broker)
func List() (dt map[string]Factory) { xerror.Panic(factories.Map(&dt)); return }
func Register(name string, broker Factory) {
xerror.Assert(name == "" || broker == nil, "[broker,name] should not be null")
xerror.Assert(factories.Has(name), "[broker] %s already exists, refer: %s", name, stack.Func(factories.Get(name)))
factories.Set(name, broker)
}

func Get(names ...string) Broker {
name := consts.GetDefault(names...)
val, ok := brokerMap.Load(name)
val, ok := brokers.Load(consts.GetDefault(names...))
if !ok {
return nil
}

return val.(Broker)
}
18 changes: 14 additions & 4 deletions broker/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ package broker

import (
"github.com/pubgo/golug/tracelog"
"github.com/pubgo/x/stack"
"github.com/pubgo/xerror"
)

func init() {
tracelog.Watch(Name, func() interface{} {
tracelog.Watch(Name+"_factory", func() interface{} {
var data = make(map[string]string)
for k, v := range List() {
data[k] = v.Name()
}
xerror.Panic(factories.Each(func(name string, fc Factory) {
data[name] = stack.Func(fc)
}))
return data
})

tracelog.Watch(Name+"_broker", func() interface{} {
var data = make(map[string]string)
xerror.Panic(brokers.Each(func(name string, fc Broker) {
data[name] = fc.String()
}))
return data
})
}
2 changes: 1 addition & 1 deletion client/etcdv3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const Name = "etcdv3"

var cfgList []Cfg
var cfgList = make(map[string]Cfg)

type Cfg struct {
Endpoints []string `json:"endpoints"`
Expand Down
4 changes: 4 additions & 0 deletions client/etcdv3/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
etcdv3:
default:
endpoints: "localhost:2379"
name: "test"
4 changes: 2 additions & 2 deletions client/etcdv3/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
func onInit(ent interface{}) {
config.Decode(Name, &cfgList)

for _, cfg := range cfgList {
for name, cfg := range cfgList {
// etcd config处理
cfg := xerror.PanicErr(cfgMerge(cfg)).(Cfg)
xerror.Panic(initClient(consts.GetDefault(cfg.Name), cfg))
xerror.Panic(initClient(consts.GetDefault(name), cfg))
}
}

Expand Down
12 changes: 2 additions & 10 deletions client/grpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ type client struct {
opts []grpc.DialOption
}

func (t *client) New() (*grpc.ClientConn, error) {
conn, err := dial(t.target, t.opts...)
if err != nil {
return nil, xerror.WrapF(err, "dial %s error\n", t.target)
}
return conn, nil
}

func (t *client) getClient() *grpc.ClientConn {
if val, ok := clients.Load(t.service); ok {
if val.(*grpc.ClientConn).GetState() == connectivity.Ready {
Expand All @@ -58,9 +50,9 @@ func (t *client) Get() (*grpc.ClientConn, error) {
return client, nil
}

conn, err := t.New()
conn, err := dial(t.target, t.opts...)
if err != nil {
return nil, err
return nil, xerror.WrapF(err, "dial %s error\n", t.service)
}

clients.Store(t.service, conn)
Expand Down
4 changes: 2 additions & 2 deletions cmds/golug/initcmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package initcmd

import (
"fmt"
"github.com/pubgo/golug/config"
"io/ioutil"
"os"
"path/filepath"

"github.com/mitchellh/go-homedir"
"github.com/pubgo/golug/golug"
"github.com/pubgo/golug/gutils"
"github.com/pubgo/xerror"
"github.com/spf13/cobra"
Expand All @@ -17,7 +17,7 @@ func New() *cobra.Command {
var cmd = &cobra.Command{Use: "init"}

cmd.Run = func(cmd *cobra.Command, args []string) {
home := filepath.Join(xerror.PanicStr(homedir.Dir()), "."+golug.Project, "config")
home := filepath.Join(xerror.PanicStr(homedir.Dir()), "."+config.Project, "config")
xerror.Panic(os.MkdirAll(home, 0755))

fmt.Println("config home:", home)
Expand Down
43 changes: 1 addition & 42 deletions cmds/protoc-gen-golug/tpl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 16 additions & 7 deletions golug/app.go → config/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golug
package config

import (
"os"
Expand All @@ -16,6 +16,7 @@ import (
var (
IsBlock = true
Domain = "golug"
EnvPrefix = "golug"
CatchSigpipe = true
Trace = false
Home = filepath.Join(xerror.PanicStr(filepath.Abs(filepath.Dir(""))), "home")
Expand All @@ -34,19 +35,27 @@ var (
lower = strings.ToLower
)

// 从环境变量中获取系统默认值
func init() {
// 从环境变量中获取系统默认值
// 获取系统默认的前缀, 环境变量前缀等
env.GetVal(&Domain, "env_prefix", "domain", "golug_domain", "golug_prefix")
env.GetWith(&EnvPrefix, "env_prefix", "golug_prefix")
if EnvPrefix = trim(lower(EnvPrefix)); EnvPrefix == "" {
EnvPrefix = "golug"
xlog.Warnf("[env_prefix] prefix should be set, default: %s", EnvPrefix)
}
env.Prefix = EnvPrefix

env.GetWith(&Domain, "domain", "golug_domain")
if Domain = trim(lower(Domain)); Domain == "" {
Domain = "golug"
xlog.Warnf("[domain] prefix should be set, default: %s", Domain)
}
env.Prefix = Domain

env.GetVal(&Home, "home", "cfg_dir", "config_path")
env.GetVal(&Project, "project", "server_name")
env.GetVal(&Mode, "mode", "run_mode")
env.GetWith(&CfgType, "cfg_type")
env.GetWith(&CfgName, "cfg_name")
env.GetWith(&Home, "home", "cfg_dir", "config_path")
env.GetWith(&Project, "project", "server_name")
env.GetWith(&Mode, "mode", "run_mode")
env.GetBoolVal(&Trace, "trace")
}

Expand Down
Loading

0 comments on commit dd3be34

Please sign in to comment.