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,57 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"app.go",
"dao.go",
"memcache.go",
],
importpath = "go-common/app/service/main/open/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/open/conf:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/orm:go_default_library",
"//library/ecode: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"],
)
go_test(
name = "go_default_test",
srcs = [
"app_test.go",
"dao_test.go",
"memcache_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/open/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,17 @@
package dao
import (
"context"
"database/sql"
"go-common/library/ecode"
)
// Secret .
func (d *Dao) Secret(c context.Context, sappKey string) (res string, err error) {
err = d.DB.Table("dm_apps").Where("appkey = ?", sappKey).Select("app_secret").Row().Scan(&res)
if err == sql.ErrNoRows {
err = ecode.NothingFound
}
return
}

View File

@@ -0,0 +1,16 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSecret(t *testing.T) {
convey.Convey("Secret", t, func() {
res, err := d.Secret(context.TODO(), "19e7f5b7d8ad531b")
convey.So(err, convey.ShouldBeNil)
convey.So(res, convey.ShouldNotBeNil)
})
}

View File

@@ -0,0 +1,56 @@
package dao
import (
"context"
"go-common/app/service/main/open/conf"
"go-common/library/cache/memcache"
"go-common/library/database/orm"
"go-common/library/log"
"github.com/jinzhu/gorm"
)
// Dao .
type Dao struct {
// db
DB *gorm.DB
//memcache
mc *memcache.Pool
}
// New new a instance.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// db
DB: orm.NewMySQL(c.ORM),
//memcache
mc: memcache.NewPool(c.Memcache),
}
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)
}
}
if err = d.pingMC(c); err != nil {
return
}
return
}
// Close close connection of db.
func (d *Dao) Close() {
if d.DB != nil {
d.DB.Close()
}
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"flag"
"os"
"testing"
"go-common/app/service/main/open/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.manager.open-service")
flag.Set("conf_token", "3642c9070eda37909090a12271b40920")
flag.Set("tree_id", "55252")
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/open-service-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(1)
}

View File

@@ -0,0 +1,64 @@
package dao
import (
"context"
"encoding/json"
"go-common/library/cache/memcache"
"go-common/library/ecode"
"go-common/library/log"
)
const (
//appkeys represents key name
_asspkeys = "sappkeys"
)
// pingMC ping memcache .
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 0}); err != nil {
log.Error("conn.Store(set, ping, 1) error (%v)", err)
}
return
}
// AppkeyCache .
func (d *Dao) AppkeyCache(c context.Context) (res map[string]string, err error) {
var (
conn memcache.Conn
item *memcache.Item
)
conn = d.mc.Get(c)
defer conn.Close()
if item, err = conn.Get(_asspkeys); err != nil {
if err == memcache.ErrNotFound {
err = ecode.NothingFound
}
return
}
res = make(map[string]string)
err = json.Unmarshal([]byte(item.Value), &res)
return
}
// SetAppkeyCache .
func (d *Dao) SetAppkeyCache(c context.Context, newData map[string]string) (err error) {
var (
conn memcache.Conn
item *memcache.Item
jsonAppCache []byte
)
if jsonAppCache, err = json.Marshal(newData); err != nil {
return
}
conn = d.mc.Get(c)
defer conn.Close()
item = &memcache.Item{
Key: _asspkeys,
Value: jsonAppCache,
}
err = conn.Set(item)
return
}

View File

@@ -0,0 +1,51 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaopingMC(t *testing.T) {
convey.Convey("pingMC", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.pingMC(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoAppkeyCache(t *testing.T) {
convey.Convey("AppkeyCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.AppkeyCache(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetAppkeyCache(t *testing.T) {
convey.Convey("SetAppkeyCache", t, func(ctx convey.C) {
var (
c = context.Background()
newData map[string]string
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetAppkeyCache(c, newData)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}