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,50 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["pendant_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/answer/conf:go_default_library",
"//vendor/github.com/go-sql-driver/mysql:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["pendant.go"],
importpath = "go-common/app/interface/main/answer/dao/account",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/answer/conf:go_default_library",
"//app/interface/main/answer/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/log: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,114 @@
package account
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/app/interface/main/answer/conf"
"go-common/app/interface/main/answer/model"
"go-common/library/cache/memcache"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"github.com/pkg/errors"
)
// Dao is elec dao.
type Dao struct {
c *conf.Config
mc *memcache.Pool
client *bm.Client
pendant string
beFormal string
extraIds string
}
const (
_wasFormal = -659
)
// New pendant dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
mc: memcache.NewPool(c.Memcache.Config),
client: bm.NewClient(c.HTTPClient.Normal),
pendant: c.Host.API + _multiGivePendant,
beFormal: c.Host.Account + _beFormal,
extraIds: c.Host.ExtraIds,
}
return
}
const (
_multiGivePendant = "/x/internal/pendant/multiGrantByMid"
_beFormal = "/api/internal/member/beFormal"
)
// GivePendant send user pendant
func (d *Dao) GivePendant(c context.Context, mid int64, pid int64, days int, ip string) (err error) {
log.Info(" GivePendant (%d,%d,%d) ", mid, pid, days)
params := url.Values{}
params.Set("mids", strconv.FormatInt(mid, 10))
params.Set("pid", strconv.FormatInt(pid, 10))
params.Set("expire", strconv.FormatInt(int64(days), 10))
var res struct {
Code int `json:"code"`
}
if err = d.client.Post(c, d.pendant, ip, params, &res); err != nil {
log.Error("pendant url(%s) error(%v)", d.pendant+"?"+params.Encode(), err)
log.Error("GivePendant(%d,%d),err:%+v", mid, pid, err)
return
}
if res.Code != 0 {
err = fmt.Errorf("pendant GivePendant failed(%v)", res.Code)
log.Error(" d.client.Get(%s) error(%v)", d.pendant+"?"+params.Encode(), err)
return
}
return
}
// BeFormal become a full member
func (d *Dao) BeFormal(c context.Context, mid int64, ip string) (err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
}
if err = d.client.Post(c, d.beFormal, ip, params, &res); err != nil {
err = errors.Wrapf(err, "beFormal url(%s)", d.beFormal+"?"+params.Encode())
log.Error("BeFormal(%d),err:%+v", mid, err)
return
}
if res.Code != 0 && res.Code != _wasFormal {
err = errors.WithStack(fmt.Errorf("beFormal(%d) failed(%v)", mid, res.Code))
log.Error("BeFormal(%d),res:%+v", mid, res)
return
}
log.Info("beFormal suc(%d) ", mid)
return
}
// ExtraIds BigData Extra Question ids.
func (d *Dao) ExtraIds(c context.Context, mid int64, ip string) (done []int64, pend []int64, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data *model.ExtraBigData
}
if err = d.client.Get(c, d.extraIds, ip, params, &res); err != nil {
log.Error("ExtraIds url(%s) error(%v)", d.extraIds+"?"+params.Encode(), err)
return
}
if res.Code != 0 || res.Data == nil {
err = fmt.Errorf("ExtraIds failed(%v)", res.Code)
log.Error(" d.client.Get(%s) res(%v) error(%v)", d.extraIds+"?"+params.Encode(), res, err)
return
}
done = res.Data.Done
pend = res.Data.Pend
return
}

View File

@@ -0,0 +1,74 @@
package account
import (
"context"
"flag"
"os"
"testing"
"go-common/app/interface/main/answer/conf"
_ "github.com/go-sql-driver/mysql"
. "github.com/smartystreets/goconvey/convey"
)
var d *Dao
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account-law.answer")
flag.Set("conf_appid", "main.account-law.answer")
flag.Set("conf_token", "ba3ee255695e8d7b46782268ddc9c8a3")
flag.Set("tree_id", "25260")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_env", "10")
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/answer-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
// Test_BeFormal .
func Test_BeFormal(t *testing.T) {
Convey("Test_BeFormal", t, func() {
var (
c = context.Background()
mid = int64(100000245)
)
d.BeFormal(c, mid, "127.0.0.1")
})
}
// Test_GivePendant .
func Test_GivePendant(t *testing.T) {
Convey("Test_GivePendant", t, func() {
err := d.GivePendant(context.Background(), 21432418, 1, 1, "127.0.0.1")
So(err, ShouldBeNil)
})
}
// go test -test.v -test.run TestDaoExtraIds
func Test_ExtraIds(t *testing.T) {
Convey("Test_GivePendant", t, func() {
var (
err error
c = context.Background()
mid = int64(100000245)
ds, ps []int64
)
ds, ps, err = d.ExtraIds(c, mid, "127.0.0.1")
So(err, ShouldBeNil)
So(ds, ShouldNotBeNil)
So(ps, ShouldNotBeNil)
})
}