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,21 @@
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/interface/main/app-player/cmd:all-srcs",
"//app/interface/main/app-player/conf:all-srcs",
"//app/interface/main/app-player/dao:all-srcs",
"//app/interface/main/app-player/http:all-srcs",
"//app/interface/main/app-player/model:all-srcs",
"//app/interface/main/app-player/service:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,67 @@
## App Player 移动端playurl接口
### Version 1.1.21
> 1.去掉大会员降级视频云处理增加is_sp参数
### Version 1.1.19
> 1.playurl地址日志增加is ipv6判断
### Version 1.1.18
> 1.no_aid上报增加mobi_app和build
### Version 1.1.17
> 1.fix durl/dash log
### Version 1.1.16
> 1.增加返回值记录
### Version 1.1.15
> 1.ugc付费iphone蓝版限制
### Version 1.1.14
> 1.ugc付费IpadHD版本限制
### Version 1.1.12
> 1.增加贴片广告cid白名单逻辑
### Version 1.1.11
> 1.fix int64 && bk走v2接口
### Version 1.1.10
> 1.付费视频up主判断
### Version 1.1.9
> 1.ugc付费build限制
### Version 1.1.8
> 1.ugc付费
### Version 1.1.7
> 1.fix panic
### Version 1.1.6
> 1.app-player鉴权
### Version 1.1.5
> 1.dash优化
### Version 1.1.4
> 1.增加prom监控
### Version 1.1.3
> 1.非生产环境未传aid直接-400
### Version 1.1.2
> 1.新增字段
### Version 1.1.1
> 1.修改err
### Version 1.1.0
> 1.完善需求
### Version 1.0.1
> 1.使用grpc auth
### Version 1.0.0
> 1.项目初始化

View File

@@ -0,0 +1,10 @@
# Owner
peiyifei
liweijia
# Author
peiyifei
sunyu
# Reviewer
peiyifei

View File

@@ -0,0 +1,15 @@
# See the OWNERS docs at https://go.k8s.io/owners
approvers:
- liweijia
- peiyifei
- sunyu
labels:
- interface
- interface/main/app-player
- main
options:
no_parent_owners: true
reviewers:
- peiyifei
- sunyu

View File

@@ -0,0 +1,44 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_library",
)
go_binary(
name = "cmd",
embed = [":go_default_library"],
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["main.go"],
data = ["app-player-test.toml"],
importpath = "go-common/app/interface/main/app-player/cmd",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/interface/main/app-player/http:go_default_library",
"//library/conf/env:go_default_library",
"//library/ecode/tip:go_default_library",
"//library/log:go_default_library",
"//library/net/trace: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,61 @@
aidGray = 1
padAid = 34943774
padCid = 61219854
phoneAid = 34108719
phoneCid = 59830850
padHDAid = 34108719
padHDCid = 59830850
[log]
dir = "/data/log/app-player"
[host]
playurl = "http://uat-videodispatch-ugc.bilibili.co"
playurlBk = "http://uat-videodispatch-ugc-bk.bilibili.co"
[httpClient]
key = "e7482d29be4a95b8"
secret = "9e803791cdef756e75faee68e12b7442"
dial = "30ms"
timeout = "500ms"
keepAlive = "60s"
[httpClient.breaker]
window = "3s"
sleep = "100ms"
bucket = 10
ratio = 0.5
request = 100
[bm]
addr = "0.0.0.0:6131"
timeout = "1s"
[accountRPC]
timeout = "300ms"
[archiveRPC]
timeout = "200ms"
[memcache]
name = "app-player-job/player"
proto = "tcp"
addr = "172.22.33.137:11218"
active = 50
idle = 10
dialTimeout = "50ms"
readTimeout = "100ms"
writeTimeout = "100ms"
idleTimeout = "80s"
expireArchive = "2h"
[redis]
name = "app-player-job/player"
proto = "tcp"
addr = "172.18.33.61:6807"
active = 20
idle = 10
dialTimeout = "50ms"
readTimeout = "100ms"
writeTimeout = "100ms"
idleTimeout = "80s"

View File

@@ -0,0 +1,52 @@
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"go-common/app/interface/main/app-player/conf"
"go-common/app/interface/main/app-player/http"
"go-common/library/conf/env"
ecode "go-common/library/ecode/tip"
"go-common/library/log"
"go-common/library/net/trace"
)
func main() {
flag.Parse()
if err := conf.Init(); err != nil {
log.Error("conf.Init() error(%v)", err)
panic(err)
}
// init log
log.Init(conf.Conf.Log)
defer log.Close()
log.Info("app-player start")
// init trace
if env.DeployEnv == env.DeployEnvProd {
trace.Init(nil)
defer trace.Close()
}
// ecode init
ecode.Init(conf.Conf.Ecode)
// service init
http.Init(conf.Conf)
// init pprof conf.Conf.Perf
// init signal
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
log.Info("app-player get a signal %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
log.Info("app-player exit")
return
case syscall.SIGHUP:
default:
return
}
}
}

View File

