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,49 @@
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"],
importpath = "go-common/app/interface/main/favorite/dao/music",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/favorite/conf:go_default_library",
"//app/interface/main/favorite/model:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/xstr: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"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/favorite/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,49 @@
package music
import (
"context"
"fmt"
"net/url"
"go-common/app/interface/main/favorite/conf"
"go-common/app/interface/main/favorite/model"
"go-common/library/ecode"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
"go-common/library/xstr"
)
const _music = "http://api.bilibili.co/x/internal/v1/audio/songs/batch"
// Dao defeine fav Dao
type Dao struct {
httpClient *httpx.Client
}
// New return fav dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
httpClient: httpx.NewClient(c.HTTPClient),
}
return
}
// MusicMap return the music map data(all state).
func (d *Dao) MusicMap(c context.Context, musicIds []int64) (data map[int64]*model.Music, err error) {
params := url.Values{}
params.Set("level", "1")
params.Set("ids", xstr.JoinInts(musicIds))
res := new(model.MusicResult)
ip := metadata.String(c, metadata.RemoteIP)
if err = d.httpClient.Get(c, _music, ip, params, res); err != nil {
log.Error("d.HTTPClient.Get(%s?%s) error(%v)", _music, params.Encode())
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.HTTPClient.Get(%s?%s) code:%d msg:%s", _music, params.Encode(), res.Code)
err = fmt.Errorf("Get Music failed!code:=%v", res.Code)
return
}
return res.Data, nil
}

View File

