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,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 = ["bangumi_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/web-show/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["bangumi.go"],
importpath = "go-common/app/interface/main/web-show/dao/bangumi",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/web-show/conf:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,52 @@
package bangumi
import (
"context"
"net/url"
"strconv"
"time"
"go-common/app/interface/main/web-show/conf"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
)
// Dao struct
type Dao struct {
client *httpx.Client
isbpURL string
}
// New Init
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
client: httpx.NewClient(c.HTTPClient),
isbpURL: c.Host.Bangumi + "/api/bp",
}
return
}
// IsBp check user bp.
func (dao *Dao) IsBp(c context.Context, mid, aid int64, ip string) (is bool) {
params := url.Values{}
params.Set("build", "web-show")
params.Set("platform", "Golang")
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Result bool `json:"result"`
}
if err := dao.client.Get(c, dao.isbpURL, ip, params, &res); err != nil {
log.Error("bangumi url(%s) error(%v) ", dao.isbpURL+"?"+params.Encode(), err)
return
}
// FIXME why two state
if res.Code != 0 && res.Code != 1 {
log.Error("bangumi Isbp api fail(%d)", res.Code)
return
}
is = res.Result
return
}

View File

@@ -0,0 +1,36 @@
package bangumi
import (
"context"
"flag"
"path/filepath"
"testing"
"go-common/app/interface/main/web-show/conf"
. "github.com/smartystreets/goconvey/convey"
)
var d *Dao
func WithDao(f func(d *Dao)) func() {
return func() {
dir, _ := filepath.Abs("../cmd/web-show-test.toml")
flag.Set("conf", dir)
conf.Init()
if d == nil {
d = New(conf.Conf)
}
f(d)
}
}
func TestDao_Bangumi(t *testing.T) {
Convey("test Bangumi", t, WithDao(func(d *Dao) {
mid := int64(5187977)
aid := int64(2)
data := d.IsBp(context.TODO(), mid, aid, "")
So(data, ShouldNotBeNil)
Printf("%+v", data)
}))
}