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_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-feed/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-feed/dao/ad",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-card/model/card/cm:go_default_library",
"//app/interface/main/app-feed/conf:go_default_library",
"//library/ecode:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/xstr: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,80 @@
package ad
import (
"context"
"net/url"
"strconv"
"time"
"go-common/app/interface/main/app-card/model/card/cm"
"go-common/app/interface/main/app-feed/conf"
"go-common/library/ecode"
httpx "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
"go-common/library/xstr"
"github.com/pkg/errors"
)
const (
_bce = "/bce/api/bce/wise"
)
// Dao is ad dao.
type Dao struct {
// http client
client *httpx.Client
// ad
bce string
}
// New new a ad dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// http client
client: httpx.NewClient(c.HTTPAd),
bce: c.Host.Ad + _bce,
}
return
}
func (d *Dao) Ad(c context.Context, mid int64, build int, buvid string, resource []int64, country, province, city, network, mobiApp, device, openEvent, adExtra string, style int, now time.Time) (advert *cm.Ad, err error) {
ip := metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("buvid", buvid)
params.Set("resource", xstr.JoinInts(resource))
params.Set("ip", ip)
params.Set("country", country)
params.Set("province", province)
params.Set("city", city)
params.Set("network", network)
params.Set("build", strconv.Itoa(build))
params.Set("mobi_app", mobiApp)
params.Set("device", device)
params.Set("open_event", openEvent)
params.Set("ad_extra", adExtra)
// 老接口做兼容
if style != 0 {
if style != 2 {
style = 1
}
params.Set("style", strconv.Itoa(style))
}
var res struct {
Code int `json:"code"`
Data *cm.Ad `json:"data"`
}
if err = d.client.Get(c, d.bce, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.bce+"?"+params.Encode())
return
}
if res.Data != nil {
res.Data.ClientIP = ip
}
advert = res.Data
return
}

View File

@ -0,0 +1,47 @@
package ad
import (
"context"
"flag"
"os"
"testing"
"time"
"go-common/app/interface/main/app-feed/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-feed")
flag.Set("conf_token", "OC30xxkAOyaH9fI6FRuXA0Ob5HL0f3kc")
flag.Set("tree_id", "2686")
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 TestAd(t *testing.T) {
Convey("TestAd", t, func(ctx C) {
gotAdvert, err := d.Ad(context.Background(), 121212, 0, "", []int64{1221}, "", "", "", "", "", "", "", "", 1, time.Now())
So(gotAdvert, ShouldNotBeNil)
So(err, ShouldBeNil)
})
}