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,62 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"cache.go",
"dao.go",
"mc.cache.go",
"push.go",
"wechat.go",
],
importpath = "go-common/app/job/main/activity/dao/bnj",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/activity/conf:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/stat/prom:go_default_library",
"//library/xstr: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 = [
"cache_test.go",
"dao_test.go",
"mc.cache_test.go",
"push_test.go",
"wechat_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/job/main/activity/conf: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,23 @@
package bnj
import "context"
func timeFinishKey() string {
return "time_finish"
}
func lessTimeKey() string {
return "time_less"
}
//go:generate $GOPATH/src/go-common/app/tool/cache/mc
type _mc interface {
// mc: -key=timeFinishKey
CacheTimeFinish(c context.Context) (int64, error)
// mc: -key=timeFinishKey -expire=d.timeFinishExpire -encode=raw
AddCacheTimeFinish(c context.Context, value int64) error
// mc: -key=lessTimeKey
CacheLessTime(c context.Context) (int64, error)
// mc: -key=lessTimeKey -expire=d.lessTimeExpire -encode=raw
AddCacheLessTime(c context.Context, value int64) error
}

View File

@@ -0,0 +1,29 @@
package bnj
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestBnjtimeFinishKey(t *testing.T) {
convey.Convey("timeFinishKey", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := timeFinishKey()
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestBnjlessTimeKey(t *testing.T) {
convey.Convey("lessTimeKey", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := lessTimeKey()
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,34 @@
package bnj
import (
"time"
"go-common/app/job/main/activity/conf"
"go-common/library/cache/memcache"
"go-common/library/net/http/blademaster"
)
// Dao .
type Dao struct {
c *conf.Config
client *blademaster.Client
mc *memcache.Pool
broadcastURL string
messageURL string
timeFinishExpire int32
lessTimeExpire int32
}
// New .
func New(c *conf.Config) *Dao {
d := &Dao{
c: c,
client: blademaster.NewClient(c.HTTPClient),
mc: memcache.NewPool(c.Memcache.Like),
}
d.broadcastURL = d.c.Host.APICo + _broadURL
d.messageURL = d.c.Host.MsgCo + _messageURL
d.timeFinishExpire = int32(time.Duration(c.Memcache.TimeFinishExpire) / time.Second)
d.lessTimeExpire = int32(time.Duration(c.Memcache.LessTimeExpire) / time.Second)
return d
}

View File

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

View File

@@ -0,0 +1,124 @@
// Code generated by $GOPATH/src/go-common/app/tool/cache/mc. DO NOT EDIT.
/*
Package bnj is a generated mc cache package.
It is generated from:
type _mc interface {
// mc: -key=timeFinishKey
CacheTimeFinish(c context.Context) (int64, error)
// mc: -key=timeFinishKey -expire=d.timeFinishExpire -encode=raw
AddCacheTimeFinish(c context.Context, value int64) error
// mc: -key=lessTimeKey
CacheLessTime(c context.Context) (int64, error)
// mc: -key=lessTimeKey -expire=d.lessTimeExpire -encode=raw
AddCacheLessTime(c context.Context, value int64) error
}
*/
package bnj
import (
"context"
"fmt"
"strconv"
"go-common/library/cache/memcache"
"go-common/library/log"
"go-common/library/stat/prom"
)
var _ _mc
// CacheTimeFinish get data from mc
func (d *Dao) CacheTimeFinish(c context.Context) (res int64, err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := timeFinishKey()
reply, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:CacheTimeFinish")
log.Errorv(c, log.KV("CacheTimeFinish", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
var v string
err = conn.Scan(reply, &v)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheTimeFinish")
log.Errorv(c, log.KV("CacheTimeFinish", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
r, err := strconv.ParseInt(v, 10, 64)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheTimeFinish")
log.Errorv(c, log.KV("CacheTimeFinish", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
res = int64(r)
return
}
// AddCacheTimeFinish Set data to mc
func (d *Dao) AddCacheTimeFinish(c context.Context, val int64) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := timeFinishKey()
bs := []byte(strconv.FormatInt(int64(val), 10))
item := &memcache.Item{Key: key, Value: bs, Expiration: d.timeFinishExpire, Flags: memcache.FlagRAW}
if err = conn.Set(item); err != nil {
prom.BusinessErrCount.Incr("mc:AddCacheTimeFinish")
log.Errorv(c, log.KV("AddCacheTimeFinish", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// CacheLessTime get data from mc
func (d *Dao) CacheLessTime(c context.Context) (res int64, err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := lessTimeKey()
reply, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:CacheLessTime")
log.Errorv(c, log.KV("CacheLessTime", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
var v string
err = conn.Scan(reply, &v)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheLessTime")
log.Errorv(c, log.KV("CacheLessTime", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
r, err := strconv.ParseInt(v, 10, 64)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheLessTime")
log.Errorv(c, log.KV("CacheLessTime", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
res = int64(r)
return
}
// AddCacheLessTime Set data to mc
func (d *Dao) AddCacheLessTime(c context.Context, val int64) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := lessTimeKey()
bs := []byte(strconv.FormatInt(int64(val), 10))
item := &memcache.Item{Key: key, Value: bs, Expiration: d.lessTimeExpire, Flags: memcache.FlagRAW}
if err = conn.Set(item); err != nil {
prom.BusinessErrCount.Incr("mc:AddCacheLessTime")
log.Errorv(c, log.KV("AddCacheLessTime", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}

View File

@@ -0,0 +1,68 @@
package bnj
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestBnjAddCacheTimeFinish(t *testing.T) {
convey.Convey("AddCacheTimeFinish", t, func(ctx convey.C) {
var (
c = context.Background()
val = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.AddCacheTimeFinish(c, val)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestBnjCacheTimeFinish(t *testing.T) {
convey.Convey("CacheTimeFinish", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.CacheTimeFinish(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestBnjCacheLessTime(t *testing.T) {
convey.Convey("CacheLessTime", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.CacheLessTime(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestBnjAddCacheLessTime(t *testing.T) {
convey.Convey("AddCacheLessTime", t, func(ctx convey.C) {
var (
c = context.Background()
val = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.AddCacheLessTime(c, val)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,60 @@
package bnj
import (
"context"
"net/url"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_opt = "1004"
_platform = "web"
_broadURL = "/x/internal/broadcast/push/all"
_messageURL = "/api/notify/send.user.notify.do"
_notify = "4"
)
// PushAll broadcast push all
func (d *Dao) PushAll(c context.Context, msg string) (err error) {
params := url.Values{}
params.Set("operation", _opt)
params.Set("platform", _platform)
params.Set("message", msg)
var res struct {
Code int `json:"code"`
}
if err = d.client.Post(c, d.broadcastURL, "", params, &res); err != nil {
log.Error("PushAll url(%s) error(%v)", d.broadcastURL+"?"+params.Encode(), err)
return
}
if res.Code != ecode.OK.Code() {
err = ecode.Int(res.Code)
}
return
}
// SendMessage send system notify.
func (d *Dao) SendMessage(c context.Context, mids []int64, mc, title, msg string) (err error) {
params := url.Values{}
params.Set("mid_list", xstr.JoinInts(mids))
params.Set("title", title)
params.Set("mc", mc)
params.Set("data_type", _notify)
params.Set("context", msg)
var res struct {
Code int `json:"code"`
}
err = d.client.Post(c, d.messageURL, "", params, &res)
if err != nil {
log.Error("SendMessage d.client.Post(%s) error(%+v)", d.messageURL+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("SendMessage url(%s) res code(%d)", d.messageURL+"?"+params.Encode(), res.Code)
err = ecode.Int(res.Code)
}
return
}

View File

@@ -0,0 +1,45 @@
package bnj
import (
"context"
"testing"
"gopkg.in/h2non/gock.v1"
"github.com/smartystreets/goconvey/convey"
)
func TestBnjPushAll(t *testing.T) {
convey.Convey("PushAll", t, func(ctx convey.C) {
var (
c = context.Background()
msg = `{"second":100,"name":"啊*"}`
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.PushAll(c, msg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestBnjSendMessage(t *testing.T) {
convey.Convey("SendMessage", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{2089809}
mc = "1_21_1"
title = "【bilibili2019拜年祭档案揭秘】001"
msg = "飞雪连天射白鹿笑书神侠倚碧鸳。当V家碰到金庸会碰撞出怎样的火花来拜年祭后台看看吧~"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("POST", d.messageURL).Reply(200).JSON(`{"code":0}`)
err := d.SendMessage(c, mids, mc, title, msg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,56 @@
package bnj
import (
"context"
"encoding/json"
"net/http"
"strings"
"go-common/library/ecode"
"go-common/library/log"
)
const (
_wechatAction = "NotifyCreate"
_wechatType = "wechat_message"
_wechatURL = "http://merak.bilibili.co"
)
// SendWechat send wechat work message.
func (d *Dao) SendWechat(c context.Context, title, msg, user string) (err error) {
var msgBytes []byte
params := map[string]interface{}{
"Action": _wechatAction,
"SendType": _wechatType,
"PublicKey": d.c.Bnj2019.WxKey,
"UserName": user,
"Content": map[string]string{
"subject": title,
"body": title + "\n" + msg,
},
"TreeId": "",
"Signature": "1",
"Severity": "P5",
}
if msgBytes, err = json.Marshal(params); err != nil {
return
}
var req *http.Request
if req, err = http.NewRequest(http.MethodPost, _wechatURL, strings.NewReader(string(msgBytes))); err != nil {
return
}
req.Header.Add("content-type", "application/json; charset=UTF-8")
res := &struct {
RetCode int `json:"RetCode"`
}{}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("SendWechat d.client.Do(title:%s,msg:%s,user:%s) error(%v)", title, msg, user, err)
return
}
if res.RetCode != 0 {
err = ecode.Int(res.RetCode)
log.Error("SendWechat d.client.Do(title:%s,msg:%s,user:%s) error(%v)", title, msg, user, err)
return
}
return
}

View File

@@ -0,0 +1,29 @@
package bnj
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestBnjSendWechat(t *testing.T) {
convey.Convey("SendWechat", t, func(ctx convey.C) {
var (
c = context.Background()
title = "【拜年祭必看!】拜年祭预约人数到达预警"
msg = "拜年祭预约人数即将到达50w请及时准备拜年祭抽奖事项。"
user = "wuhao02"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("POST", _wechatURL).Reply(200).JSON(`{"RetCode":0}`)
err := d.SendWechat(c, title, msg, user)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}