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,68 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"api_test.go",
"dao_test.go",
"redis_test.go",
"type_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/videoup/conf:go_default_library",
"//app/interface/main/videoup/model/archive:go_default_library",
"//app/service/main/up/api/v1:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/golang/mock/gomock: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 = [
"api.go",
"dao.go",
"redis.go",
"type.go",
],
importpath = "go-common/app/interface/main/videoup/dao/archive",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/dao/tool:go_default_library",
"//app/interface/main/videoup/conf:go_default_library",
"//app/interface/main/videoup/model/archive:go_default_library",
"//app/interface/main/videoup/model/porder:go_default_library",
"//app/service/main/up/api/v1:go_default_library",
"//library/cache/redis:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/sync/errgroup: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,365 @@
package archive
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"go-common/app/interface/main/creative/dao/tool"
"go-common/app/interface/main/videoup/conf"
"go-common/app/interface/main/videoup/model/archive"
pordermdl "go-common/app/interface/main/videoup/model/porder"
upapi "go-common/app/service/main/up/api/v1"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/sync/errgroup"
"net/http"
"net/url"
"strconv"
"sync"
"time"
)
const (
_viewURL = "/videoup/view"
_addURL = "/videoup/add"
_editURL = "/videoup/edit"
_tagUpURL = "/videoup/tag/up"
_applyStaffs = "/videoup/staff/archive/applys"
// StaffWhiteGroupID const
StaffWhiteGroupID = int64(24)
)
// View get archive and videos.
func (d *Dao) View(c context.Context, aid int64, ip string) (a *archive.Archive, vs []*archive.Video, err error) {
params := url.Values{}
params.Set("aid", strconv.FormatInt(aid, 10))
var res struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Archive *archive.Archive `json:"archive"`
Videos []*archive.Video `json:"videos"`
} `json:"data"`
}
if err = d.httpR.Get(c, d.viewURI, ip, params, &res); err != nil {
log.Error("videoup view archive error(%v) | viewUri(%s) aid(%d) ip(%s) params(%v)", err, d.viewURI+"?"+params.Encode(), aid, ip, params)
err = ecode.CreativeArchiveAPIErr
return
}
if res.Code != 0 {
err = ecode.Error(ecode.Int(res.Code), res.Message)
log.Error("videoup view archive res code nq zero, res.Code(%d) | viewUri(%s) aid(%d) ip(%s) params(%v) res(%v)", res.Code, d.viewURI+"?"+params.Encode(), aid, ip, params, res)
return
}
a = res.Data.Archive
vs = res.Data.Videos
return
}
// ApplyStaffs fn
func (d *Dao) ApplyStaffs(c context.Context, aid int64, ip string) (staffs []*archive.Staff, err error) {
params := url.Values{}
params.Set("aid", strconv.FormatInt(aid, 10))
var res struct {
Code int `json:"code"`
Message string `json:"message"`
Data []*archive.StaffView `json:"data"`
}
if err = d.httpR.Get(c, d.applyStaffs, ip, params, &res); err != nil {
log.Error("videoup view archive error(%v) | viewUri(%s) aid(%d) ip(%s) params(%v)", err, d.viewURI+"?"+params.Encode(), aid, ip, params)
err = ecode.CreativeArchiveAPIErr
return
}
if res.Code != 0 {
log.Error("videoup view archive res code nq zero, res.Code(%d) | viewUri(%s) aid(%d) ip(%s) params(%v) res(%v)", res.Code, d.viewURI+"?"+params.Encode(), aid, ip, params, res)
err = ecode.CreativeArchiveAPIErr
return
}
for _, v := range res.Data {
staff := &archive.Staff{
Mid: v.ApMID,
Title: v.ApTitle,
}
staffs = append(staffs, staff)
}
return
}
// Add add archive and videos.
func (d *Dao) Add(c context.Context, ap *archive.ArcParam, ip string) (aid int64, err error) {
params := url.Values{}
params.Set("appkey", d.c.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
mh := md5.Sum([]byte(params.Encode() + d.c.App.Secret))
params.Set("sign", hex.EncodeToString(mh[:]))
var (
uri = d.addURI + "?" + params.Encode()
)
bs, err := json.Marshal(ap)
if err != nil {
log.Error("json.Marshal error(%v) | ap(%v) ap.Mid(%d) ap.videos(%v)", err, ap, ap.Mid, ap.Videos)
return
}
req, err := http.NewRequest("POST", uri, bytes.NewReader(bs))
if err != nil {
log.Error("http.NewRequest error(%v) | uri(%s) ap.Mid(%d)", err, uri, ap.Mid)
return
}
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
var res struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Aid int64 `json:"aid"`
} `json:"data"`
}
if err = d.httpW.Do(c, req, &res); err != nil {
log.Error("d.Add error(%v) | uri(%s) ap(%+v)", err, uri, ap)
err = ecode.CreativeArchiveAPIErr
return
}
if res.Code != 0 {
err = ecode.Error(ecode.Int(res.Code), res.Message)
log.Error("d.Add nq zero (%v)|(%v)|(%v)|(%v)|uri(%s),ap(%+v)", res.Code, res.Message, res.Data, err, uri, ap)
return
}
log.Info("d.Add (%s)|res.Data.Aid(%d) ip(%s) ", string(bs), res.Data.Aid, ip)
aid = res.Data.Aid
return
}
// Edit edit archive and videos.
func (d *Dao) Edit(c context.Context, ap *archive.ArcParam, ip string) (err error) {
params := url.Values{}
params.Set("appkey", conf.Conf.App.Key)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
mh := md5.Sum([]byte(params.Encode() + d.c.App.Secret))
params.Set("sign", hex.EncodeToString(mh[:]))
// uri
var (
uri = d.editURI + "?" + params.Encode()
)
// new request
bs, err := json.Marshal(ap)
if err != nil {
log.Error("json.Marshal ap error (%v) | ap(%v)", err, ap)
return
}
req, err := http.NewRequest("POST", uri, bytes.NewReader(bs))
if err != nil {
log.Error("http.NewRequest error(%v) | uri(%s) ap(%v)", err, uri, ap)
return
}
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
var res struct {
Code int `json:"code"`
Message string `json:"message"`
}
if err = d.httpW.Do(c, req, &res); err != nil {
log.Error("d.Edit error(%v) | uri(%s) ap(%+v)", err, uri, ap)
err = ecode.CreativeArchiveAPIErr
return
}
if res.Code != 0 {
err = ecode.Error(ecode.Int(res.Code), res.Message)
log.Error("d.Add nq zero (%v)|(%v)|(%v)|uri(%s),ap(%+v)", res.Code, res.Message, err, uri, ap)
return
}
log.Info("d.Edit(%s) | ip(%s)", string(bs), ip)
return
}
// DescFormat fn
func (d *Dao) DescFormat(c context.Context) (descFormats map[int]*archive.DescFormat, err error) {
var res struct {
Code int `json:"code"`
Message string `json:"message"`
Data []*archive.DescFormat `json:"data"`
}
descFormats = make(map[int]*archive.DescFormat)
if err = d.httpR.Get(c, d.descFormatURI, "", nil, &res); err != nil {
log.Error("videoup descFormat error(%v) | descFormatURI(%s) err(%v)", err, d.descFormatURI, err)
err = ecode.CreativeArchiveAPIErr
return
}
if res.Code != 0 {
err = ecode.Error(ecode.Int(res.Code), res.Message)
log.Error("videoup descFormat res.Code(%d) | descFormatURI(%s) res(%v) err(%v)", res.Code, d.descFormatURI, res, err)
return
}
for _, v := range res.Data {
descFormats[v.ID] = v
}
return
}
// TagUp fn
func (d *Dao) TagUp(c context.Context, aid int64, tag, ip string) (err error) {
params := url.Values{}
params.Set("aid", strconv.Itoa(int(aid)))
params.Set("tag", tag)
var res struct {
Code int `json:"code"`
}
if err = d.httpW.Post(c, d.tagUpURI, ip, params, &res); err != nil {
log.Error("Post(%s,%s,%s) err(%v)", d.tagUpURI, ip, params.Encode(), err)
err = ecode.CreativeArchiveAPIErr
return
}
if res.Code != 0 {
log.Error("Code(%s,%s,%s) err(%v), code(%d)", d.tagUpURI, ip, params.Encode(), err, res.Code)
err = ecode.CreativeArchiveAPIErr
return
}
return
}
// PorderCfgList fn
func (d *Dao) PorderCfgList(c context.Context) (cfgs map[int64]*pordermdl.Config, err error) {
cfgs = make(map[int64]*pordermdl.Config)
var res struct {
Code int `json:"code"`
Data []*pordermdl.Config `json:"data"`
}
if err = d.httpR.Get(c, d.porderConfigURL, "", nil, &res); err != nil {
log.Error("archive.porderConfigURL url(%s) error(%v)", d.porderConfigURL, err)
err = ecode.CreativeArchiveAPIErr
return
}
if res.Code != 0 {
log.Error("archive.porderConfigURL url(%s) res(%v)", d.porderConfigURL, res)
err = ecode.CreativeArchiveAPIErr
return
}
for _, cfg := range res.Data {
cfgs[cfg.ID] = cfg
}
return
}
// GameList fn
func (d *Dao) GameList(c context.Context) (gameMap map[int64]*pordermdl.Game, err error) {
gameMap = make(map[int64]*pordermdl.Game)
params := url.Values{}
params.Set("appkey", conf.Conf.Game.App.Key)
params.Set("appsecret", conf.Conf.Game.App.Secret)
params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
var res struct {
Code int `json:"code"`
Data []*pordermdl.Game `json:"data"`
}
var (
query, _ = tool.Sign(params)
url string
)
url = d.gameListURL + "?" + query
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Error("http.NewRequest(%s) error(%v), ", url, err)
err = ecode.CreativeGameOpenAPIErr
return
}
if err = d.httpR.Do(c, req, &res); err != nil {
log.Error("d.httpR.Do(%s) error(%v);", url, err)
err = ecode.CreativeGameOpenAPIErr
return
}
log.Info("GameList url(%+v)|gameLen(%+v)", url, len(res.Data))
if res.Code != 0 {
log.Error("GameList api url(%s) res(%v);, code(%d)", url, res, res.Code)
err = ecode.CreativeGameOpenAPIErr
return
}
for _, data := range res.Data {
gameMap[data.GameBaseID] = data
}
return
}
//StaffUps 联合投稿白名单
func (d *Dao) StaffUps(c context.Context) (ups map[int64]int64, err error) {
return d.UpSpecial(c, StaffWhiteGroupID)
}
// StaffTypeConfig 获取联合投稿分区配置
func (d *Dao) StaffTypeConfig(c context.Context) (isGary bool, typeConf map[int16]*archive.StaffTypeConf, err error) {
typeConf = make(map[int16]*archive.StaffTypeConf)
params := url.Values{}
var res struct {
Code int `json:"code"`
Data struct {
IsGary bool `json:"is_gary"`
TypeList []*archive.StaffTypeConf `json:"typelist"`
} `json:"data"`
}
if err = d.httpR.Get(c, d.staffConfigURI, "", params, &res); err != nil {
log.Error("StaffTypeConfig error(%v) | staffConfigURI(%s)", err, d.staffConfigURI)
err = ecode.CreativeArchiveAPIErr
return
}
log.Info("StaffTypeConfig url(%+v)|res(%+v)", d.staffConfigURI, res)
if res.Code != 0 || res.Data.TypeList == nil {
log.Error("StaffTypeConfig api url(%s) res(%+v);, code(%d)", d.staffConfigURI, res, res.Code)
err = ecode.CreativeArchiveAPIErr
return
}
isGary = res.Data.IsGary
for _, v := range res.Data.TypeList {
typeConf[v.TypeID] = v
}
return
}
// UpSpecial 获取UP主的特殊用户组
func (d *Dao) UpSpecial(c context.Context, gpid int64) (ups map[int64]int64, err error) {
var (
res *upapi.UpGroupMidsReply
page int
g errgroup.Group
l sync.RWMutex
)
if res, err = d.UpClient.UpGroupMids(c, &upapi.UpGroupMidsReq{
GroupID: gpid,
Pn: 1,
Ps: 1,
}); err != nil {
log.Error("UpSpecial d.UpSpecial gpid(%d)|error(%v)", gpid, err)
return
}
log.Warn("UpSpecial get total: gpid(%d)|total(%d)", gpid, res.Total)
if res.Total <= 0 {
return
}
ups = make(map[int64]int64, res.Total)
ps := int(10000)
pageNum := res.Total / ps
if res.Total%ps != 0 {
pageNum++
}
for page = 1; page <= pageNum; page++ {
tmpPage := page
g.Go(func() (err error) {
resgg, err := d.UpClient.UpGroupMids(c, &upapi.UpGroupMidsReq{
GroupID: gpid,
Pn: tmpPage,
Ps: ps,
})
if err != nil {
log.Error("d.UpGroupMids gg (%d,%d,%d) error(%v) ", gpid, tmpPage, ps, err)
err = nil
return
}
for _, mid := range resgg.Mids {
l.Lock()
ups[mid] = mid
l.Unlock()
}
return
})
}
g.Wait()
log.Warn("UpSpecial get result: gpid,total,midslen,upslens (%d)|(%d)|(%d)", gpid, res.Total, len(ups))
return
}

