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,57 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"dynamic.go",
"ugc.go",
],
importpath = "go-common/app/job/main/web-goblin/dao/web",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/web-goblin/conf:go_default_library",
"//library/database/elastic: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",
],
)
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 = [
"dao_test.go",
"dynamic_test.go",
"ugc_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/web-goblin/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,49 @@
package web
import (
"context"
"go-common/app/job/main/web-goblin/conf"
"go-common/library/database/elastic"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/stat/prom"
)
const _broadURL = "/x/internal/broadcast/push/all"
// Dao dao
type Dao struct {
c *conf.Config
// http client
http *bm.Client
// broadcast URL
broadcastURL string
ela *elastic.Elastic
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
http: bm.NewClient(c.HTTPClient),
broadcastURL: c.Host.API + _broadURL,
ela: elastic.NewElastic(nil),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) error {
return nil
}
// PromError stat and log.
func PromError(name string, format string, args ...interface{}) {
prom.BusinessErrCount.Incr(name)
log.Error(format, args...)
}

View File

@@ -0,0 +1,36 @@
package web
import (
"flag"
"os"
"testing"
"go-common/app/job/main/web-goblin/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.web-svr.web-goblin-job")
flag.Set("conf_token", "12e333e44103d9cba0d6c967f37dd311")
flag.Set("tree_id", "42274")
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/web-goblin-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,35 @@
package web
import (
"context"
"net/url"
"strconv"
"go-common/library/ecode"
"go-common/library/log"
)
const (
_opt = "1002"
_platform = "web"
)
// PushAll broadcast push all
func (d *Dao) PushAll(c context.Context, msg string, ip string) (err error) {
var res struct {
Code int `json:"code"`
}
params := url.Values{}
params.Set("operation", _opt)
params.Set("speed", strconv.Itoa(d.c.Rule.BroadFeed))
params.Set("platform", _platform)
params.Set("message", msg)
if err = d.http.Post(c, d.broadcastURL, ip, 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
}

View File

@@ -0,0 +1,22 @@
package web
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestWebPushAll(t *testing.T) {
var (
c = context.Background()
msg = `{"1":64060766,"180":23753334,"31":49310729,"live":516587}`
ip = "127.0.0.1"
)
convey.Convey("PushAll", t, func(ctx convey.C) {
err := d.PushAll(c, msg, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,19 @@
package web
import (
"context"
"go-common/library/log"
)
const _ugcIncre = "web_goblin"
// UgcSearch ugc insert .
func (d *Dao) UgcSearch(ctx context.Context, data map[string]interface{}) (err error) {
insert := d.ela.NewUpdate(_ugcIncre).Insert()
insert.AddData(_ugcIncre, data)
if err = insert.Do(ctx); err != nil {
log.Error("insert.Do error(%v)", err)
}
return
}

View File

@@ -0,0 +1,23 @@
package web
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestWebUgcSearch(t *testing.T) {
convey.Convey("UgcSearch", t, func(ctx convey.C) {
var (
c = context.Background()
data map[string]interface{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.UgcSearch(c, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}