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,64 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"mng_auth.go",
"mng_role.go",
"mng_user.go",
"publish.go",
],
importpath = "go-common/app/admin/main/macross/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/macross/conf:go_default_library",
"//app/admin/main/macross/model/manager:go_default_library",
"//app/admin/main/macross/model/publish: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",
"//app/admin/main/macross/dao/oss:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"mng_auth_test.go",
"mng_role_test.go",
"mng_user_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/macross/conf:go_default_library",
"//library/database/sql:go_default_library",
"//vendor/github.com/bouk/monkey:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,41 @@
package dao
import (
"context"
"go-common/app/admin/main/macross/conf"
"go-common/library/database/sql"
"go-common/library/log"
)
// Dao macross dao.
type Dao struct {
// conf
c *conf.Config
// db
db *sql.DB
}
// New dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.DB.Macross),
}
return
}
// Ping dao.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.Ping(c); err != nil {
log.Error("d.db error(%v)", err)
}
return
}
// Close close kafka connection.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
}

View File

@@ -0,0 +1,58 @@
package dao
import (
"context"
"flag"
"os"
"testing"
"go-common/app/admin/main/macross/conf"
"go-common/library/database/sql"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.macross")
flag.Set("conf_token", "5cc0121bad1156f7eb0335fbb44ce0ff")
flag.Set("tree_id", "6798")
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")
} else {
flag.Set("conf", "../cmd/macross-admin-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func WithReopenDB(f func(d *Dao)) func() {
return func() {
convey.Reset(func() {
d.db = sql.NewMySQL(d.c.DB.Macross)
})
f(d)
}
}
func TestDaoPing(t *testing.T) {
convey.Convey("Ping", t, func(ctx convey.C) {
err := d.Ping(context.TODO())
ctx.Convey("Err should be nil", func() {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,138 @@
package dao
import (
"context"
model "go-common/app/admin/main/macross/model/manager"
"go-common/library/log"
)
const (
// load cache(get all).
_authRelationSQL = `SELECT rid,auth_id FROM auth_relation`
_authsSQL = `SELECT id,system,name,flag,ctime,mtime FROM auth`
// auth.
_inAuthSQL = `INSERT INTO auth (system,name,flag) VALUES(?,?,?)`
_upAuthSQL = `UPDATE auth SET name=? WHERE id=?`
_delAuthSQL = `DELETE FROM auth WHERE id=?`
// auth_relation.
_inAuthRelationSQL = `INSERT INTO auth_relation (rid,auth_id) VALUES(?,?)`
_delAuthRelationSQL = `DELETE FROM auth_relation WHERE rid=? AND auth_id=?`
_cleanRelationByAuth = "DELETE FROM auth_relation WHERE auth_id=?"
)
// Auths select all auth from db.
func (d *Dao) Auths(c context.Context) (res map[string]map[int64]*model.Auth, err error) {
rows, err := d.db.Query(c, _authsSQL)
if err != nil {
log.Error("Auths d.db.Query(%d) error(%v)", err)
return
}
defer rows.Close()
res = make(map[string]map[int64]*model.Auth)
for rows.Next() {
var (
auths map[int64]*model.Auth
ok bool
)
auth := &model.Auth{}
if err = rows.Scan(&auth.AuthID, &auth.System, &auth.AuthName, &auth.AuthFlag, &auth.CTime, &auth.MTime); err != nil {
log.Error("Auths rows.Scan error(%v)", err)
return
}
if auths, ok = res[auth.System]; !ok {
auths = make(map[int64]*model.Auth)
res[auth.System] = auths
}
auths[auth.AuthID] = auth
}
return
}
// AddAuth insert auth.
func (d *Dao) AddAuth(c context.Context, system, authName, authFlag string) (rows int64, err error) {
res, err := d.db.Exec(c, _inAuthSQL, system, authName, authFlag)
if err != nil {
log.Error("AddAuth d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// UpAuth update auth.
func (d *Dao) UpAuth(c context.Context, authName string, authID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _upAuthSQL, authName, authID)
if err != nil {
log.Error("UpAuth d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// DelAuth del auth.
func (d *Dao) DelAuth(c context.Context, authID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delAuthSQL, authID)
if err != nil {
log.Error("DelAuth d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// CleanAuthRelationByAuth del all auth relation by auth.
func (d *Dao) CleanAuthRelationByAuth(c context.Context, authID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _cleanRelationByAuth, authID)
if err != nil {
log.Error("CleanAuthRelationByAuth d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// AuthRelation select all auth_relation from db.
func (d *Dao) AuthRelation(c context.Context) (res map[int64][]int64, err error) {
rows, err := d.db.Query(c, _authRelationSQL)
if err != nil {
log.Error("Roles d.db.Query(%d) error(%v)", err)
return
}
defer rows.Close()
res = make(map[int64][]int64)
for rows.Next() {
var (
roleID, authID int64
)
if err = rows.Scan(&roleID, &authID); err != nil {
log.Error("Roles rows.Scan error(%v)", err)
return
}
res[roleID] = append(res[roleID], authID)
}
return
}
// AddAuthRelation insert auth_relation.
func (d *Dao) AddAuthRelation(c context.Context, roleID, authID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _inAuthRelationSQL, roleID, authID)
if err != nil {
log.Error("AddAuthRelation d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// DelAuthRelation del auth_relation.
func (d *Dao) DelAuthRelation(c context.Context, roleID, authID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delAuthRelationSQL, roleID, authID)
if err != nil {
log.Error("DelAuthRelation d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}

View File

@@ -0,0 +1,55 @@
package dao
import (
"context"
"fmt"
"reflect"
"testing"
xsql "go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestAuths(t *testing.T) {
convey.Convey("Auths", t, func(ctx convey.C) {
ctx.Convey("When everything is correct", func(ctx convey.C) {
res, err := d.Auths(context.Background())
ctx.Convey("Error should be nil, res should not be empty", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeEmpty)
})
})
ctx.Convey("When db.Query gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Query", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (*xsql.Rows, error) {
return nil, fmt.Errorf("db.Query error")
})
defer guard.Unpatch()
_, err := d.Auths(context.Background())
ctx.Convey("Error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
// Set d.close() to get reversal case
func TestAuthRelation(t *testing.T) {
convey.Convey("AuthRelation", t, func(ctx convey.C) {
convey.Convey("When everything is correct,", func(ctx convey.C) {
asgs, err := d.AuthRelation(context.Background())
ctx.Convey("Error should be nil, asgs should not be nil(No Data)", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(asgs, convey.ShouldBeNil)
})
})
convey.Convey("When set db closed", WithReopenDB(func(d *Dao) {
d.Close()
_, err := d.AuthRelation(context.Background())
convey.Convey("Error should not be nil", func(ctx convey.C) {
convey.So(err, convey.ShouldNotBeNil)
})
}))
})
}

View File

@@ -0,0 +1,90 @@
package dao
import (
"context"
model "go-common/app/admin/main/macross/model/manager"
"go-common/library/log"
)
const (
// load cache(get all).
_rolesSQL = `SELECT id,system,name,ctime,mtime FROM role`
// role.
_inRoleSQL = `INSERT INTO role (system,name) VALUES(?,?)`
_upRoleSQL = `UPDATE role SET name=? WHERE id=?`
_delRoleSQL = `DELETE FROM role WHERE id=?`
_cleanRelationByRole = "DELETE FROM auth_relation WHERE rid=?"
)
// Roles select all role from db.
func (d *Dao) Roles(c context.Context) (res map[string]map[int64]*model.Role, err error) {
rows, err := d.db.Query(c, _rolesSQL)
if err != nil {
log.Error("Roles d.db.Query(%d) error(%v)", err)
return
}
defer rows.Close()
res = make(map[string]map[int64]*model.Role)
for rows.Next() {
var (
roles map[int64]*model.Role
ok bool
)
role := &model.Role{}
if err = rows.Scan(&role.RoleID, &role.System, &role.RoleName, &role.CTime, &role.MTime); err != nil {
log.Error("Roles rows.Scan error(%v)", err)
return
}
if roles, ok = res[role.System]; !ok {
roles = make(map[int64]*model.Role)
res[role.System] = roles
}
roles[role.RoleID] = role
}
return
}
// AddRole insert role.
func (d *Dao) AddRole(c context.Context, system, roleName string) (rows int64, err error) {
res, err := d.db.Exec(c, _inRoleSQL, system, roleName)
if err != nil {
log.Error("AddRole d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// UpRole update role.
func (d *Dao) UpRole(c context.Context, roleName string, roleID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _upRoleSQL, roleName, roleID)
if err != nil {
log.Error("UpRole d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// DelRole del role.
func (d *Dao) DelRole(c context.Context, roleID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delRoleSQL, roleID)
if err != nil {
log.Error("DelRole d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// CleanAuthRelationByRole del all auth relation by role.
func (d *Dao) CleanAuthRelationByRole(c context.Context, roleID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _cleanRelationByRole, roleID)
if err != nil {
log.Error("CleanAuthRelationByRole d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}

View File

@@ -0,0 +1,35 @@
package dao
import (
"context"
"fmt"
"reflect"
"testing"
xsql "go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestRoles(t *testing.T) {
convey.Convey("Roles", t, func(ctx convey.C) {
ctx.Convey("When everything is correct", func(ctx convey.C) {
res, err := d.Roles(context.Background())
ctx.Convey("Error should be nil, res should not be empty", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeEmpty)
})
})
ctx.Convey("When db.Query gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Query", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (*xsql.Rows, error) {
return nil, fmt.Errorf("db.Query error")
})
defer guard.Unpatch()
_, err := d.Roles(context.Background())
ctx.Convey("Error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,94 @@
package dao
import (
"context"
model "go-common/app/admin/main/macross/model/manager"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
// load cache(get all).
_usersSQL = `SELECT user.id,user.system,user.name,user.rid,role.name,user.ctime,user.mtime FROM user,role WHERE user.rid=role.id`
_userSQL = `SELECT user.id,user.system,user.name,user.rid,role.name,user.ctime,user.mtime FROM user,role WHERE user.rid=role.id AND user.id=?`
// user.
_inUserSQL = `INSERT INTO user (system,name,rid) VALUES(?,?,?)`
_upUserSQL = `UPDATE user SET name=?,rid=? WHERE id=?`
_delUserSQL = `DELETE FROM user WHERE id=?`
)
// Users select all user from db.
func (d *Dao) Users(c context.Context) (res map[string]map[string]*model.User, err error) {
rows, err := d.db.Query(c, _usersSQL)
if err != nil {
log.Error("UserAll d.db.Query(%d) error(%v)", err)
return
}
defer rows.Close()
res = make(map[string]map[string]*model.User)
for rows.Next() {
var (
users map[string]*model.User
ok bool
)
user := &model.User{}
if err = rows.Scan(&user.UserID, &user.System, &user.UserName, &user.RoleID, &user.RoleName, &user.CTime, &user.MTime); err != nil {
log.Error("Users rows.Scan error(%v)", err)
return
}
if users, ok = res[user.System]; !ok {
users = make(map[string]*model.User)
res[user.System] = users
}
users[user.UserName] = user
}
return
}
// User get user.
func (d *Dao) User(c context.Context, userID int64) (re *model.User, err error) {
row := d.db.QueryRow(c, _userSQL, userID)
re = &model.User{}
if err = row.Scan(&re.UserID, &re.System, &re.UserName, &re.RoleID, &re.RoleName, &re.CTime, &re.MTime); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("User d.db.QueryRow(%d) error(%v)", userID, err)
}
}
return
}
// AddUser insert user.
func (d *Dao) AddUser(c context.Context, roleID int64, system, userName string) (rows int64, err error) {
res, err := d.db.Exec(c, _inUserSQL, system, userName, roleID)
if err != nil {
log.Error("AddUser d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// UpUser update user.
func (d *Dao) UpUser(c context.Context, userID, roleID int64, userName string) (rows int64, err error) {
res, err := d.db.Exec(c, _upUserSQL, userName, roleID, userID)
if err != nil {
log.Error("UpUser d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}
// DelUser del user.
func (d *Dao) DelUser(c context.Context, userID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delUserSQL, userID)
if err != nil {
log.Error("DelUser d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}

View File

@@ -0,0 +1,56 @@
package dao
import (
"context"
"fmt"
"reflect"
"testing"
xsql "go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
func TestUsers(t *testing.T) {
convey.Convey("Users", t, func(ctx convey.C) {
ctx.Convey("When everything is correct", func(ctx convey.C) {
res, err := d.Users(context.Background())
ctx.Convey("Error should be nil, res should not be empty", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeEmpty)
})
})
ctx.Convey("When db.Query gets error", func(ctx convey.C) {
guard := monkey.PatchInstanceMethod(reflect.TypeOf(d.db), "Query", func(_ *xsql.DB, _ context.Context, _ string, _ ...interface{}) (*xsql.Rows, error) {
return nil, fmt.Errorf("db.Query error")
})
defer guard.Unpatch()
_, err := d.Users(context.Background())
ctx.Convey("Error should not be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
// Set d.close() to get reversal case
func TestUser(t *testing.T) {
var id = int64(67)
convey.Convey("User", t, func(ctx convey.C) {
convey.Convey("When everything is correct,", func(ctx convey.C) {
asgs, err := d.User(context.Background(), id)
ctx.Convey("Error should be nil, asgs should not be nil(No Data)", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(asgs, convey.ShouldBeNil)
})
})
convey.Convey("When set db closed", WithReopenDB(func(d *Dao) {
d.Close()
_, err := d.User(context.Background(), id)
convey.Convey("Error should not be nil", func(ctx convey.C) {
convey.So(err, convey.ShouldNotBeNil)
})
}))
})
}

View File

@@ -0,0 +1,33 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/admin/main/macross/dao/oss",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/macross/conf:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/aliyun/aliyun-oss-go-sdk/oss: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,61 @@
package oss
import (
"context"
"io"
"path"
"go-common/app/admin/main/macross/conf"
"go-common/library/log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// Dao macross dao
type Dao struct {
// conf
c *conf.Config
client *oss.Client
}
// New dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
}
client, err := oss.New(d.c.Oss.Endpoint, d.c.Oss.AccessKeyID, d.c.Oss.AccessKeySecret)
if err != nil {
panic(err)
}
d.client = client
return
}
// Put put object into oss.
func (d *Dao) Put(c context.Context, rd io.Reader, apkName string) (uri string, err error) {
bucket, _ := d.client.Bucket(d.c.Oss.Bucket)
uri = path.Join(d.c.Oss.OriginDir, apkName)
// put
if err = bucket.PutObject(uri, rd); err != nil {
log.Error("bucket.PutObject(%s) error(%v)", uri, err)
return
}
uri = path.Join(d.c.Oss.Bucket, uri) // NOTE: begin with '/' when upload success
return
}
// Publish publish object into oss.
func (d *Dao) Publish(objectKey string, destKey string) (uri string, err error) {
bucket, _ := d.client.Bucket(d.c.Oss.Bucket)
_, err = bucket.CopyObject(d.c.Oss.PublishDir+objectKey, d.c.Oss.PublishDir+destKey)
if err != nil {
log.Error("bucket.CopyObject(%s, %s) error(%v)", d.c.Oss.PublishDir+objectKey, d.c.Oss.PublishDir+destKey, err)
return
}
uri = path.Join("/", d.c.Oss.PublishDir+destKey)
return
}
// Close close kafka connection.
func (d *Dao) Close() {
}

View File

@@ -0,0 +1,53 @@
package dao
import (
"bytes"
"context"
"fmt"
"go-common/app/admin/main/macross/model/publish"
"go-common/library/log"
)
const (
_logSharding = 10
// dashborad
_inDashboradSQL = `INSERT INTO dashboard (name,label,commit_info,out_url,coverage_url,text_size_arm64,res_size,extra) VALUES(?,?,?,?,?,?,?,?)`
_inDashboradLogsSQL = `INSERT INTO dashboard_log_%02d (dashboard_id,level,msg) VALUES`
)
func (d *Dao) hitLogs(id int64) int64 {
return id % _logSharding
}
// Dashborad insert dashboard.
func (d *Dao) Dashborad(c context.Context, dashboard *publish.Dashboard) (rows int64, err error) {
res, err := d.db.Exec(c, _inDashboradSQL, dashboard.Name, dashboard.Label, dashboard.Commit, dashboard.OutURL, dashboard.CoverageURL, dashboard.TextSizeArm64, dashboard.ResSize, dashboard.Extra)
if err != nil {
log.Error("Dashborad() d.db.Exec() error(%v)", err)
return
}
rows, err = res.LastInsertId()
return
}
// DashboradLogs insert dashboard log.
func (d *Dao) DashboradLogs(c context.Context, id int64, logs []*publish.Log) (rows int64, err error) {
var (
buffer bytes.Buffer
insertTp string
)
insertTp = "(%d,'%s','%s'),"
buffer.WriteString(fmt.Sprintf(_inDashboradLogsSQL, d.hitLogs(id)))
for _, v := range logs {
buffer.WriteString(fmt.Sprintf(insertTp, id, v.Level, v.Msg))
}
buffer.Truncate(buffer.Len() - 1)
res, err := d.db.Exec(c, buffer.String())
if err != nil {
log.Error("DashboradLogs d.db.Exec() error(%v)", err)
return
}
rows, err = res.RowsAffected()
return
}