View File

@@ -0,0 +1,216 @@
package archive
import (
"context"
"go-common/app/interface/main/videoup/model/archive"
upapi "go-common/app/service/main/up/api/v1"
"go-common/library/ecode"
"testing"
"github.com/golang/mock/gomock"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestPing(t *testing.T) {
Convey("Ping", t, func(ctx C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx C) {
err := d.Ping(c)
ctx.Convey("Then err should be nil.a,vs should not be nil.", func(ctx C) {
ctx.So(err, ShouldNotBeNil)
})
})
})
}
func TestArchiveView(t *testing.T) {
Convey("View", t, func(ctx C) {
var (
c = context.Background()
aid = int64(10110826)
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx C) {
defer gock.OffAll()
httpMock("GET", d.viewURI).Reply(200).JSON(`{"code":20001,"data":""}`)
a, vs, err := d.View(c, aid, ip)
ctx.Convey("Then err should be nil.a,vs should not be nil.", func(ctx C) {
ctx.So(err, ShouldNotBeNil)
ctx.So(vs, ShouldBeNil)
ctx.So(a, ShouldBeNil)
})
})
})
}
func TestArchiveAdd(t *testing.T) {
Convey("Add", t, func(ctx C) {
var (
c = context.Background()
ap = &archive.ArcParam{}
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx C) {
defer gock.OffAll()
httpMock("Post", d.addURI).Reply(200).JSON(`{"code":20001,"data":""}`)
aid, err := d.Add(c, ap, ip)
ctx.Convey("Then err should be nil.aid should not be nil.", func(ctx C) {
ctx.So(err, ShouldNotBeNil)
ctx.So(aid, ShouldNotBeNil)
})
})
})
}
func TestArchiveEdit(t *testing.T) {
Convey("Edit", t, func(ctx C) {
var (
c = context.Background()
ap = &archive.ArcParam{}
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx C) {
defer gock.OffAll()
httpMock("Post", d.editURI).Reply(200).JSON(`{"code":20001,"data":""}`)
err := d.Edit(c, ap, ip)
ctx.Convey("Then err should be nil.", func(ctx C) {
ctx.So(err, ShouldNotBeNil)
})
})
})
}
func TestArchiveDescFormat(t *testing.T) {
Convey("DescFormat", t, func(ctx C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx C) {
descFormats, err := d.DescFormat(c)
ctx.Convey("Then err should be nil.descFormats should not be nil.", func(ctx C) {
ctx.So(err, ShouldBeNil)
ctx.So(descFormats, ShouldNotBeNil)
})
})
})
}
func TestArchiveTagUp(t *testing.T) {
Convey("TagUp", t, func(ctx C) {
var (
c = context.Background()
aid = int64(10110826)
tag = "iamatag"
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx C) {
err := d.TagUp(c, aid, tag, ip)
ctx.Convey("Then err should be nil.", func(ctx C) {
ctx.So(err, ShouldBeNil)
})
})
})
}
func TestArchivePorderCfgList(t *testing.T) {
Convey("PorderCfgList", t, func(ctx C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx C) {
cfgs, err := d.PorderCfgList(c)
ctx.Convey("Then err should be nil.cfgs should not be nil.", func(ctx C) {
ctx.So(err, ShouldBeNil)
ctx.So(cfgs, ShouldNotBeNil)
})
})
})
}
func TestArchiveGameList(t *testing.T) {
Convey("GameList", t, func(ctx C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx C) {
defer gock.OffAll()
httpMock("GET", d.viewURI).Reply(200).JSON(`{"code":20051,"data":""}`)
gameMap, err := d.GameList(c)
ctx.Convey("Then err should be nil.gameMap should not be nil.", func(ctx C) {
ctx.So(err, ShouldNotBeNil)
ctx.So(gameMap, ShouldNotBeNil)
})
})
})
}
func TestUpSpecial(t *testing.T) {
var (
c = context.Background()
res map[int64]int64
err error
)
Convey("UpSpecial", t, func(ctx C) {
res, err = d.UpSpecial(c, 17)
So(res, ShouldNotBeNil)
So(err, ShouldBeNil)
})
}
func TestDao_ApplyStaffs(t *testing.T) {
var (
c = context.Background()
aid = int64(10110826)
ip = "127.0.0.1"
err error
)
Convey("ApplyStaffs", t, func(ctx C) {
httpMock("GET", d.applyStaffs).Reply(200).JSON(`{"code":0}`)
_, err = d.ApplyStaffs(c, aid, ip)
So(err, ShouldBeNil)
})
}
func WithMock(t *testing.T, f func(mock *gomock.Controller)) func() {
return func() {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
f(mockCtrl)
}
}
func TestDao_StaffUps(t *testing.T) {
Convey("StaffUps", t, WithMock(t, func(mockCtrl *gomock.Controller) {
var (
c = context.Background()
err error
ups map[int64]int64
)
mock := upapi.NewMockUpClient(mockCtrl)
d.UpClient = mock
mockReq := &upapi.UpGroupMidsReq{
GroupID: 1,
Pn: 1,
Ps: 1,
}
mock.EXPECT().UpGroupMids(gomock.Any(), mockReq).Return(nil, ecode.CreativeAccServiceErr)
ups, err = d.StaffUps(c)
So(err, ShouldNotBeNil)
So(ups, ShouldBeNil)
}))
}
func TestDao_StaffTypeConfig(t *testing.T) {
var (
c = context.Background()
err error
)
Convey("StaffTypeConfig", t, func(ctx C) {
httpMock("GET", d.staffConfigURI).Reply(200).JSON(`{"code":0,"data":{"is_gray":true,"typelist":[{"typeid":22,"max_staff":6}]}}`)
_, _, err = d.StaffTypeConfig(c)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,87 @@
package archive
import (
"context"
"go-common/app/interface/main/videoup/conf"
upapi "go-common/app/service/main/up/api/v1"
"go-common/library/cache/redis"
bm "go-common/library/net/http/blademaster"
"time"
)
// Dao is archive dao.
type Dao struct {
c *conf.Config
// http
httpR *bm.Client
httpW *bm.Client
UpClient upapi.UpClient
// redis
redis *redis.Pool
redisExpire int32
// uri
viewURI string
addURI string
editURI string
typesURI string
descFormatURI string
tagUpURI string
staffConfigURI string
applyStaffs string
// ad check
porderConfigURL string
gameListURL string
}
const (
_descFormatURL = "/videoup/desc/format"
_porderConfig = "/videoup/porder/config/list"
_gameList = "/game/list"
_staffConfURI = "/x/internal/creative/staff/config"
)
// New new a dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
//filename redis
redis: redis.NewPool(c.Redis.Videoup.Config),
redisExpire: int32(time.Duration(c.Redis.Videoup.Expire) / time.Second),
// http client
httpR: bm.NewClient(c.HTTPClient.Read),
httpW: bm.NewClient(c.HTTPClient.Write),
// uri
viewURI: c.Host.Archive + _viewURL,
addURI: c.Host.Archive + _addURL,
editURI: c.Host.Archive + _editURL,
typesURI: c.Host.Archive + _typesURL,
descFormatURI: c.Host.Archive + _descFormatURL,
tagUpURI: c.Host.Archive + _tagUpURL,
staffConfigURI: c.Host.APICo + _staffConfURI,
applyStaffs: c.Host.Archive + _applyStaffs,
// ad
porderConfigURL: c.Host.Archive + _porderConfig,
gameListURL: c.Game.OpenHost + _gameList,
}
var err error
if d.UpClient, err = upapi.NewClient(c.UpClient); err != nil {
panic(err)
}
return d
}
// Ping ping success.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.pingRedis(c); err != nil {
return
}
return
}
// Close close resource.
func (d *Dao) Close() {
if d.redis != nil {
d.redis.Close()
}
}

