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,55 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"app_test.go",
"dao_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/open/conf:go_default_library",
"//app/admin/main/open/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"app.go",
"dao.go",
],
importpath = "go-common/app/admin/main/open/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/open/conf:go_default_library",
"//app/admin/main/open/model:go_default_library",
"//library/database/orm:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/jinzhu/gorm: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,35 @@
package dao
import (
"context"
"go-common/app/admin/main/open/model"
)
// AddApp .
func (d *Dao) AddApp(c context.Context, g *model.App) error {
return d.DB.Table("dm_apps").Create(g).Error
}
// DelApp .
func (d *Dao) DelApp(c context.Context, appid int64) error {
return d.DB.Table("dm_apps").Where("appid = ?", appid).Update("enabled", 0).Error
}
// UpdateApp .
func (d *Dao) UpdateApp(c context.Context, arg *model.AppParams) error {
return d.DB.Table("dm_apps").Where("appid = ?", arg.AppID).Update("app_name", arg.AppName).Error
}
// ListApp .
func (d *Dao) ListApp(c context.Context, t *model.AppListParams) (res []*model.App, err error) {
db := d.DB.Table("dm_apps").Where("enabled = ?", 1)
if t.AppKey != "" {
db = db.Where("appkey = ?", t.AppKey)
}
if t.AppName != "" {
db = db.Where("app_name = ?", t.AppName)
}
err = db.Find(&res).Error
return
}

View File

@@ -0,0 +1,43 @@
package dao
import (
"context"
"testing"
"go-common/app/admin/main/open/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddApp(t *testing.T) {
convey.Convey("AddApp", t, func() {
p1 := d.AddApp(context.TODO(), nil)
convey.So(p1, convey.ShouldBeNil)
})
}
func TestDaoDelApp(t *testing.T) {
convey.Convey("DelApp", t, func() {
p1 := d.DelApp(context.TODO(), 0)
convey.So(p1, convey.ShouldBeNil)
})
}
func TestDaoUpdateApp(t *testing.T) {
bean := &model.AppParams{
AppID: 123,
AppName: "xxx",
}
convey.Convey("UpdateApp", t, func() {
p1 := d.UpdateApp(context.TODO(), bean)
convey.So(p1, convey.ShouldBeNil)
})
}
func TestDaoListApp(t *testing.T) {
convey.Convey("ListApp", t, func() {
res, err := d.ListApp(context.TODO(), nil)
convey.So(err, convey.ShouldBeNil)
convey.So(res, convey.ShouldNotBeNil)
})
}

View File

@@ -0,0 +1,47 @@
package dao
import (
"context"
"go-common/app/admin/main/open/conf"
"go-common/library/database/orm"
"go-common/library/log"
"github.com/jinzhu/gorm"
)
// Dao .
type Dao struct {
DB *gorm.DB
}
// New new a instance.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// db
DB: orm.NewMySQL(c.ORM),
}
d.initORM()
return
}
func (d *Dao) initORM() {
d.DB.LogMode(true)
}
// Ping check connection of db , mc.
func (d *Dao) Ping(c context.Context) (err error) {
if d.DB != nil {
if err = d.DB.DB().PingContext(c); err != nil {
log.Error("d.PingContext error (%v)", err)
}
}
return
}
// Close close connection of db , mc.
func (d *Dao) Close() {
if d.DB != nil {
d.DB.Close()
}
}

View File

@@ -0,0 +1,41 @@
package dao
import (
"flag"
"os"
"path/filepath"
"testing"
"go-common/app/admin/main/open/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "")
flag.Set("conf_token", "")
flag.Set("tree_id", "")
flag.Set("conf_version", "server-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()
dir, _ := filepath.Abs("../cmd/open-admin-test.toml")
if err := flag.Set("conf", dir); err != nil {
panic(err)
}
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(1)
}