@@ -0,0 +1,45 @@
package music
import (
"context"
"flag"
"os"
"testing"
"go-common/app/interface/main/favorite/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.community.favorite")
flag.Set("conf_token", "929707200cf97646d1de7116e555dcfa")
flag.Set("tree_id", "2296")
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/favorite-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func TestMusicMap(t *testing.T) {
Convey("MusicMap", t, func() {
_, err := d.MusicMap(context.Background(), []int64{0})
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,49 @@
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"],
importpath = "go-common/app/interface/main/favorite/dao/topic",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/favorite/conf:go_default_library",
"//app/interface/main/favorite/model:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/xstr: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"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/favorite/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,61 @@
package topic
import (
"context"
"net/url"
"strconv"
"go-common/app/interface/main/favorite/conf"
"go-common/app/interface/main/favorite/model"
"go-common/library/ecode"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
"go-common/library/xstr"
)
const _topic = "http://matsuri.bilibili.co/activity/pages"
// Dao defeine fav Dao
type Dao struct {
httpClient *httpx.Client
}
// New return fav dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
httpClient: httpx.NewClient(c.HTTPClient),
}
return
}
// TopicMap return the user favorited topic's map data(all state).
func (d *Dao) TopicMap(c context.Context, tpIDs []int64, isNomal bool, appInfo *model.AppInfo) (data map[int64]*model.Topic, err error) {
params := url.Values{}
params.Set("mold", "1")
if !isNomal {
params.Set("all", "isOne")
}
params.Set("pids", xstr.JoinInts(tpIDs))
if appInfo != nil {
params.Set("http", strconv.Itoa(model.HttpMode4Https))
} else {
params.Set("http", strconv.Itoa(model.HttpMode4Both))
}
res := new(model.TopicsResult)
ip := metadata.String(c, metadata.RemoteIP)
if err = d.httpClient.Get(c, _topic, ip, params, res); err != nil {
log.Error("d.HTTPClient.Get(%s?%s) error(%v)", _topic, params.Encode())
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.HTTPClient.Get(%s?%s) code:%d msg:%s", _topic, params.Encode(), res.Code)
err = model.ErrTopicRequest
return
}
data = make(map[int64]*model.Topic, len(res.Data.List))
for _, r := range res.Data.List {
data[r.ID] = r
}
return
}

View File

@@ -0,0 +1,45 @@
package topic
import (
"context"
"flag"
"os"
"testing"
"go-common/app/interface/main/favorite/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.community.favorite")
flag.Set("conf_token", "929707200cf97646d1de7116e555dcfa")
flag.Set("tree_id", "2296")
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/favorite-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func TestTopicMap(t *testing.T) {
Convey("TopicMap", t, func() {
_, err := d.TopicMap(context.Background(), []int64{1}, false, nil)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,54 @@
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",
"redis_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/favorite/conf:go_default_library",
"//app/service/main/favorite/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"redis.go",
],
importpath = "go-common/app/interface/main/favorite/dao/video",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/favorite/conf:go_default_library",
"//app/service/main/favorite/model:go_default_library",
"//library/cache/redis: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,38 @@
package video
import (
"context"
"time"
"go-common/app/interface/main/favorite/conf"
xredis "go-common/library/cache/redis"
)
// Dao defeine fav Dao
type Dao struct {
redisPool *xredis.Pool
expireRedis int
coverExpireRedis int
}
// New return fav dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
redisPool: xredis.NewPool(c.Redis.Config),
expireRedis: int(time.Duration(c.Redis.Expire) / time.Second),
coverExpireRedis: int(time.Duration(c.Redis.CoverExpire) / time.Second),
}
return
}
// Close close all connection
func (d *Dao) Close() {
if d.redisPool != nil {
d.redisPool.Close()
}
}
// Ping check connection used in dao
func (d *Dao) Ping(c context.Context) (err error) {
return d.pingRedis(c)
}

View File

@@ -0,0 +1,45 @@
package video
import (
"context"
"flag"
"os"
"testing"
"go-common/app/interface/main/favorite/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.community.favorite")
flag.Set("conf_token", "929707200cf97646d1de7116e555dcfa")
flag.Set("tree_id", "2296")
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/favorite-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func TestPing(t *testing.T) {
Convey("Ping", t, func() {
err := d.Ping(context.Background())
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,120 @@
package video
import (
"context"
"encoding/json"
"fmt"
"go-common/app/service/main/favorite/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_covers = "fcs_"
)
func coversKey(mid, fid int64) string {
return fmt.Sprintf("%s%d_%d", _covers, mid, fid)
}
// pingRedis check redis connection
func (d *Dao) pingRedis(c context.Context) (err error) {
conn := d.redisPool.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}
// SetNewCoverCache set fav's cover to cache
func (d *Dao) SetNewCoverCache(c context.Context, mid, fid int64, covers []*model.Cover) (err error) {
key := coversKey(mid, fid)
conn := d.redisPool.Get(c)
defer conn.Close()
for _, cover := range covers {
var bs []byte
if bs, err = json.Marshal(cover); err != nil {
log.Error("json.Marshal(%v) err(%v)", cover, err)
return
}
if err = conn.Send("RPUSH", key, bs); err != nil {
log.Error("conn.Send RPUSH error(%v)", err)
return
}
}
if err = conn.Send("EXPIRE", key, d.coverExpireRedis); err != nil {
log.Error("conn.Send(EXPIRE) err(%v)", err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush err(%v)", err)
return
}
for i := 0; i < len(covers)+1; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive error(%v)", err)
}
}
return
}
// NewCoversCache get multi cover of fids by pipeline
func (d *Dao) NewCoversCache(c context.Context, mid int64, fids []int64) (fcvs map[int64][]*model.Cover, mis []int64, err error) {
conn := d.redisPool.Get(c)
defer conn.Close()
for _, fid := range fids {
key := coversKey(mid, fid)
if err = conn.Send("LRANGE", key, 0, 2); err != nil {
log.Error("conn.Send(LRANGE) err(%v)", err)
return
}
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() err(%v)", err)
return
}
fcvs = make(map[int64][]*model.Cover, len(fids))
// receive lrange
for i := 0; i < len(fids); i++ {
var (
bbs [][]byte
cvs []*model.Cover
)
if bbs, err = redis.ByteSlices(conn.Receive()); err != nil {
if err == redis.ErrNil {
err = nil
mis = append(mis, fids[i])
continue
}
log.Error("redis.ByteSlices err(%v)", err)
return
}
if len(bbs) == 0 {
mis = append(mis, fids[i])
continue
}
for _, bs := range bbs {
cv := &model.Cover{}
if err = json.Unmarshal(bs, cv); err != nil {
log.Error("json.Unmarshal err(%v)", err)
return
}
cvs = append(cvs, cv)
}
fcvs[fids[i]] = cvs
}
return
}
// DelCoverCache delete folder cover
func (d *Dao) DelCoverCache(c context.Context, mid, fid int64) (err error) {
var (
key = coversKey(mid, fid)
conn = d.redisPool.Get(c)
)
defer conn.Close()
if _, err = conn.Do("DEL", key); err != nil {
log.Error("conn.Do(DEL, %s) error(%v)", key, err)
}
return
}

View File

@@ -0,0 +1,91 @@
package video
import (
"context"
"testing"
"go-common/app/service/main/favorite/model"
"github.com/smartystreets/goconvey/convey"
)
func TestVideocoversKey(t *testing.T) {
convey.Convey("coversKey", t, func(ctx convey.C) {
var (
mid = int64(1)
fid = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := coversKey(mid, fid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func Test_pingRedis(t *testing.T) {
convey.Convey("pingRedis", t, func(ctx convey.C) {
var c = context.Background()
ctx.Convey("When everything gose 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 TestVideoSetNewCoverCache(t *testing.T) {
convey.Convey("SetNewCoverCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
fid = int64(1)
covers = []*model.Cover{{
Aid: 123,
Pic: "123",
Type: 2,
},
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.SetNewCoverCache(c, mid, fid, covers)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestVideoNewCoversCache(t *testing.T) {
convey.Convey("NewCoversCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
fids = []int64{1}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
_, _, err := d.NewCoversCache(c, mid, fids)
ctx.Convey("Then err should be nil.fcvs,mis should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDelCoverCache(t *testing.T) {
convey.Convey("DelCoverCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
fid = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.DelCoverCache(c, mid, fid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}