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,63 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"acc_test.go",
"dao_test.go",
"mysql_test.go",
"redis_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/usersuit/conf:go_default_library",
"//app/service/main/usersuit/model:go_default_library",
"//vendor/github.com/satori/go.uuid:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"acc.go",
"dao.go",
"mysql.go",
"redis.go",
],
importpath = "go-common/app/service/main/usersuit/dao/invite",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/usersuit/conf:go_default_library",
"//app/service/main/usersuit/model:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,42 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/library/ecode"
"go-common/library/log"
)
const (
_accErrWasFormal = -659
)
// BeFormal be formal member.
func (d *Dao) BeFormal(c context.Context, mid int64, cookie, ip string) (err error) {
params := url.Values{}
params.Set("Cookie", cookie)
params.Set("mid", strconv.FormatInt(mid, 10))
// request
req, err := d.httpClient.NewRequest("POST", d.beFormalURI, ip, params)
if err != nil {
log.Error("account beformal uri(%s) error(%v)", d.beFormalURI+params.Encode(), err)
return
}
req.Header.Set("Cookie", cookie)
var res struct {
Code int `json:"code"`
}
if err = d.httpClient.Do(c, req, &res); err != nil {
return
}
if res.Code != 0 {
if res.Code == _accErrWasFormal {
return
}
err = ecode.Int(res.Code)
log.Error("account beformal uri(%s) error(%v)", d.beFormalURI+params.Encode(), err)
}
return
}

View File

