Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["conf.go"],
importpath = "go-common/app/tool/saga/conf",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/tool/saga/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/cache/redis:go_default_library",
"//library/conf:go_default_library",
"//library/database/hbase.v2:go_default_library",
"//library/database/orm:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/trace:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/BurntSushi/toml:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["conf_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

197
app/tool/saga/conf/conf.go Normal file
View File

@@ -0,0 +1,197 @@
package conf
import (
"flag"
"regexp"
"sync"
"time"
"go-common/app/tool/saga/model"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
"go-common/library/conf"
"go-common/library/database/hbase.v2"
"go-common/library/database/orm"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/trace"
xtime "go-common/library/time"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
)
const (
configKey = "saga.toml"
)
var (
confPath string
client *conf.Client
// Conf store the global config
Conf = &Config{}
reload chan bool
)
func init() {
flag.StringVar(&confPath, "conf", "", "config path")
reload = make(chan bool, 10)
}
// Config def.
type Config struct {
Tracer *trace.Config
BM *bm.ServerConfig
HTTPClient *bm.ClientConfig
Memcache *Memcache
Redis *redis.Config
HBase *HBaseConfig
Log *log.Config
Property *Property
sync.RWMutex
// orm
ORM *orm.Config
}
// Memcache config.
type Memcache struct {
MR *memcache.Config
MRRecordExpire xtime.Duration
}
// HBaseConfig for new hbase client.
type HBaseConfig struct {
*hbase.Config
WriteTimeout xtime.Duration
ReadTimeout xtime.Duration
}
// Property config for biz logic.
type Property struct {
TaskInterval xtime.Duration // 任务轮询时间
TaskTimeout xtime.Duration // 任务超时时间
PollPipeline xtime.Duration //pipeline轮询间隔时间
Gitlab *struct {
API string // gitlab api host
Token string // saga 账户 access token
}
WebHooks []*model.WebHook
Mail *struct {
Host string
Port int
Address string
Pwd string
Name string
}
HealthCheck *struct {
CheckCron string
AlertAddrs []*model.MailAddress
}
ReportRequiredVisible *struct {
CheckCron string
AlertAddrs []*model.MailAddress
}
SyncContact *struct {
CheckCron string
}
UT *struct {
Rate float64
}
Wechat *model.AppConfig
Contact *model.AppConfig
Repos []*model.RepoConfig
}
// Init init conf.
func Init() (err error) {
if confPath == "" {
return configCenter()
}
if _, err = toml.DecodeFile(confPath, &Conf); err != nil {
log.Error("toml.DecodeFile(%s) err(%+v)", confPath, err)
return
}
Conf = doDefault(Conf)
return
}
func configCenter() (err error) {
if client, err = conf.New(); err != nil {
panic(err)
}
if err = load(); err != nil {
return
}
//client.WatchAll()
client.Watch(configKey)
go func() {
for range client.Event() {
log.Info("config reload")
if err = load(); err != nil {
log.Error("config reload error (+%v)", err)
} else {
reload <- true
}
}
}()
return
}
func load() (err error) {
var (
s string
ok bool
tmpConf *Config
)
if s, ok = client.Value(configKey); !ok {
err = errors.Errorf("load config center error [%s]", configKey)
return
}
if _, err = toml.Decode(s, &tmpConf); err != nil {
err = errors.Wrapf(err, "could not decode config err(%+v)", err)
return
}
Conf = doDefault(tmpConf)
return
}
func doDefault(c *Config) *Config {
if int64(c.Property.TaskInterval) == 0 {
c.Property.TaskInterval = xtime.Duration(3 * time.Second)
}
if int64(c.Property.TaskTimeout) == 0 {
c.Property.TaskTimeout = xtime.Duration(time.Minute)
}
if int64(c.Property.PollPipeline) == 0 {
c.Property.PollPipeline = xtime.Duration(10 * time.Second)
}
for _, r := range c.Property.Repos {
if r.GName == "" {
r.GName = r.Name
}
if r.Language == "" {
r.Language = "any"
}
if r.MinReviewer < 0 {
r.MinReviewer = 0
}
if r.LockTimeout == 0 {
r.LockTimeout = 600
}
if len(r.AuthBranches) == 0 {
r.AuthBranches = []string{"master"}
}
if len(r.TargetBranches) == 0 {
r.TargetBranches = r.AuthBranches
}
for _, b := range r.TargetBranches {
r.TargetBranchRegexes = append(r.TargetBranchRegexes, regexp.MustCompile(b))
}
}
return c
}
// ReloadEvents return the reload chan
func ReloadEvents() <-chan bool {
return reload
}

View File

@@ -0,0 +1,33 @@
package conf
import (
"flag"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func init() {
var err error
flag.Set("conf", "../cmd/saga-test.toml")
if err = Init(); err != nil {
panic(err)
}
}
func TestConf(t *testing.T) {
Convey("test conf", t, func() {
So(Conf, ShouldNotBeNil)
So(Conf.Property, ShouldNotBeNil)
So(Conf.Property.Gitlab, ShouldNotBeNil)
So(Conf.Property.Repos, ShouldNotBeNil)
So(Conf.Property.Repos[0].MinReviewer, ShouldEqual, 1)
So(Conf.Property.Repos[1].MinReviewer, ShouldEqual, 0)
So(Conf.Property.Repos[0].AuthBranches[0], ShouldEqual, "master")
So(Conf.Property.Repos[1].AuthBranches[0], ShouldEqual, "master")
So(Conf.Property.Mail, ShouldNotBeNil)
So(Conf.Property.HealthCheck, ShouldNotBeNil)
So(Conf.Property.HealthCheck.AlertAddrs, ShouldNotBeEmpty)
So(Conf.Property.Wechat, ShouldNotBeNil)
})
}