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,58 @@
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",
"mc_test.go",
"mysql_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/job/main/identify/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",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"mc.go",
"mysql.go",
],
importpath = "go-common/app/job/main/identify/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/identify/conf:go_default_library",
"//app/job/main/identify/model:go_default_library",
"//app/service/main/passport-auth/model:go_default_library",
"//library/cache/memcache: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,29 @@
package dao
import (
"go-common/app/job/main/identify/conf"
"go-common/library/cache/memcache"
xsql "go-common/library/database/sql"
)
// Dao dao
type Dao struct {
c *conf.Config
authDB *xsql.DB
authMC *memcache.Pool
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
authDB: xsql.NewMySQL(c.AuthDB),
authMC: memcache.NewPool(c.AuthMC),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.authDB.Close()
}

View File

@@ -0,0 +1,54 @@
package dao
import (
"flag"
"os"
"reflect"
"testing"
"go-common/app/job/main/identify/conf"
"go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.identify-job")
flag.Set("conf_token", "138ecf5e1bc269922614e4b549b261bb")
flag.Set("tree_id", "18436")
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/identify-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func TestDaoClose(t *testing.T) {
convey.Convey("Close", t, func(ctx convey.C) {
monkey.PatchInstanceMethod(reflect.TypeOf(d.authDB), "Close", func(_ *sql.DB) error {
return nil
})
defer monkey.UnpatchAll()
var err error
d.Close()
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,89 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/main/passport-auth/model"
"go-common/library/cache/memcache"
"go-common/library/log"
)
func ckKey(session string) string {
return fmt.Sprintf("ck_%s", session)
}
func akKey(token string) string {
return fmt.Sprintf("ak_%s", token)
}
// CookieCache get cookie info from cache
func (d *Dao) CookieCache(c context.Context, session string) (res *model.Cookie, err error) {
key := ckKey(session)
conn := d.authMC.Get(c)
defer conn.Close()
var item *memcache.Item
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", key, err)
return
}
res = new(model.Cookie)
if err = conn.Scan(item, res); err != nil {
log.Error("conn.Scan(%v) error(%v)", string(item.Value), err)
}
return
}
// DelCookieCache del cache.
func (d *Dao) DelCookieCache(c context.Context, session string) (err error) {
conn := d.authMC.Get(c)
defer conn.Close()
if err = conn.Delete(ckKey(session)); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Delete(%s) error(%v)", ckKey(session), err)
}
return
}
// TokenCache get token from cache
func (d *Dao) TokenCache(c context.Context, sd string) (res *model.Token, err error) {
key := akKey(sd)
conn := d.authMC.Get(c)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", key, err)
return
}
res = new(model.Token)
if err = conn.Scan(r, res); err != nil {
log.Error("conn.Scan(%v) error(%v)", string(r.Value), err)
}
return
}
// DelTokenCache del cache.
func (d *Dao) DelTokenCache(c context.Context, token string) (err error) {
key := akKey(token)
conn := d.authMC.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Delete(%s) error(%v)", key, err)
}
return
}

View File

@@ -0,0 +1,74 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoCookieCache(t *testing.T) {
convey.Convey("CookieCache", t, func(ctx convey.C) {
var (
c = context.Background()
session = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.CookieCache(c, session)
ctx.Convey("Then res should be nil.", func(ctx convey.C) {
ctx.So(res, convey.ShouldBeNil)
})
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelCookieCache(t *testing.T) {
convey.Convey("DelCookieCache", t, func(ctx convey.C) {
var (
c = context.Background()
session = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.DelCookieCache(c, session)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTokenCache(t *testing.T) {
convey.Convey("TokenCache", t, func(ctx convey.C) {
var (
c = context.Background()
session = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.TokenCache(c, session)
ctx.Convey("Then res should be nil.", func(ctx convey.C) {
ctx.So(res, convey.ShouldBeNil)
})
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelTokenCache(t *testing.T) {
convey.Convey("DelTokenCache", t, func(ctx convey.C) {
var (
c = context.Background()
session = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.DelTokenCache(c, session)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,71 @@
package dao
import (
"context"
"encoding/hex"
"fmt"
"go-common/app/job/main/identify/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_newSessionBinByteLen = 16
_getCookieDeletedSQL = "SELECT id,mid,session FROM user_cookie_deleted_%s where id > ? limit ?"
_getTokenDeletedSQL = "SELECT id,mid,token FROM user_token_deleted_%s where id > ? limit ?"
)
// CookieDeleted get cookie deleted
func (d *Dao) CookieDeleted(c context.Context, start, count int64, suffix string) (res []*model.AuthCookie, err error) {
var rows *xsql.Rows
if rows, err = d.authDB.Query(c, fmt.Sprintf(_getCookieDeletedSQL, suffix), start, count); err != nil {
log.Error("fail to get CookieDeleted, dao.authDB.Query(%s) error(%v)", _getCookieDeletedSQL, err)
return
}
defer rows.Close()
for rows.Next() {
var session []byte
a := new(model.AuthCookie)
if err = rows.Scan(&a.ID, &a.Mid, &session); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
a.Session = encodeSession(session)
res = append(res, a)
}
return
}
// TokenDeleted get token deleted
func (d *Dao) TokenDeleted(c context.Context, start, count int64, suffix string) (res []*model.AuthToken, err error) {
var rows *xsql.Rows
if rows, err = d.authDB.Query(c, fmt.Sprintf(_getTokenDeletedSQL, suffix), start, count); err != nil {
log.Error("fail to get TokenDeleted, dao.authDB.Query(%s) error(%v)", _getTokenDeletedSQL, err)
return
}
defer rows.Close()
for rows.Next() {
var token []byte
a := new(model.AuthToken)
if err = rows.Scan(&a.ID, &a.Mid, &token); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
a.Token = hex.EncodeToString(token)
res = append(res, a)
}
return
}
func encodeSession(b []byte) (s string) {
// format new
if len(b) == _newSessionBinByteLen {
return hex.EncodeToString(b)
}
// or format old
return string(b)
}

View File

@@ -0,0 +1,49 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoCookieDeleted(t *testing.T) {
convey.Convey("CookieDeleted", t, func(ctx convey.C) {
var (
c = context.Background()
suffix = "201810"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.CookieDeleted(c, 0, 100, suffix)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTokenDeleted(t *testing.T) {
convey.Convey("TokenDeleted", t, func(ctx convey.C) {
var (
c = context.Background()
suffix = "201810"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.TokenDeleted(c, 0, 100, suffix)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDao_encodeSession(t *testing.T) {
convey.Convey("encodeSession", t, func(ctx convey.C) {
res := encodeSession([]byte{1})
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(res, convey.ShouldNotBeNil)
})
})
}