@@ -0,0 +1,39 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["conf.go"],
importpath = "go-common/app/interface/main/app-player/conf",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/cache/memcache:go_default_library",
"//library/conf:go_default_library",
"//library/ecode/tip:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/rpc:go_default_library",
"//library/net/rpc/warden:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/BurntSushi/toml: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,112 @@
package conf
import (
"errors"
"flag"
"go-common/library/cache/memcache"
"go-common/library/conf"
ecode "go-common/library/ecode/tip"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/rpc"
"go-common/library/net/rpc/warden"
xtime "go-common/library/time"
"github.com/BurntSushi/toml"
)
var (
confPath string
client *conf.Client
// Conf all conf
Conf = &Config{}
)
// Config is
type Config struct {
HTTPClient *bm.ClientConfig
ArchiveRPC *rpc.ClientConfig
ResourceRPC *rpc.ClientConfig
Ecode *ecode.Config
Log *log.Config
Host *Host
AidGray int64
PadAid int64
PadCid int64
PhoneAid int64
PhoneCid int64
PadHDAid int64
PadHDCid int64
Bnj *Bnj
// mc
Memcache *memcache.Config
// Warden Client
ArchiveClient *warden.ClientConfig
UGCpayClient *warden.ClientConfig
AccountClient *warden.ClientConfig
}
// Bnj is
type Bnj struct {
Tick xtime.Duration
Aids []int64
}
// Host struct
type Host struct {
Playurl string
PlayurlBk string
}
func init() {
flag.StringVar(&confPath, "conf", "", "config path")
}
// Init init conf
func Init() error {
if confPath != "" {
return local()
}
return remote()
}
func local() (err error) {
_, err = toml.DecodeFile(confPath, &Conf)
return
}
func remote() (err error) {
if client, err = conf.New(); err != nil {
return
}
if err = load(); err != nil {
return
}
client.Watch("app-player.toml")
go func() {
for range client.Event() {
log.Info("config reload")
if load() != nil {
log.Error("config reload error (%v)", err)
}
}
}()
return
}
func load() (err error) {
var (
s string
ok bool
tmpConf *Config
)
if s, ok = client.Toml2(); !ok {
return errors.New("load config center error")
}
if _, err = toml.Decode(s, &tmpConf); err != nil {
return errors.New("could not decode config")
}
*Conf = *tmpConf
return
}

View File

@@ -0,0 +1,55 @@
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-player/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/main/app-player/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/interface/main/app-player/model:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata: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",
"//app/interface/main/app-player/dao/account:all-srcs",
"//app/interface/main/app-player/dao/archive:all-srcs",
"//app/interface/main/app-player/dao/resource:all-srcs",
"//app/interface/main/app-player/dao/ugcpay:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,46 @@
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-player/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/main/app-player/dao/account",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/service/main/account/api: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,36 @@
package account
import (
"context"
"fmt"
"go-common/app/interface/main/app-player/conf"
accrpc "go-common/app/service/main/account/api"
"github.com/pkg/errors"
)
// Dao is account dao.
type Dao struct {
// rpc
accRPC accrpc.AccountClient
}
// New account dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{}
var err error
d.accRPC, err = accrpc.NewClient(c.AccountClient)
if err != nil {
panic(fmt.Sprintf("account NewClient error(%v)", err))
}
return
}
// Card get card
func (d *Dao) Card(c context.Context, mid int64) (card *accrpc.CardReply, err error) {
if card, err = d.accRPC.Card3(c, &accrpc.MidReq{Mid: mid}); err != nil {
err = errors.Wrapf(err, "%v", mid)
}
return
}

View File

