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,48 @@
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"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-view/conf: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 = ["dao.go"],
importpath = "go-common/app/interface/main/app-view/dao/live",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-view/conf:go_default_library",
"//app/interface/main/app-view/model/live:go_default_library",
"//library/ecode:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/pkg/errors: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,94 @@
package live
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"go-common/app/interface/main/app-view/conf"
"go-common/app/interface/main/app-view/model/live"
"go-common/library/ecode"
httpx "go-common/library/net/http/blademaster"
"github.com/pkg/errors"
)
const (
_list = "/room/v1/RoomMng/allLivingRoomInfo"
_bnj2019Conf = "/activity/v0/bainian/config"
)
// Dao is space dao
type Dao struct {
client *httpx.Client
list string
bnj2019 string
}
// New initial space dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
client: httpx.NewClient(c.HTTPAsync),
list: c.Host.APILiveCo + _list,
bnj2019: c.Host.APILiveCo + _bnj2019Conf,
}
return
}
// Living is get living rooms from api
func (d *Dao) Living(c context.Context) (ll []*live.Live, err error) {
params := url.Values{}
params.Set("filter_user_cover", "0")
params.Set("need_broadcast_type", "1")
params.Set("extra_fields[]", "title")
var res struct {
Code int `json:"code"`
Data []*live.RoomInfo `json:"data"`
}
if err = d.client.Get(c, d.list, "", params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.list)
return
}
for _, info := range res.Data {
l := &live.Live{}
l.LiveChange(info)
ll = append(ll, l)
}
return
}
// Bnj2019Conf 直播控制白名单
func (d *Dao) Bnj2019Conf(c context.Context) (greyStatus int, mids []int64, err error) {
var res struct {
Code int `json:"code"`
Data struct {
GreyStatus int `json:"grey_status"`
GreyUids string `json:"grey_uids"`
} `json:"data"`
}
if err = d.client.Get(c, d.bnj2019, "", nil, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.bnj2019)
return
}
greyStatus = res.Data.GreyStatus
if greyStatus == 1 {
midsStr := strings.Split(res.Data.GreyUids, ",")
for _, midStr := range midsStr {
var mid int64
if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
err = errors.New(fmt.Sprintf("live grey_uids(%s)", res.Data.GreyUids))
return
}
mids = append(mids, mid)
}
}
return
}

View File

@@ -0,0 +1,82 @@
package live
import (
"context"
"flag"
"os"
"strings"
"testing"
"go-common/app/interface/main/app-view/conf"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-view")
flag.Set("conf_token", "3a4CNLBhdFbRQPs7B4QftGvXHtJo92xw")
flag.Set("tree_id", "4575")
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/app-view-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}
func TestDaoLiving(t *testing.T) {
convey.Convey("TestDaoLiving", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.client.SetTransport(gock.DefaultTransport)
defer gock.Off()
httpMock("GET", d.list).Reply(200).JSON(`{"code":0,"data":[{"uid":"0","roomid":"1","title":"something","broadcast_type":1}]}`)
ll, err := d.Living(c)
ctx.Convey("Then ll should not be nil. err should be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ll, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoBnj2019Conf(t *testing.T) {
convey.Convey("TestDaoBnj2019Conf", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.client.SetTransport(gock.DefaultTransport)
defer gock.Off()
httpMock("GET", d.bnj2019).Reply(200).JSON(`{"code":0,"data":{"grey_status":1,"grey_uids":"0,1"}}`)
_, mids, err := d.Bnj2019Conf(c)
ctx.Convey("Then mids should not be nil. err should be nil", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(mids, convey.ShouldNotBeNil)
})
})
})
}