@@ -0,0 +1,26 @@
package dao
import (
"testing"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestDaoBeFormal(t *testing.T) {
convey.Convey("BeFormal", t, func(ctx convey.C) {
var (
mid = int64(88888970)
cookie = "fts=1532412723; buvid3=9FE379B1-4742-4414-83BC-070DC6D5C30128620infoc; rpdid=oxlilxsoqdoskipoimqw; UM_distinctid=164cb9399eb124-053863286996f2-163b6952-1fa400-164cb9399ec15e; LIVE_BUVID=dfbc64db5d227b750394fc08dc27ad5e; LIVE_BUVID__ckMd5=612509d11892d4bd; im_notify_type_27956255=0; sid=66taifil; im_local_unread_27956255=0; im_seqno_27956255=4; CNZZDATA2724999=cnzz_eid%3D937547062-1534212022-%26ntime%3D1534212022; pgv_pvi=1353490432; finger=14bc3c4e; DedeUserID=27956255; DedeUserID__ckMd5=2dfe07340adf4bc9; SESSDATA=3f325bec%2C1538121389%2Cb8e04f4a; bili_jct=008e9de4d371cf91f089a66b77e1d8d7; PHPSESSID=3btocfs7f8mo3u8cpkhfv155e0; bp_t_offset_27956255=157578003585063279; _dfcaptcha=aa24be76e8c462474766bc1f4db916a0"
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("POST", d.beFormalURI).Reply(0).JSON(`{"code":0}`)
err := d.BeFormal(c, mid, cookie, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,58 @@
package dao
import (
"context"
"time"
"go-common/app/service/main/usersuit/conf"
"go-common/library/cache/redis"
"go-common/library/database/sql"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
const (
_beFormal = "/api/member/beFormal"
)
// Dao struct answer history of Dao
type Dao struct {
db *sql.DB
redis *redis.Pool
//http
httpClient *bm.Client
c *conf.Config
beFormalURI string
inviteExpire int32
}
// New new a Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.MySQL),
redis: redis.NewPool(c.Redis.Config),
inviteExpire: int32(time.Duration(c.Redis.InviteExpire) / time.Second),
httpClient: bm.NewClient(c.HTTPClient),
beFormalURI: c.AccountIntranetURI + _beFormal,
}
return
}
// Close close connections of mc, redis, db.
func (d *Dao) Close() {
d.db.Close()
d.redis.Close()
}
// Ping ping health.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.Ping(c); err != nil {
log.Error("dao.db.Ping() error(%v)", err)
return
}
if err = d.pingRedis(c); err != nil {
log.Error("dao.pingRedis() error(%v)", err)
}
return
}

View File

@@ -0,0 +1,47 @@
package dao
import (
"context"
"flag"
"os"
"strings"
"testing"
"go-common/app/service/main/usersuit/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
c = context.Background()
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.usersuit-service")
flag.Set("conf_token", "BVWgBtBvS2pkTBbmxAl0frX6KRA14d5P")
flag.Set("tree_id", "6813")
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/convey-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}

View File

@@ -0,0 +1,88 @@
package dao
import (
"context"
"database/sql"
"time"
"go-common/app/service/main/usersuit/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_addInviteSQL = "INSERT INTO invite_code(mid,code,buy_ip,buy_ip_ng,expires,ctime) VALUES(?,?,?,?,?,?)"
_updateInviteSQL = "UPDATE invite_code SET imid=?,used_at=? WHERE code=?"
_getInviteSQL = "SELECT mid,imid,code,expires,used_at,ctime FROM invite_code WHERE code=?"
_getInvitesSQL = "SELECT mid,imid,code,expires,used_at,ctime FROM invite_code WHERE mid=?"
_getCurrentCountSQL = "SELECT count(1) FROM invite_code WHERE mid=? AND ctime>=? AND ctime<=?"
)
// Begin begin transaction
func (d *Dao) Begin(c context.Context) (tx *xsql.Tx, err error) {
return d.db.Begin(c)
}
// TxAddInvite transaction invite
func (d *Dao) TxAddInvite(c context.Context, tx *xsql.Tx, inv *model.Invite) (affected int64, err error) {
var res sql.Result
if res, err = tx.Exec(_addInviteSQL, inv.Mid, inv.Code, inv.IP, inv.IPng, inv.Expires, inv.Ctime); err != nil {
log.Error("add invite, dao.db.Exec(%d, %s, %d, %v, %d, %v) error(%v)", inv.Mid, inv.Code, inv.IP, inv.IPng, inv.Expires, inv.Ctime, err)
return
}
return res.RowsAffected()
}
// UpdateInvite update invite
func (d *Dao) UpdateInvite(c context.Context, imid int64, usedAt int64, code string) (affected int64, err error) {
var res sql.Result
if res, err = d.db.Exec(c, _updateInviteSQL, imid, usedAt, code); err != nil {
log.Error("update invite, dao.db.Exec(%d, %d, %s) error(%v)", imid, usedAt, code, err)
return
}
return res.RowsAffected()
}
// Invite invite info
func (d *Dao) Invite(c context.Context, code string) (res *model.Invite, err error) {
row := d.db.QueryRow(c, _getInviteSQL, code)
res = new(model.Invite)
if err = row.Scan(&res.Mid, &res.Imid, &res.Code, &res.Expires, &res.UsedAt, &res.Ctime); err != nil {
if err == sql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("get invite, row.Scan() error(%v)", err)
}
}
return
}
// Invites invite list
func (d *Dao) Invites(c context.Context, mid int64) (res []*model.Invite, err error) {
var rows *xsql.Rows
if rows, err = d.db.Query(c, _getInvitesSQL, mid); err != nil {
log.Error("get invites, dao.db.Query(%d) error(%v)", mid, err)
return
}
defer rows.Close()
for rows.Next() {
inv := new(model.Invite)
if err = rows.Scan(&inv.Mid, &inv.Imid, &inv.Code, &inv.Expires, &inv.UsedAt, &inv.Ctime); err != nil {
log.Error("row.Scan() error(%v)", err)
return
}
res = append(res, inv)
}
err = rows.Err()
return
}
// CurrentCount current count
func (d *Dao) CurrentCount(c context.Context, mid int64, start, end time.Time) (res int64, err error) {
row := d.db.QueryRow(c, _getCurrentCountSQL, mid, start, end)
if err = row.Scan(&res); err != nil {
log.Error("get current count, mid: %d, start: %d, end: %d, row.Scan() error(%v)", mid, start, end, err)
}
return
}

View File

@@ -0,0 +1,107 @@
package dao
import (
"testing"
"time"
"go-common/app/service/main/usersuit/model"
"github.com/satori/go.uuid"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoBegin(t *testing.T) {
convey.Convey("Begin", t, func(ctx convey.C) {
ctx.Convey("When everything goes positive", func(ctx convey.C) {
tx, err := d.Begin(c)
ctx.Convey("Then err should be nil.tx should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(tx, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTxAddInvite(t *testing.T) {
convey.Convey("TxAddInvite", t, func(ctx convey.C) {
var (
tx, _ = d.Begin(c)
inv = &model.Invite{
Mid: 1,
IPng: []byte{1, 1, 1, 1},
Code: uuid.NewV4().String(),
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
affected, err := d.TxAddInvite(c, tx, inv)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateInvite(t *testing.T) {
convey.Convey("UpdateInvite", t, func(ctx convey.C) {
var (
imid = int64(0)
usedAt = int64(0)
code = "2bbf90926c984a53"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
affected, err := d.UpdateInvite(c, imid, usedAt, code)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInvite(t *testing.T) {
convey.Convey("Invite", t, func(ctx convey.C) {
var (
code = "2bbf90926c984a53"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.Invite(c, code)
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 TestDaoInvites(t *testing.T) {
convey.Convey("Invites", t, func(ctx convey.C) {
var (
mid = int64(88888970)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.Invites(c, mid)
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 TestDaoCurrentCount(t *testing.T) {
convey.Convey("CurrentCount", t, func(ctx convey.C) {
var (
mid = int64(0)
start = time.Now()
end = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.CurrentCount(c, mid, start, end)
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)
})
})
})
}

View File

@@ -0,0 +1,86 @@
package dao
import (
"context"
"strconv"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_prefixBuyFlag = "b_"
_prefixApplyFlag = "a_"
)
func keyBuyFlag(mid int64) string {
return _prefixBuyFlag + strconv.FormatInt(mid, 10)
}
func keyApplyFlag(code string) string {
return _prefixApplyFlag + code
}
// pingRedis ping redis.
func (d *Dao) pingRedis(c context.Context) (err error) {
conn := d.redis.Get(c)
defer conn.Close()
if _, err = redis.String(conn.Do("PING")); err != nil {
log.Error("redis.String(conn.Do(PING)) error(%v)", err)
}
return
}
// SetBuyFlagCache set buy flag cache
func (d *Dao) SetBuyFlagCache(c context.Context, mid int64, f string) (ok bool, err error) {
key := keyBuyFlag(mid)
conn := d.redis.Get(c)
defer conn.Close()
var res interface{}
if res, err = conn.Do("SET", key, f, "EX", d.inviteExpire, "NX"); err != nil {
log.Error("conn.Do(SET, %s, %s, EX, %d, NX)", key, f)
return
}
if res != nil {
ok = true
}
return
}
// DelBuyFlagCache del buy flag cache
func (d *Dao) DelBuyFlagCache(c context.Context, mid int64) (err error) {
key := keyBuyFlag(mid)
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("DEL", key); err != nil {
log.Error("conn.Do(DEL, %s)", key)
}
return
}
// SetApplyFlagCache set apply flag cache
func (d *Dao) SetApplyFlagCache(c context.Context, code, f string) (ok bool, err error) {
key := keyApplyFlag(code)
conn := d.redis.Get(c)
defer conn.Close()
var res interface{}
if res, err = conn.Do("SET", key, f, "EX", d.inviteExpire, "NX"); err != nil {
log.Error("conn.Do(SET, %s, %s, EX, %d, NX)", key, f, d.inviteExpire)
return
}
if res != nil {
ok = true
}
return
}
// DelApplyFlagCache del apply Flag Cache
func (d *Dao) DelApplyFlagCache(c context.Context, code string) (err error) {
key := keyApplyFlag(code)
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("DEL", key); err != nil {
log.Error("conn.Do(DEL, %s)", key)
}
return
}

View File

@@ -0,0 +1,106 @@
package dao
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaokeyBuyFlag(t *testing.T) {
convey.Convey("keyBuyFlag", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyBuyFlag(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyApplyFlag(t *testing.T) {
convey.Convey("keyApplyFlag", t, func(ctx convey.C) {
var (
code = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyApplyFlag(code)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaopingRedis(t *testing.T) {
convey.Convey("pingRedis", t, func(ctx convey.C) {
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.pingRedis(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetBuyFlagCache(t *testing.T) {
convey.Convey("SetBuyFlagCache", t, func(ctx convey.C) {
var (
mid = int64(0)
f = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ok, err := d.SetBuyFlagCache(c, mid, f)
ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDelBuyFlagCache(t *testing.T) {
convey.Convey("DelBuyFlagCache", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelBuyFlagCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetApplyFlagCache(t *testing.T) {
convey.Convey("SetApplyFlagCache", t, func(ctx convey.C) {
var (
code = ""
f = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ok, err := d.SetApplyFlagCache(c, code, f)
ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDelApplyFlagCache(t *testing.T) {
convey.Convey("DelApplyFlagCache", t, func(ctx convey.C) {
var (
code = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelApplyFlagCache(c, code)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}