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,52 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"mysql_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/infra/databus/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"mysql.go",
],
importpath = "go-common/app/infra/databus/dao",
tags = ["automanaged"],
deps = [
"//app/infra/databus/conf:go_default_library",
"//app/infra/databus/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/log: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"],
)

View File

@@ -0,0 +1,33 @@
package dao
import (
"context"
"go-common/app/infra/databus/conf"
"go-common/library/database/sql"
)
// Dao mysql struct
type Dao struct {
db *sql.DB
}
// New new a Dao and return
func New(c *conf.Config) (d *Dao) {
d = &Dao{
db: sql.NewMySQL(c.MySQL),
}
return
}
// Ping ping mysql
func (d *Dao) Ping(c context.Context) error {
return d.db.Ping(c)
}
// Close release mysql connection
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
}

View File

@@ -0,0 +1,34 @@
package dao
import (
"flag"
"os"
"testing"
"go-common/app/infra/databus/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.common-arch.databus")
flag.Set("conf_token", "c61dd278f4e311e796e96aeec6da2455")
flag.Set("tree_id", "2698")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,52 @@
package dao
import (
"context"
"go-common/app/infra/databus/model"
"go-common/library/log"
)
const (
_getAuthSQL = `SELECT auth.group_name,auth.operation,app.app_key,app.app_secret,auth.topic,app.cluster
FROM auth LEFT JOIN app ON auth.app_id=app.id`
_getAuth2SQL = `SELECT auth2.group,auth2.operation,app2.app_key,app2.app_secret,auth2.number,topic.topic,topic.cluster
FROM auth2 LEFT JOIN app2 On auth2.app_id=app2.id LEFT JOIN topic On topic.id=auth2.topic_id WHERE auth2.app_id!=0 AND auth2.is_delete=0`
)
// Auth verify group,topic,key
func (d *Dao) Auth(c context.Context) (auths map[string]*model.Auth, err error) {
auths = make(map[string]*model.Auth)
// auth
rows, err := d.db.Query(c, _getAuthSQL)
if err != nil {
log.Error("getAuthStmt.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
a := &model.Auth{}
if err = rows.Scan(&a.Group, &a.Operation, &a.Key, &a.Secret, &a.Topic, &a.Cluster); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
auths[a.Group] = a
}
// auth2
rows2, err := d.db.Query(c, _getAuth2SQL)
if err != nil {
log.Error("getAuthStmt.Query error(%v)", err)
return
}
defer rows2.Close()
for rows2.Next() {
a := &model.Auth{}
if err = rows2.Scan(&a.Group, &a.Operation, &a.Key, &a.Secret, &a.Batch, &a.Topic, &a.Cluster); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
auths[a.Group] = a
}
return
}

View File

@@ -0,0 +1,21 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAuth(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("Auth", t, func(ctx convey.C) {
auths, err := d.Auth(c)
ctx.Convey("Then err should be nil.auths should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(auths, convey.ShouldNotBeNil)
})
})
}