@@ -0,0 +1,51 @@
package account
import (
"context"
"flag"
"os"
"testing"
"go-common/app/interface/main/app-player/conf"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-player")
flag.Set("conf_token", "e477d98a7c5689623eca4f32f6af735c")
flag.Set("tree_id", "52581")
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")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func TestCard(t *testing.T) {
var (
c = context.Background()
mid = int64(1)
)
convey.Convey("Card", t, func(ctx convey.C) {
card, err := d.Card(c, mid)
ctx.Convey("Then err should be nil.card should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(card, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,55 @@
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",
"memcache_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"memcache.go",
],
importpath = "go-common/app/interface/main/app-player/dao/archive",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/interface/main/app-player/model/archive:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/log: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,125 @@
package archive
import (
"context"
"fmt"
"runtime"
"go-common/app/interface/main/app-player/conf"
"go-common/app/interface/main/app-player/model/archive"
arcrpc "go-common/app/service/main/archive/api"
"go-common/library/cache/memcache"
"go-common/library/log"
)
// Dao is archive dao.
type Dao struct {
// memcache
arcMc *memcache.Pool
// chan
mCh chan func()
// rpc
arcRPC arcrpc.ArchiveClient
}
// New new a archive dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// memcache
arcMc: memcache.NewPool(c.Memcache),
// mc proc
mCh: make(chan func(), 1024),
}
var err error
d.arcRPC, err = arcrpc.NewClient(c.ArchiveClient)
if err != nil {
panic(fmt.Sprintf("archive NewClient error(%v)", err))
}
for i := 0; i < runtime.NumCPU(); i++ {
go d.cacheproc()
}
return
}
// Ping ping check memcache connection
func (d *Dao) Ping(c context.Context) (err error) {
return d.pingMC(c)
}
// addCache add archive to mc or redis
func (d *Dao) addCache(f func()) {
select {
case d.mCh <- f:
default:
log.Warn("cacheproc chan full")
}
}
// cacheproc write memcache and stat redis use goroutine
func (d *Dao) cacheproc() {
for {
f, ok := <-d.mCh
if !ok {
return
}
f()
}
}
// ArchiveCache is
func (d *Dao) ArchiveCache(c context.Context, aid int64) (arc *archive.Info, err error) {
if arc, err = d.archiveCache(c, aid); err != nil {
log.Error("%+v", err)
err = nil
}
if arc != nil {
return
}
var (
view *arcrpc.ViewReply
cids []int64
)
if view, err = d.arcRPC.View(c, &arcrpc.ViewRequest{Aid: aid}); err != nil {
log.Error("d.arcRPC.View3(%d) error(%+v)", aid, err)
return
}
for _, p := range view.Pages {
cids = append(cids, p.Cid)
}
arc = &archive.Info{
Aid: view.Arc.Aid,
State: view.Arc.State,
Mid: view.Arc.Author.Mid,
Cids: cids,
Attribute: view.Arc.Attribute,
}
d.addCache(func() {
d.addArchiveCache(context.Background(), aid, arc)
})
return
}
// Views is
func (d *Dao) Views(c context.Context, aids []int64) (arcs map[int64]*archive.Info, err error) {
var reply *arcrpc.ViewsReply
if reply, err = d.arcRPC.Views(c, &arcrpc.ViewsRequest{Aids: aids}); err != nil {
return
}
arcs = make(map[int64]*archive.Info)
for _, v := range reply.Views {
var (
info = new(archive.Info)
cids []int64
)
info.Aid = v.Arc.Aid
info.State = v.Arc.State
info.Mid = v.Arc.Author.Mid
info.Attribute = v.Arc.Attribute
for _, p := range v.Pages {
cids = append(cids, p.Cid)
}
info.Cids = cids
arcs[info.Aid] = info
}
return
}

View File

@@ -0,0 +1,77 @@
package archive
import (
"context"
"flag"
"fmt"
"os"
"testing"
"go-common/app/interface/main/app-player/conf"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-player")
flag.Set("conf_token", "e477d98a7c5689623eca4f32f6af735c")
flag.Set("tree_id", "52581")
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")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func TestPing(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("Ping", t, func(ctx convey.C) {
err := d.Ping(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestArchiveCache(t *testing.T) {
var (
c = context.Background()
aid = int64(10110670)
)
convey.Convey("ArchiveCache", t, func(ctx convey.C) {
arc, err := d.ArchiveCache(c, aid)
fmt.Printf("%#v", arc)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestViews(t *testing.T) {
var (
c = context.Background()
aids = []int64{10110670}
)
convey.Convey("Views", t, func(ctx convey.C) {
_, err := d.Views(c, aids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,59 @@
package archive
import (
"context"
"strconv"
"go-common/app/interface/main/app-player/model/archive"
"go-common/library/cache/memcache"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
_prefixArc = "p_"
)
func keyArc(aid int64) string {
return _prefixArc + strconv.FormatInt(aid, 10)
}
func (d *Dao) archiveCache(c context.Context, aid int64) (arcMc *archive.Info, err error) {
conn := d.arcMc.Get(c)
key := keyArc(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
arcMc = &archive.Info{}
if err = conn.Scan(r, arcMc); err != nil {
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// addArchiveCache add archive cache.
func (d *Dao) addArchiveCache(c context.Context, aid int64, arc *archive.Info) (err error) {
conn := d.arcMc.Get(c)
key := keyArc(aid)
item := &memcache.Item{Key: key, Object: arc, Flags: memcache.FlagProtobuf}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s, %v) error(%v)", key, arc, err)
}
conn.Close()
return
}
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.arcMc.Get(c)
err = conn.Set(&memcache.Item{Key: "ping", Value: []byte{1}, Flags: memcache.FlagRAW, Expiration: 0})
conn.Close()
return
}

View File

@@ -0,0 +1,20 @@
package archive
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestPingMc(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("Ping", t, func(ctx convey.C) {
err := d.Ping(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,79 @@
package dao
import (
"context"
"encoding/json"
"net/http"
"net/url"
"strconv"
"go-common/app/interface/main/app-player/conf"
"go-common/app/interface/main/app-player/model"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
"github.com/pkg/errors"
)
// Dao is
type Dao struct {
client *httpx.Client
}
// New elec dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
client: httpx.NewClient(c.HTTPClient),
}
return
}
// Playurl is
func (d *Dao) Playurl(c context.Context, mid, aid, cid, qn int64, npcybs, fnver, fnval, forceHost, isSp int, otype, mobiApp, buvid, fp, session, reqURL string) (playurl *model.Playurl, code int, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("otype", otype)
params.Set("buvid", buvid)
params.Set("platform", mobiApp)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("cid", strconv.FormatInt(cid, 10))
params.Set("session", session)
params.Set("force_host", strconv.Itoa(forceHost))
params.Set("is_sp", strconv.Itoa(isSp))
if aid > 0 {
params.Set("avid", strconv.FormatInt(aid, 10))
}
params.Set("fnver", strconv.Itoa(fnver))
params.Set("fnval", strconv.Itoa(fnval))
if qn != 0 {
params.Set("qn", strconv.FormatInt(qn, 10))
}
if npcybs != 0 {
params.Set("npcybs", strconv.Itoa(npcybs))
}
var res struct {
Code int `json:"code"`
*model.Playurl
}
var req *http.Request
if req, err = d.client.NewRequest(http.MethodGet, reqURL, ip, params); err != nil {
err = errors.Wrap(err, "d.client.NewRequest error")
return
}
if fp != "" {
req.Header.Set("X-BVC-FINGERPRINT", fp)
}
if err = d.client.Do(c, req, &res); err != nil {
return
}
playurl = res.Playurl
playurl.FormatDash()
code = res.Code
if code == 0 {
durl, _ := json.Marshal(playurl.Durl)
dash, _ := json.Marshal(playurl.Dash)
log.Info("playurlresult isv6:%v ip:%s, durl:%s, dash:%s", len(ip) > 16, ip, durl, dash)
}
return
}

View File

@@ -0,0 +1,49 @@
package dao
import (
"context"
"flag"
"os"
"testing"
"go-common/app/interface/main/app-player/conf"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-player")
flag.Set("conf_token", "e477d98a7c5689623eca4f32f6af735c")
flag.Set("tree_id", "52581")
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")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func TestPlayurl(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("Playurl", t, func(ctx convey.C) {
_, _, err := d.Playurl(c, 0, 1, 10109227, 32, 0, 0, 0, 0, 0, "json", "android", "dajskldasjkl", "", "", "")
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,45 @@
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"],
tags = ["automanaged"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/main/app-player/dao/resource",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/service/main/resource/rpc/client: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,32 @@
package resource
import (
"context"
"go-common/app/interface/main/app-player/conf"
resrpc "go-common/app/service/main/resource/rpc/client"
"go-common/library/log"
)
type Dao struct {
c *conf.Config
// rpc
resRPC *resrpc.Service
}
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
// rpc
resRPC: resrpc.New(c.ResourceRPC),
}
return
}
// PasterCID get all paster cid.
func (d *Dao) PasterCID(c context.Context) (cids map[int64]int64, err error) {
if cids, err = d.resRPC.PasterCID(c); err != nil {
log.Error("d.resRPC.PasterCID() error(%v)", err)
}
return
}

View File

@@ -0,0 +1,37 @@
package resource
import (
"context"
"flag"
"path/filepath"
"testing"
"time"
"go-common/app/interface/main/app-player/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func ctx() context.Context {
return context.Background()
}
func init() {
dir, _ := filepath.Abs("../../cmd/app-player-test.toml")
flag.Set("conf", dir)
conf.Init()
d = New(conf.Conf)
time.Sleep(time.Second)
}
func TestPasterCID(t *testing.T) {
Convey("PasterCID", t, func() {
_, err := d.PasterCID(ctx())
err = nil
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,46 @@
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-player/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/main/app-player/dao/ugcpay",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/service/main/ugcpay/api/grpc/v1: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,36 @@
package ugcpay
import (
"context"
"fmt"
"go-common/app/interface/main/app-player/conf"
ugcpay "go-common/app/service/main/ugcpay/api/grpc/v1"
"go-common/library/log"
)
// Dao is ugcpay dao.
type Dao struct {
// rpc
ugcpayRPC ugcpay.UGCPayClient
}
// New new a archive dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{}
var err error
d.ugcpayRPC, err = ugcpay.NewClient(c.UGCpayClient)
if err != nil {
panic(fmt.Sprintf("ugcpay NewClient error(%v)", err))
}
return
}
// AssetRelation is
func (d *Dao) AssetRelation(c context.Context, aid, mid int64) (relation *ugcpay.AssetRelationResp, err error) {
if relation, err = d.ugcpayRPC.AssetRelation(c, &ugcpay.AssetRelationReq{Oid: aid, Mid: mid, Otype: "archive"}); err != nil {
log.Error("d.ugcpayRPC.AssetRelationDetail(%d) error(%+v)", aid, err)
return
}
return
}

View File

@@ -0,0 +1,51 @@
package ugcpay
import (
"context"
"flag"
"os"
"testing"
"go-common/app/interface/main/app-player/conf"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-player")
flag.Set("conf_token", "e477d98a7c5689623eca4f32f6af735c")
flag.Set("tree_id", "52581")
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")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func TestAssetRelation(t *testing.T) {
var (
c = context.Background()
aid = int64(10110670)
mid = int64(2)
)
convey.Convey("AssetRelation", t, func(ctx convey.C) {
_, err := d.AssetRelation(c, aid, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,43 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"http.go",
"playurl.go",
],
importpath = "go-common/app/interface/main/app-player/http",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/interface/main/app-player/model:go_default_library",
"//app/interface/main/app-player/service:go_default_library",
"//library/conf/env:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/auth:go_default_library",
"//library/net/http/blademaster/middleware/verify:go_default_library",
"//library/stat/prom: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,43 @@
package http
import (
"go-common/app/interface/main/app-player/conf"
"go-common/app/interface/main/app-player/service"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/auth"
"go-common/library/net/http/blademaster/middleware/verify"
"go-common/library/stat/prom"
)
var (
svr *service.Service
ver *verify.Verify
ah *auth.Auth
errCount = prom.BusinessErrCount
)
// Init init http
func Init(c *conf.Config) {
initService(c)
engine := bm.DefaultServer(nil)
outerRouter(engine)
if err := engine.Start(); err != nil {
panic(err)
}
}
func initService(c *conf.Config) {
svr = service.New(c)
ver = verify.New(nil)
ah = auth.New(nil)
}
func outerRouter(e *bm.Engine) {
e.Ping(ping)
e.GET("/x/playurl", ver.Verify, ah.GuestMobile, playurl)
}
// Ping is
func ping(ctx *bm.Context) {
}

View File

@@ -0,0 +1,50 @@
package http
import (
"fmt"
"go-common/app/interface/main/app-player/model"
"go-common/library/conf/env"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
func playurl(c *bm.Context) {
params := &model.Param{}
if err := c.Bind(params); err != nil {
return
}
var mid int64
if midInter, ok := c.Get("mid"); ok {
mid = midInter.(int64)
}
header := c.Request.Header
buvid := header.Get("Buvid")
fp := header.Get("X-BVC-FINGERPRINT")
if params.AID <= 0 {
errCount.Incr(fmt.Sprintf("%s_%d", params.MobiApp, params.Build))
log.Warn("juranmeichuan aid %s", c.Request.URL.Path+"?"+c.Request.Form.Encode())
if env.DeployEnv != env.DeployEnvProd {
c.JSON(nil, ecode.RequestErr)
return
}
}
if params.CID <= 0 {
c.JSON(nil, ecode.RequestErr)
return
}
if params.Qn < 0 {
c.JSON(nil, ecode.RequestErr)
return
}
if params.Npcybs < 0 {
c.JSON(nil, ecode.RequestErr)
return
}
if params.Otype != "json" && params.Otype != "xml" {
params.Otype = "json"
}
plat := model.Plat(params.MobiApp, params.Device)
c.JSON(svr.Playurl(c, mid, params, plat, buvid, fp))
}

View File

@@ -0,0 +1,34 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"const.go",
"model.go",
],
importpath = "go-common/app/interface/main/app-player/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/interface/main/app-player/model/archive:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,49 @@
load(
"@io_bazel_rules_go//proto:def.bzl",
"go_proto_library",
)
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["archive.go"],
embed = [":archive_go_proto"],
importpath = "go-common/app/interface/main/app-player/model/archive",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = ["@com_github_golang_protobuf//proto: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"],
)
proto_library(
name = "archive_proto",
srcs = ["model.proto"],
tags = ["automanaged"],
)
go_proto_library(
name = "archive_go_proto",
compilers = ["@io_bazel_rules_go//proto:go_proto"],
importpath = "go-common/app/interface/main/app-player/model/archive",
proto = ":archive_proto",
tags = ["automanaged"],
)

View File

@@ -0,0 +1,37 @@
package archive
// is
const (
StateOpen = int32(0)
AttrNo = int32(0)
AttrYes = int32(1)
AttrBitBadgepay = uint(18)
AttrBitUGCPay = uint(22)
AttrBitIsPGC = uint(9)
)
// IsNormal check archive is normal
func (info *Info) IsNormal() bool {
return info.State >= StateOpen
}
// IsPGC is
func (info *Info) IsPGC() bool {
return info.AttrVal(AttrBitIsPGC) == AttrYes
}
// AttrVal get attr val by bit.
func (info *Info) AttrVal(bit uint) int32 {
return (info.Attribute >> bit) & int32(1)
}
// HasCid check cid is in info.Cids
func (info *Info) HasCid(cid int64) (ok bool) {
for _, id := range info.Cids {
if cid == id {
ok = true
break
}
}
return
}

View File

@@ -0,0 +1,511 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: model.proto
/*
Package archive is a generated protocol buffer package.
It is generated from these files:
model.proto
It has these top-level messages:
Info
*/
package archive
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import io "io"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type Info struct {
Aid int64 `protobuf:"varint,1,opt,name=aid,proto3" json:"aid,omitempty"`
Cids []int64 `protobuf:"varint,2,rep,packed,name=cids" json:"cids,omitempty"`
State int32 `protobuf:"varint,3,opt,name=state,proto3" json:"state,omitempty"`
Mid int64 `protobuf:"varint,4,opt,name=mid,proto3" json:"mid,omitempty"`
Attribute int32 `protobuf:"varint,5,opt,name=attribute,proto3" json:"attribute,omitempty"`
}
func (m *Info) Reset() { *m = Info{} }
func (m *Info) String() string { return proto.CompactTextString(m) }
func (*Info) ProtoMessage() {}
func (*Info) Descriptor() ([]byte, []int) { return fileDescriptorModel, []int{0} }
func (m *Info) GetAid() int64 {
if m != nil {
return m.Aid
}
return 0
}
func (m *Info) GetCids() []int64 {
if m != nil {
return m.Cids
}
return nil
}
func (m *Info) GetState() int32 {
if m != nil {
return m.State
}
return 0
}
func (m *Info) GetMid() int64 {
if m != nil {
return m.Mid
}
return 0
}
func (m *Info) GetAttribute() int32 {
if m != nil {
return m.Attribute
}
return 0
}
func init() {
proto.RegisterType((*Info)(nil), "archive.Info")
}
func (m *Info) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalTo(dAtA)
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *Info) MarshalTo(dAtA []byte) (int, error) {
var i int
_ = i
var l int
_ = l
if m.Aid != 0 {
dAtA[i] = 0x8
i++
i = encodeVarintModel(dAtA, i, uint64(m.Aid))
}
if len(m.Cids) > 0 {
dAtA2 := make([]byte, len(m.Cids)*10)
var j1 int
for _, num1 := range m.Cids {
num := uint64(num1)
for num >= 1<<7 {
dAtA2[j1] = uint8(uint64(num)&0x7f | 0x80)
num >>= 7
j1++
}
dAtA2[j1] = uint8(num)
j1++
}
dAtA[i] = 0x12
i++
i = encodeVarintModel(dAtA, i, uint64(j1))
i += copy(dAtA[i:], dAtA2[:j1])
}
if m.State != 0 {
dAtA[i] = 0x18
i++
i = encodeVarintModel(dAtA, i, uint64(m.State))
}
if m.Mid != 0 {
dAtA[i] = 0x20
i++
i = encodeVarintModel(dAtA, i, uint64(m.Mid))
}
if m.Attribute != 0 {
dAtA[i] = 0x28
i++
i = encodeVarintModel(dAtA, i, uint64(m.Attribute))
}
return i, nil
}
func encodeFixed64Model(dAtA []byte, offset int, v uint64) int {
dAtA[offset] = uint8(v)
dAtA[offset+1] = uint8(v >> 8)
dAtA[offset+2] = uint8(v >> 16)
dAtA[offset+3] = uint8(v >> 24)
dAtA[offset+4] = uint8(v >> 32)
dAtA[offset+5] = uint8(v >> 40)
dAtA[offset+6] = uint8(v >> 48)
dAtA[offset+7] = uint8(v >> 56)
return offset + 8
}
func encodeFixed32Model(dAtA []byte, offset int, v uint32) int {
dAtA[offset] = uint8(v)
dAtA[offset+1] = uint8(v >> 8)
dAtA[offset+2] = uint8(v >> 16)
dAtA[offset+3] = uint8(v >> 24)
return offset + 4
}
func encodeVarintModel(dAtA []byte, offset int, v uint64) int {
for v >= 1<<7 {
dAtA[offset] = uint8(v&0x7f | 0x80)
v >>= 7
offset++
}
dAtA[offset] = uint8(v)
return offset + 1
}
func (m *Info) Size() (n int) {
var l int
_ = l
if m.Aid != 0 {
n += 1 + sovModel(uint64(m.Aid))
}
if len(m.Cids) > 0 {
l = 0
for _, e := range m.Cids {
l += sovModel(uint64(e))
}
n += 1 + sovModel(uint64(l)) + l
}
if m.State != 0 {
n += 1 + sovModel(uint64(m.State))
}
if m.Mid != 0 {
n += 1 + sovModel(uint64(m.Mid))
}
if m.Attribute != 0 {
n += 1 + sovModel(uint64(m.Attribute))
}
return n
}
func sovModel(x uint64) (n int) {
for {
n++
x >>= 7
if x == 0 {
break
}
}
return n
}
func sozModel(x uint64) (n int) {
return sovModel(uint64((x << 1) ^ uint64((int64(x) >> 63))))
}
func (m *Info) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowModel
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: Info: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: Info: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field Aid", wireType)
}
m.Aid = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowModel
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
m.Aid |= (int64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
case 2:
if wireType == 0 {
var v int64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowModel
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
v |= (int64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
m.Cids = append(m.Cids, v)
} else if wireType == 2 {
var packedLen int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowModel
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
packedLen |= (int(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
if packedLen < 0 {
return ErrInvalidLengthModel
}
postIndex := iNdEx + packedLen
if postIndex > l {
return io.ErrUnexpectedEOF
}
for iNdEx < postIndex {
var v int64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowModel
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
v |= (int64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
m.Cids = append(m.Cids, v)
}
} else {
return fmt.Errorf("proto: wrong wireType = %d for field Cids", wireType)
}
case 3:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field State", wireType)
}
m.State = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowModel
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
m.State |= (int32(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
case 4:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field Mid", wireType)
}
m.Mid = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowModel
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
m.Mid |= (int64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
case 5:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field Attribute", wireType)
}
m.Attribute = 0
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowModel
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
m.Attribute |= (int32(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
default:
iNdEx = preIndex
skippy, err := skipModel(dAtA[iNdEx:])
if err != nil {
return err
}
if skippy < 0 {
return ErrInvalidLengthModel
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func skipModel(dAtA []byte) (n int, err error) {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowModel
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
wireType := int(wire & 0x7)
switch wireType {
case 0:
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowModel
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
iNdEx++
if dAtA[iNdEx-1] < 0x80 {
break
}
}
return iNdEx, nil
case 1:
iNdEx += 8
return iNdEx, nil
case 2:
var length int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowModel
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
length |= (int(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
iNdEx += length
if length < 0 {
return 0, ErrInvalidLengthModel
}
return iNdEx, nil
case 3:
for {
var innerWire uint64
var start int = iNdEx
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowModel
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
innerWire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
innerWireType := int(innerWire & 0x7)
if innerWireType == 4 {
break
}
next, err := skipModel(dAtA[start:])
if err != nil {
return 0, err
}
iNdEx = start + next
}
return iNdEx, nil
case 4:
return iNdEx, nil
case 5:
iNdEx += 4
return iNdEx, nil
default:
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
}
}
panic("unreachable")
}
var (
ErrInvalidLengthModel = fmt.Errorf("proto: negative length found during unmarshaling")
ErrIntOverflowModel = fmt.Errorf("proto: integer overflow")
)
func init() { proto.RegisterFile("model.proto", fileDescriptorModel) }
var fileDescriptorModel = []byte{
// 159 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0xce, 0xcd, 0x4f, 0x49,
0xcd, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4f, 0x2c, 0x4a, 0xce, 0xc8, 0x2c, 0x4b,
0x55, 0x2a, 0xe2, 0x62, 0xf1, 0xcc, 0x4b, 0xcb, 0x17, 0x12, 0xe0, 0x62, 0x4e, 0xcc, 0x4c, 0x91,
0x60, 0x54, 0x60, 0xd4, 0x60, 0x0e, 0x02, 0x31, 0x85, 0x84, 0xb8, 0x58, 0x92, 0x33, 0x53, 0x8a,
0x25, 0x98, 0x14, 0x98, 0x35, 0x98, 0x83, 0xc0, 0x6c, 0x21, 0x11, 0x2e, 0xd6, 0xe2, 0x92, 0xc4,
0x92, 0x54, 0x09, 0x66, 0x05, 0x46, 0x0d, 0xd6, 0x20, 0x08, 0x07, 0xa4, 0x37, 0x37, 0x33, 0x45,
0x82, 0x05, 0xa2, 0x37, 0x37, 0x33, 0x45, 0x48, 0x86, 0x8b, 0x33, 0xb1, 0xa4, 0xa4, 0x28, 0x33,
0xa9, 0xb4, 0x24, 0x55, 0x82, 0x15, 0xac, 0x16, 0x21, 0xe0, 0x24, 0x70, 0xe2, 0x91, 0x1c, 0xe3,
0x85, 0x47, 0x72, 0x8c, 0x0f, 0x1e, 0xc9, 0x31, 0xce, 0x78, 0x2c, 0xc7, 0x90, 0xc4, 0x06, 0x76,
0x95, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x5a, 0xb5, 0xa1, 0xbc, 0xa4, 0x00, 0x00, 0x00,
}

View File

@@ -0,0 +1,10 @@
syntax = "proto3";
package archive;
message Info {
int64 aid = 1;
repeated int64 cids = 2;
int32 state = 3;
int64 mid = 4;
int32 attribute = 5;
}

View File

@@ -0,0 +1,93 @@
package model
const (
// PlatAndroid is int8 for android.
PlatAndroid = int8(0)
// PlatIPhone is int8 for iphone.
PlatIPhone = int8(1)
// PlatIPad is int8 for ipad.
PlatIPad = int8(2)
// PlatWPhone is int8 for wphone.
PlatWPhone = int8(3)
// PlatAndroidG is int8 for Android Global.
PlatAndroidG = int8(4)
// PlatIPhoneI is int8 for Iphone Global.
PlatIPhoneI = int8(5)
// PlatIPadI is int8 for IPAD Global.
PlatIPadI = int8(6)
// PlatAndroidTV is int8 for AndroidTV Global.
PlatAndroidTV = int8(7)
// PlatAndroidI is int8 for Android Global.
PlatAndroidI = int8(8)
// PlatIpadHD is int8 for IpadHD
PlatIpadHD = int8(9)
// PlatAndroidB is int8 for Android Blue.
PlatAndroidB = int8(10)
// PlatIphoneB is int8 for Iphone Blue
PlatIphoneB = int8(11)
)
// IsAndroid check plat is android or ipad.
func IsAndroid(plat int8) bool {
return plat == PlatAndroid
}
// IsIOS check plat is iphone or ipad.
func IsIOS(plat int8) bool {
return plat == PlatIPad || plat == PlatIPhone || plat == PlatIPadI || plat == PlatIPhoneI
}
// IsIphone check plat is iphone.
func IsIphone(plat int8) bool {
return plat == PlatIPhone
}
// IsIPad check plat is pad.
func IsIPad(plat int8) bool {
return plat == PlatIPad
}
// IsIPadHD check plat is padHD.
func IsIPadHD(plat int8) bool {
return plat == PlatIpadHD
}
// IsOverseas is overseas
func IsOverseas(plat int8) bool {
return plat == PlatAndroidI || plat == PlatIPhoneI || plat == PlatIPadI
}
// Plat return plat by platStr or mobiApp
func Plat(mobiApp, device string) int8 {
switch mobiApp {
case "iphone":
if device == "pad" {
return PlatIPad
}
return PlatIPhone
case "white":
return PlatIPhone
case "ipad":
return PlatIpadHD
case "android", "android_b":
return PlatAndroid
case "win":
return PlatWPhone
case "android_G":
return PlatAndroidG
case "android_i":
return PlatAndroidI
case "iphone_i":
if device == "pad" {
return PlatIPadI
}
return PlatIPhoneI
case "ipad_i":
return PlatIPadI
case "android_tv":
return PlatAndroidTV
case "iphone_b":
return PlatIphoneB
}
return PlatIPhone
}

View File

@@ -0,0 +1,93 @@
package model
// Playurl is http://git.bilibili.co/video/playurl_doc/blob/master/PlayurlV2%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3.md
type Playurl struct {
From string `json:"from"`
Result string `json:"result"`
Quality int64 `json:"quality"`
Format string `json:"format"`
Timelength int64 `json:"timelength"`
AcceptFormat string `json:"accept_format"`
AcceptDescription []string `json:"accept_description,omitempty"`
AcceptQuality []int64 `json:"accept_quality"`
VideoCodecid int `json:"video_codecid"`
Fnver int `json:"fnver"`
Fnval int `json:"fnval"`
VideoProject bool `json:"video_project"`
SeekParam string `json:"seek_param"`
SeekType string `json:"seek_type"`
Abtid int `json:"abtid,omitempty"`
Durl []*Durl `json:"durl,omitempty"`
Dash *Dash `json:"dash,omitempty"`
}
// FormatDash dash 驼峰 -> 下划线
func (p *Playurl) FormatDash() {
if p.Dash != nil {
var as, vs []*DashItem
for _, a := range p.Dash.Audio {
as = append(as, &DashItem{
ID: a.ID,
BaseURL: a.BaseURLRes,
BackupURL: a.BackupURLRes,
Bandwidth: a.Bandwidth,
Codecid: a.Codecid,
})
}
for _, v := range p.Dash.Video {
vs = append(vs, &DashItem{
ID: v.ID,
BaseURL: v.BaseURLRes,
BackupURL: v.BackupURLRes,
Bandwidth: v.Bandwidth,
Codecid: v.Codecid,
})
}
p.Dash.Audio = as
p.Dash.Video = vs
}
}
// Durl is
type Durl struct {
Order int `json:"order"`
Length int64 `json:"length"`
Size int64 `json:"size"`
AHead string `json:"ahead,omitempty"`
VHead string `json:"vhead,omitempty"`
URL string `json:"url"`
BackupURL []string `json:"backup_url,omitempty"`
}
//Param is
type Param struct {
AID int64 `form:"aid"`
CID int64 `form:"cid"`
Qn int64 `form:"qn"`
Npcybs int `form:"npcybs"`
Otype string `form:"otype"`
MobiApp string `form:"mobi_app"`
Fnver int `form:"fnver"`
Fnval int `form:"fnval"`
Session string `form:"session"`
Build int `form:"build"`
Device string `form:"device"`
ForceHost int `form:"force_host"`
}
// Dash is
type Dash struct {
Video []*DashItem `json:"video"`
Audio []*DashItem `json:"audio"`
}
// DashItem is
type DashItem struct {
ID int64 `json:"id"`
BaseURL string `json:"base_url"`
BackupURL []string `json:"backup_url,omitempty"`
BaseURLRes string `json:"baseUrl,omitempty"`
BackupURLRes []string `json:"backupUrl,omitempty"`
Bandwidth int64 `json:"bandwidth"`
Codecid int64 `json:"codecid"`
}

View File

@@ -0,0 +1,13 @@
#### app-player
##### 项目简介
> 1.app端playurl接口
##### 编译环境
> 请只用golang v1.8.x以上版本编译执行。
##### 依赖包
> 1.公共包go-common
##### 特别说明
> 1.model目录可能会被其他项目引用请谨慎请改并通知各方。

View File

@@ -0,0 +1,59 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["service_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/interface/main/app-player/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"bnj.go",
"service.go",
],
importpath = "go-common/app/interface/main/app-player/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/interface/main/app-player/dao:go_default_library",
"//app/interface/main/app-player/dao/account:go_default_library",
"//app/interface/main/app-player/dao/archive:go_default_library",
"//app/interface/main/app-player/dao/resource:go_default_library",
"//app/interface/main/app-player/dao/ugcpay:go_default_library",
"//app/interface/main/app-player/model:go_default_library",
"//app/interface/main/app-player/model/archive:go_default_library",
"//app/service/main/account/api:go_default_library",
"//app/service/main/ugcpay/api/grpc/v1:go_default_library",
"//library/ecode: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,28 @@
package service
import (
"context"
"time"
arcmdl "go-common/app/interface/main/app-player/model/archive"
"go-common/library/log"
)
func (s *Service) loadBnjArc() {
var (
arcs map[int64]*arcmdl.Info
err error
)
if arcs, err = s.arcDao.Views(context.Background(), s.c.Bnj.Aids); err != nil {
log.Error("s.arcDao.Views error(%+v)", err)
return
}
s.bnjArcs = arcs
}
func (s *Service) bnjTickproc() {
for {
time.Sleep(time.Duration(s.c.Bnj.Tick))
s.loadBnjArc()
}
}

View File

@@ -0,0 +1,185 @@
package service
import (
"context"
"time"
"go-common/app/interface/main/app-player/conf"
"go-common/app/interface/main/app-player/dao"
accdao "go-common/app/interface/main/app-player/dao/account"
arcdao "go-common/app/interface/main/app-player/dao/archive"
resdao "go-common/app/interface/main/app-player/dao/resource"
ugcpaydao "go-common/app/interface/main/app-player/dao/ugcpay"
"go-common/app/interface/main/app-player/model"
arcmdl "go-common/app/interface/main/app-player/model/archive"
accrpc "go-common/app/service/main/account/api"
ugcpaymdl "go-common/app/service/main/ugcpay/api/grpc/v1"
"go-common/library/ecode"
"go-common/library/log"
)
const (
_qn1080 = 80
_qn480 = 32
_relationPaid = "paid"
_playURLV2 = "/v2/playurl"
_playURLV3 = "/v3/playurl"
_androidBuild = 5340000
_iosBuild = 8230
_ipadHDBuild = 12070
)
// Service is space service
type Service struct {
c *conf.Config
dao *dao.Dao
arcDao *arcdao.Dao
accDao *accdao.Dao
ugcpayDao *ugcpaydao.Dao
resDao *resdao.Dao
bnjArcs map[int64]*arcmdl.Info
// paster
pasterCache map[int64]int64
}
// New new space
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
dao: dao.New(c),
arcDao: arcdao.New(c),
accDao: accdao.New(c),
ugcpayDao: ugcpaydao.New(c),
resDao: resdao.New(c),
// paster
pasterCache: make(map[int64]int64),
}
s.loadPasterCID()
s.loadBnjArc()
go s.bnjTickproc()
go s.reloadproc()
return
}
func (s *Service) loadPasterCID() (err error) {
var tmpPaster map[int64]int64
if tmpPaster, err = s.resDao.PasterCID(context.Background()); err != nil {
log.Error("%v", err)
return
}
s.pasterCache = tmpPaster
return
}
// reloadproc reload data.
func (s *Service) reloadproc() {
for {
time.Sleep(time.Minute * 10)
s.loadPasterCID()
}
}
// Playurl is
func (s *Service) Playurl(c context.Context, mid int64, params *model.Param, plat int8, buvid, fp string) (playurl *model.Playurl, err error) {
var (
code, isSp int
reqPath = _playURLV2
aid = params.AID
cid = params.CID
build = params.Build
qn = params.Qn
)
_, ok := s.pasterCache[cid]
if aid > 0 && aid%10 < s.c.AidGray && !ok {
reqPath = _playURLV3
var (
arc *arcmdl.Info
relation *ugcpaymdl.AssetRelationResp
)
if arc, ok = s.bnjArcs[aid]; !ok {
if arc, err = s.arcDao.ArchiveCache(c, aid); err != nil {
log.Error("verifyArchive %+v", err)
return
}
}
if !arc.IsNormal() || !arc.HasCid(cid) {
err = ecode.NothingFound
log.Warn("verifyArchive aid(%d) can not play or no cid(%d)", aid, cid)
return
}
// TODO 历史老坑 纪录片之类的会请求UGC的playurl先打日志记一记
if arc.IsPGC() && arc.AttrVal(arcmdl.AttrBitBadgepay) == arcmdl.AttrYes {
log.Warn("verifyArchive aid(%d) pay(%d) cid(%d) is pgc!!!!", aid, arc.AttrVal(arcmdl.AttrBitBadgepay), cid)
err = ecode.NothingFound
return
}
if arc.AttrVal(arcmdl.AttrBitUGCPay) == 1 {
if mid <= 0 {
aid, cid = s.validBuild(plat, build)
if aid == 0 {
err = ecode.PlayURLNotLogin
return
}
} else if arc.Mid != mid {
if relation, err = s.ugcpayDao.AssetRelation(c, aid, mid); err != nil {
log.Error("verifyArchive %+v", err)
err = ecode.PlayURLNotPay
return
} else if relation.State != _relationPaid {
log.Warn("verifyArchive not pay aid(%d) mid(%d) state(%s)", aid, mid, relation.State)
aid, cid = s.validBuild(plat, build)
if aid == 0 {
err = ecode.PlayURLNotPay
return
}
}
}
}
if mid > 0 {
if arc.Mid == mid {
isSp = 1
} else {
var card *accrpc.CardReply
if card, err = s.accDao.Card(c, mid); err != nil {
log.Error("verifyArchive %+v", err)
err = nil
} else if card.Card != nil && card.Card.Vip.IsValid() {
isSp = 1
}
}
} else if qn > _qn480 { //未登录最高清晰度 480
qn = _qn480
}
}
reqURL := s.c.Host.Playurl + reqPath
playurl, code, err = s.dao.Playurl(c, mid, aid, cid, qn, params.Npcybs, params.Fnver, params.Fnval, params.ForceHost, isSp, params.Otype, params.MobiApp, buvid, fp, params.Session, reqURL)
if err != nil {
log.Error("%+v", err)
reqURL = s.c.Host.PlayurlBk + _playURLV2
playurl, code, err = s.dao.Playurl(c, mid, aid, cid, qn, params.Npcybs, params.Fnver, params.Fnval, params.ForceHost, isSp, params.Otype, params.MobiApp, buvid, fp, params.Session, reqURL)
if err != nil {
log.Error("%+v", err)
return
}
}
if code != ecode.OK.Code() {
log.Error("playurl aid(%d) cid(%d) code(%d)", aid, cid, code)
err = ecode.NothingFound
playurl = nil
}
return
}
func (s *Service) validBuild(plat int8, build int) (aid, cid int64) {
if (model.IsIphone(plat) && build < _iosBuild) || (model.IsAndroid(plat) && build < _androidBuild) {
aid = s.c.PhoneAid
cid = s.c.PhoneCid
} else if model.IsIPad(plat) && build <= _iosBuild {
aid = s.c.PadAid
cid = s.c.PadCid
} else if model.IsIPadHD(plat) && build <= _ipadHDBuild {
aid = s.c.PadHDAid
cid = s.c.PadHDCid
}
return
}

View File

@@ -0,0 +1,36 @@
package service
import (
"context"
"flag"
"path/filepath"
"testing"
"go-common/app/interface/main/app-player/conf"
"go-common/app/interface/main/app-player/model"
. "github.com/smartystreets/goconvey/convey"
)
var (
s *Service
)
func init() {
dir, _ := filepath.Abs("../cmd/app-player-test.toml")
flag.Set("conf", dir)
conf.Init()
s = New(conf.Conf)
}
func Test_Playurl(t *testing.T) {
var params = &model.Param{
AID: 1,
MobiApp: "android",
CID: 1,
Qn: 32,
}
Convey("Test_Playurl", t, func() {
s.Playurl(context.TODO(), 1, params, 1, "dajskldasjkl", "")
})
}