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,54 @@
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",
"search.go",
],
importpath = "go-common/app/admin/main/feed/dao/search",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/feed/conf:go_default_library",
"//app/admin/main/feed/model/search:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/orm: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 = [
"dao_test.go",
"search_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/feed/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,49 @@
package search
import (
"context"
"go-common/app/admin/main/feed/conf"
"go-common/library/cache/memcache"
"go-common/library/database/orm"
"github.com/jinzhu/gorm"
)
// Dao struct user of color egg Dao.
type Dao struct {
// db
DB *gorm.DB
MC *memcache.Pool
}
// New create a instance of color egg Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// db
DB: orm.NewMySQL(c.ORMResource),
MC: memcache.NewPool(c.Memcache.Config),
}
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 {
err = d.DB.DB().PingContext(c)
return
}
return
}
// Close close connection of db , mc.
func (d *Dao) Close() {
if d.DB != nil {
d.DB.Close()
}
}

View File

@@ -0,0 +1,32 @@
package search
import (
"flag"
"go-common/app/admin/main/feed/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.web-svr.feed-admin")
flag.Set("conf_token", "e0d2b216a460c8f8492473a2e3cdd218")
flag.Set("tree_id", "45266")
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)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,52 @@
package search
import (
"context"
"time"
searchModel "go-common/app/admin/main/feed/model/search"
"go-common/library/cache/memcache"
)
//SetSearchAuditStat set hot publish state to MC
func (d *Dao) SetSearchAuditStat(c context.Context, key string, state bool) (err error) {
var (
conn memcache.Conn
p searchModel.PublishState
)
p.Date = time.Now().Format("2006-01-02")
p.State = state
conn = d.MC.Get(c)
defer conn.Close()
itemJSON := &memcache.Item{
Key: key,
Flags: memcache.FlagJSON,
Object: p,
Expiration: 0,
}
if err = conn.Set(itemJSON); err != nil {
return
}
return
}
//GetSearchAuditStat get hot publish state from MC
func (d *Dao) GetSearchAuditStat(c context.Context, key string) (f bool, date string, err error) {
var (
conn memcache.Conn
item *memcache.Item
p searchModel.PublishState
)
conn = d.MC.Get(c)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
return false, "", nil
}
return
}
if err = conn.Scan(item, &p); err != nil {
return
}
return p.State, p.Date, nil
}

View File

@@ -0,0 +1,42 @@
package search
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestSearchSetSearchAuditStat(t *testing.T) {
convey.Convey("SetSearchAuditStat", t, func(ctx convey.C) {
var (
c = context.Background()
key = "test"
state bool
)
state = true
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetSearchAuditStat(c, key, state)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestSearchGetSearchAuditStat(t *testing.T) {
convey.Convey("GetSearchAuditStat", t, func(ctx convey.C) {
var (
c = context.Background()
key = "test"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
f, date, err := d.GetSearchAuditStat(c, key)
ctx.Convey("Then err should be nil.f,date should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(date, convey.ShouldNotBeNil)
ctx.So(f, convey.ShouldNotBeNil)
})
})
})
}