View File

@@ -0,0 +1,45 @@
package archive
import (
"flag"
"go-common/app/interface/main/videoup/conf"
"os"
"strings"
"testing"
gock "gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.videoup")
flag.Set("conf_token", "9772c9629b00ac09af29a23004795051")
flag.Set("tree_id", "2306")
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/videoup.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)
d.httpR.SetTransport(gock.DefaultTransport)
return r
}

View File

@@ -0,0 +1,73 @@
package archive
import (
"context"
"strconv"
"time"
"go-common/app/interface/main/videoup/model/archive"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_upFavTpsPrefix = "up_fav_tps_"
)
func keyUpFavTpsPrefix(mid int64) string {
return _upFavTpsPrefix + strconv.FormatInt(mid, 10)
}
// FilenameExpires get filename expire time.
func (d *Dao) FilenameExpires(c context.Context, vs []*archive.VideoParam) (ves []*archive.VideoExpire, err error) {
var conn = d.redis.Get(c)
defer conn.Close()
for _, v := range vs {
conn.Send("GET", v.Filename)
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() error(%v) | vs(%#v)", err, vs)
return
}
for _, v := range vs {
var exp int64
if exp, err = redis.Int64(conn.Receive()); err != nil && err != redis.ErrNil {
log.Error("conn.Receive error(%+v) | filename(%s)", err, v.Filename)
return
}
err = nil // NOTE: maybe err==redis.ErrNil
ves = append(ves, &archive.VideoExpire{
Filename: v.Filename,
Expire: exp,
})
}
return
}
// FreshFavTypes fn
func (d *Dao) FreshFavTypes(c context.Context, mid int64, tp int) (err error) {
var (
conn = d.redis.Get(c)
score = time.Now().Unix()
)
defer conn.Close()
if err = conn.Send("ZADD", keyUpFavTpsPrefix(mid), score, strconv.Itoa(tp)); err != nil {
log.Error("conn.Send(ZADD, %s, %d) error(%v)", _upFavTpsPrefix, tp, err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
}
return
}
func (d *Dao) pingRedis(c context.Context) (err error) {
conn := d.redis.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}

View File

@@ -0,0 +1,69 @@
package archive
import (
"context"
"go-common/app/interface/main/videoup/model/archive"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestArchivekeyUpFavTpsPrefix(t *testing.T) {
convey.Convey("keyUpFavTpsPrefix", t, func(ctx convey.C) {
var (
mid = int64(2089809)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyUpFavTpsPrefix(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestArchiveFilenameExpires(t *testing.T) {
convey.Convey("FilenameExpires", t, func(ctx convey.C) {
var (
c = context.Background()
vs = []*archive.VideoParam{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ves, err := d.FilenameExpires(c, vs)
ctx.Convey("Then err should be nil.ves should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(ves, convey.ShouldBeNil)
})
})
})
}
func TestArchiveFreshFavTypes(t *testing.T) {
convey.Convey("FreshFavTypes", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2089809)
tp = int(162)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.FreshFavTypes(c, mid, tp)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestArchivepingRedis(t *testing.T) {
convey.Convey("pingRedis", t, func(ctx convey.C) {
var (
c = context.Background()
)
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.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,39 @@
package archive
import (
"context"
"go-common/app/interface/main/videoup/model/archive"
"go-common/library/ecode"
"go-common/library/log"
)
const (
_typesURL = "/videoup/types"
)
// TypeMapping is second types opposite first types.
func (d *Dao) TypeMapping(c context.Context) (rmap map[int16]*archive.Type, err error) {
var res struct {
Code int `json:"code"`
Message string `json:"message"`
Data map[int16]*archive.Type `json:"data"`
}
if err = d.httpR.Get(c, d.typesURI, "", nil, &res); err != nil {
log.Error("videoup view archive error(%v) | typesURI(%s)", err, d.typesURI)
err = ecode.CreativeArchiveAPIErr
return
}
if res.Code != 0 {
err = ecode.CreativeArchiveAPIErr
log.Error("get archive type failed res.Code(%d) | typesURI(%s) res(%v)", res.Code, d.typesURI, res)
return
}
rmap = make(map[int16]*archive.Type, len(res.Data))
for _, v := range res.Data {
if v.PID != 0 {
rmap[v.ID] = v
}
}
return
}

View File

@@ -0,0 +1,28 @@
package archive
import (
"context"
"testing"
"gopkg.in/h2non/gock.v1"
"github.com/smartystreets/goconvey/convey"
)
func TestArchiveTypeMapping(t *testing.T) {
convey.Convey("TypeMapping", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.typesURI).Reply(200).JSON(`{"code":20001}`)
rmap, err := d.TypeMapping(c)
ctx.Convey("Then err should be nil.rmap should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(rmap, convey.ShouldBeNil)
})
})
})
}