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,56 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"dao.go",
],
importpath = "go-common/app/interface/main/creative/dao/growup",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/growup:go_default_library",
"//library/ecode: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"],
)
go_test(
name = "go_default_test",
srcs = [
"api_test.go",
"dao_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)

View File

@@ -0,0 +1,200 @@
package growup
import (
"context"
"net/url"
"strconv"
"time"
"go-common/app/interface/main/creative/model/growup"
"go-common/library/ecode"
"go-common/library/log"
)
// UpStatus get up info.
func (d *Dao) UpStatus(c context.Context, mid int64, ip string) (us *growup.UpStatus, err error) {
us = &growup.UpStatus{}
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data *growup.UpStatus
}
if err = d.client.Get(c, d.upStatusURL, ip, params, &res); err != nil {
log.Error("growup url(%s) response(%v) error(%v)", d.upStatusURL+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
if res.Code != 0 {
log.Error("growup url(%s) res(%v)", d.upStatusURL, res)
err = ecode.CreativeOrderAPIErr
return
}
us = res.Data
return
}
// UpInfo get up info.
func (d *Dao) UpInfo(c context.Context, mid int64, ip string) (ui *growup.UpInfo, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data *growup.UpInfo
}
if err = d.client.Get(c, d.upInfoURL, ip, params, &res); err != nil {
log.Error("growup url(%s) response(%v) error(%v)", d.upInfoURL+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
if res.Code != 0 {
log.Error("growup url(%s) res(%v)", d.upInfoURL, res)
err = ecode.CreativeOrderAPIErr
return
}
ui = res.Data
return
}
//Join join growup.
func (d *Dao) Join(c context.Context, mid int64, accTy, signTy int, ip string) (err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("account_type", strconv.Itoa(accTy))
params.Set("sign_type", strconv.Itoa(signTy))
var res struct {
Code int `json:"code"`
Message string `json:"message"`
}
if err = d.client.Post(c, d.joinURL, ip, params, &res); err != nil {
log.Error("growup url(%s) mid(%d) error(%v)", d.joinURL+"?"+params.Encode(), mid, err)
err = ecode.CreativeOrderAPIErr
return
}
if res.Code != 0 {
log.Error("growup url(%s) mid(%d) res.code(%d) error(%v)", d.joinURL+"?"+params.Encode(), mid, res.Code, err)
err = ecode.CreativeOrderAPIErr
}
return
}
//Quit quit growup.
func (d *Dao) Quit(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"`
Message string `json:"message"`
}
if err = d.client.Post(c, d.quitURL, ip, params, &res); err != nil {
log.Error("growup url(%s) mid(%d) error(%v)", d.quitURL+"?"+params.Encode(), mid, err)
err = ecode.CreativeOrderAPIErr
return
}
if res.Code != 0 {
log.Error("growup url(%s) mid(%d) res.code(%d) error(%v)", d.quitURL+"?"+params.Encode(), mid, res.Code, err)
err = ecode.CreativeOrderAPIErr
}
return
}
// Summary income.
func (d *Dao) Summary(c context.Context, mid int64, ip string) (sm *growup.Summary, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
var res struct {
Code int `json:"code"`
Data *growup.Summary
Message string `json:"message"`
}
if err = d.client.Get(c, d.summaryURL, ip, params, &res); err != nil {
log.Error("growup url(%s) response(%v) error(%v)", d.summaryURL+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
if res.Code != 0 {
log.Error("growup url(%s) res(%v)", d.summaryURL, res)
err = ecode.CreativeOrderAPIErr
return
}
sm = res.Data
return
}
// Stat income by month.
func (d *Dao) Stat(c context.Context, mid int64, ip string) (st *growup.Stat, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
var res struct {
Code int `json:"code"`
Data *growup.Stat
Message string `json:"message"`
}
if err = d.client.Get(c, d.statURL, ip, params, &res); err != nil {
log.Error("growup url(%s) response(%v) error(%v)", d.statURL+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
if res.Code != 0 {
log.Error("growup url(%s) res(%v)", d.statURL, res)
err = ecode.CreativeOrderAPIErr
return
}
st = res.Data
return
}
// IncomeList income by video/article/music.
func (d *Dao) IncomeList(c context.Context, mid int64, ty, pn, ps int, ip string) (il *growup.IncomeList, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("type", strconv.Itoa(ty))
params.Set("page", strconv.Itoa(pn))
params.Set("size", strconv.Itoa(ps))
params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
var res struct {
Code int `json:"code"`
Data *growup.IncomeList
Message string `json:"message"`
}
if err = d.client.Get(c, d.arcURL, ip, params, &res); err != nil {
log.Error("growup url(%s) response(%v) error(%v)", d.arcURL+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
if res.Code != 0 {
log.Error("growup url(%s) res(%v)", d.arcURL, res)
err = ecode.CreativeOrderAPIErr
return
}
il = res.Data
return
}
// BreachList breach list.
func (d *Dao) BreachList(c context.Context, mid int64, pn, ps int, ip string) (bl *growup.BreachList, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("page", strconv.Itoa(pn))
params.Set("size", strconv.Itoa(ps))
params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
var res struct {
Code int `json:"code"`
Data *growup.BreachList
Message string `json:"message"`
}
if err = d.client.Get(c, d.breachURL, ip, params, &res); err != nil {
log.Error("growup url(%s) response(%v) error(%v)", d.breachURL+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
if res.Code != 0 {
log.Error("growup url(%s) res(%v)", d.breachURL, res)
err = ecode.CreativeOrderAPIErr
return
}
bl = res.Data
return
}

View File

@@ -0,0 +1,142 @@
package growup
import (
"context"
"go-common/library/ecode"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestGrowupUpStatus(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("UpStatus", t, func(ctx convey.C) {
us, err := d.UpStatus(c, mid, ip)
ctx.Convey("Then err should be nil.us should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.CreativeOrderAPIErr)
ctx.So(us, convey.ShouldNotBeNil)
})
})
}
func TestGrowupUpInfo(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("UpInfo", t, func(ctx convey.C) {
ui, err := d.UpInfo(c, mid, ip)
ctx.Convey("Then err should be nil.ui should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(ui, convey.ShouldBeNil)
})
})
}
func TestGrowupJoin(t *testing.T) {
var (
c = context.TODO()
mid = int64(0)
accTy = int(0)
signTy = int(0)
ip = ""
)
convey.Convey("Join", t, func(ctx convey.C) {
err := d.Join(c, mid, accTy, signTy, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.CreativeOrderAPIErr)
})
})
}
func TestGrowupQuit(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("Quit1", t, func(ctx convey.C) {
httpMock("POST", d.quitURL).Reply(200).JSON(`{"code":20008}`)
err := d.Quit(c, mid, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
convey.Convey("Quit2", t, func(ctx convey.C) {
httpMock("POST", d.quitURL).Reply(502)
err := d.Quit(c, mid, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
}
func TestGrowupSummary(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("Summary", t, func(ctx convey.C) {
sm, err := d.Summary(c, mid, ip)
ctx.Convey("Then err should be nil.sm should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.CreativeOrderAPIErr)
ctx.So(sm, convey.ShouldBeNil)
})
})
}
func TestGrowupStat(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("Stat", t, func(ctx convey.C) {
st, err := d.Stat(c, mid, ip)
ctx.Convey("Then err should be nil.st should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.CreativeOrderAPIErr)
ctx.So(st, convey.ShouldBeNil)
})
})
}
func TestGrowupIncomeList(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ty = int(0)
pn = int(1)
ps = int(10)
ip = "127.0.0.1"
)
convey.Convey("IncomeList", t, func(ctx convey.C) {
il, err := d.IncomeList(c, mid, ty, pn, ps, ip)
ctx.Convey("Then err should be nil.il should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.CreativeOrderAPIErr)
ctx.So(il, convey.ShouldBeNil)
})
})
}
func TestGrowupBreachList(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
pn = int(1)
ps = int(10)
ip = "127.0.0.1"
)
convey.Convey("BreachList", t, func(ctx convey.C) {
bl, err := d.BreachList(c, mid, pn, ps, ip)
ctx.Convey("Then err should be nil.bl should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.CreativeOrderAPIErr)
ctx.So(bl, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,56 @@
package growup
import (
httpx "go-common/library/net/http/blademaster"
"go-common/app/interface/main/creative/conf"
)
const (
//up check
_upStatus = "/allowance/api/x/internal/growup/up/status"
_upInfo = "/allowance/api/x/internal/growup/up/info"
_join = "/allowance/api/x/internal/growup/up/add"
_quit = "/allowance/api/x/internal/growup/up/quit"
//up income
_summary = "/up-openapi/api/open_api/v1/income/summary"
_stat = "/up-openapi/api/open_api/v1/income/statis"
_arc = "/up-openapi/api/open_api/v1/income/archive"
_breach = "/up-openapi/api/open_api/v1/income/breach"
)
// Dao define
type Dao struct {
c *conf.Config
// http
client *httpx.Client
// up check uri
upStatusURL string
upInfoURL string
joinURL string
quitURL string
// up income uri
summaryURL string
statURL string
arcURL string
breachURL string
}
// New init dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
client: httpx.NewClient(c.HTTPClient.UpMng),
//up check
upStatusURL: c.Host.Growup + _upStatus,
upInfoURL: c.Host.Growup + _upInfo,
joinURL: c.Host.Growup + _join,
quitURL: c.Host.Growup + _quit,
//up check
summaryURL: c.Host.UpMng + _summary,
statURL: c.Host.UpMng + _stat,
arcURL: c.Host.UpMng + _arc,
breachURL: c.Host.UpMng + _breach,
}
return
}

View File

@@ -0,0 +1,45 @@
package growup
import (
"flag"
"go-common/app/interface/main/creative/conf"
"os"
"strings"
"testing"
gock "gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative")
flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
flag.Set("tree_id", "2305")
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/creative.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
d.client.SetTransport(gock.DefaultTransport)
return r
}