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,65 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"channel_test.go",
"dao_test.go",
"pgc_test.go",
"recruit_test.go",
"ugc_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/web-goblin/conf:go_default_library",
"//app/interface/main/web-goblin/model/web:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"channel.go",
"dao.go",
"pgc.go",
"recruit.go",
"ugc.go",
],
importpath = "go-common/app/interface/main/web-goblin/dao/web",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/web-goblin/conf:go_default_library",
"//app/interface/main/web-goblin/model/web:go_default_library",
"//library/database/elastic:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata: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"],
)

View File

@@ -0,0 +1,31 @@
package web
import (
"context"
"time"
"go-common/app/interface/main/web-goblin/model/web"
)
const _cardSQL = "SELECT id,title,tag_id,card_type,card_value,recommand_reason,recommand_state,priority FROM channel_card WHERE stime<? AND etime>? AND `check`=2 AND is_delete=0 ORDER BY priority DESC"
// ChCard channel card.
func (d *Dao) ChCard(ctx context.Context, now time.Time) (res map[int64][]*web.ChCard, err error) {
res = map[int64][]*web.ChCard{}
rows, err := d.showDB.Query(ctx, _cardSQL, now, now)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
c := &web.ChCard{}
if err = rows.Scan(&c.ID, &c.Title, &c.ChannelID, &c.Type, &c.Value, &c.Reason, &c.ReasonType, &c.Pos); err != nil {
return
}
res[c.ChannelID] = append(res[c.ChannelID], c)
}
if err = rows.Err(); err != nil {
return
}
return
}

View File

@@ -0,0 +1,22 @@
package web
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestWebChCard(t *testing.T) {
convey.Convey("ChCard", t, func(c convey.C) {
var (
ctx = context.Background()
now = time.Now()
)
res, err := d.ChCard(ctx, now)
convey.So(err, convey.ShouldBeNil)
convey.So(res, convey.ShouldNotBeNil)
convey.Printf("%+v", res)
})
}

View File

@@ -0,0 +1,56 @@
package web
import (
"context"
"go-common/app/interface/main/web-goblin/conf"
"go-common/library/database/elastic"
"go-common/library/database/sql"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/stat/prom"
)
const (
_pgcFullURL = "/ext/internal/archive/channel/content"
_pgcIncreURL = "/ext/internal/archive/channel/content/change"
)
// Dao dao .
type Dao struct {
c *conf.Config
db *sql.DB
showDB *sql.DB
httpR *bm.Client
pgcFullURL, pgcIncreURL string
ela *elastic.Elastic
}
// New init mysql db .
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
db: sql.NewMySQL(c.DB.Goblin),
showDB: sql.NewMySQL(c.DB.Show),
httpR: bm.NewClient(c.SearchClient),
pgcFullURL: c.Host.PgcURI + _pgcFullURL,
pgcIncreURL: c.Host.PgcURI + _pgcIncreURL,
ela: elastic.NewElastic(c.Es),
}
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,44 @@
package web
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/interface/main/web-goblin/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.web-svr.web-goblin")
flag.Set("conf_token", "b2850ed0834343a9e435809857f5670d")
flag.Set("tree_id", "41730")
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-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
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,65 @@
package web
import (
"context"
"net/url"
"strconv"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// PgcFull pgc full .
func (d *Dao) PgcFull(ctx context.Context, tp int, pn, ps int64, source string) (res interface{}, err error) {
var (
param = url.Values{}
ip = metadata.String(ctx, metadata.RemoteIP)
rs struct {
Code int `json:"code"`
Data interface{} `json:"result"`
}
)
param.Set("bsource", source)
param.Set("season_type", strconv.Itoa(tp))
param.Set("page_no", strconv.FormatInt(pn, 10))
param.Set("page_size", strconv.FormatInt(ps, 10))
if err = d.httpR.Get(ctx, d.pgcFullURL, ip, param, &rs); err != nil {
log.Error("d.httpR.Get err(%v)", err)
return
}
if rs.Code != ecode.OK.Code() {
err = ecode.Int(rs.Code)
return
}
res = rs.Data
return
}
// PgcIncre pgc increment .
func (d *Dao) PgcIncre(ctx context.Context, tp int, pn, ps, start, end int64, source string) (res interface{}, err error) {
var (
param = url.Values{}
ip = metadata.String(ctx, metadata.RemoteIP)
)
var rs struct {
Code int `json:"code"`
Data interface{} `json:"result"`
}
param.Set("bsource", source)
param.Set("season_type", strconv.Itoa(tp))
param.Set("page_no", strconv.FormatInt(pn, 10))
param.Set("page_size", strconv.FormatInt(ps, 10))
param.Set("start_ts", strconv.FormatInt(start, 10))
param.Set("end_ts", strconv.FormatInt(end, 10))
if err = d.httpR.Get(ctx, d.pgcIncreURL, ip, param, &rs); err != nil {
log.Error("d.httpR.Get url(%s) err(%s)", d.pgcIncreURL+"?"+param.Encode(), err)
return
}
if rs.Code != ecode.OK.Code() {
err = ecode.Int(rs.Code)
return
}
res = rs.Data
return
}

View File

@@ -0,0 +1,48 @@
package web
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestWebPgcFull(t *testing.T) {
convey.Convey("PgcFull", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int(1)
pn = int64(1)
ps = int64(10)
source = "youku"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.PgcFull(c, tp, pn, ps, source)
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 TestWebPgcIncre(t *testing.T) {
convey.Convey("PgcIncre", t, func(ctx convey.C) {
var (
c = context.Background()
tp = int(2)
pn = int64(1)
ps = int64(10)
start = int64(1505876448)
end = int64(1505876450)
source = "youku"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.PgcIncre(c, tp, pn, ps, start, end, source)
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)
})
})
})
}

View File

@@ -0,0 +1,37 @@
package web
import (
"context"
"encoding/json"
xhttp "net/http"
"net/url"
"go-common/app/interface/main/web-goblin/model/web"
"go-common/library/ecode"
"go-common/library/log"
)
// Recruit .
func (d *Dao) Recruit(ctx context.Context, param url.Values, route *web.Params) (res json.RawMessage, err error) {
var (
req *xhttp.Request
rs json.RawMessage
mokaURI = d.c.Recruit.MokaURI + "/" + route.Route + "/" + d.c.Recruit.Orgid
)
if route.JobID != "" {
mokaURI = mokaURI + "/" + route.JobID
}
param.Del("route")
param.Del("job_id")
if req, err = xhttp.NewRequest("GET", mokaURI+"?"+param.Encode(), nil); err != nil {
log.Error("Recruit xhttp.NewRequest url(%s) error(%v)", mokaURI, err)
return
}
if err = d.httpR.Do(ctx, req, &rs); err != nil {
log.Error("Recruit d.httpR.Do url(%s) error(%v)", mokaURI, err)
err = ecode.NothingFound
return
}
res = rs
return
}

View File

@@ -0,0 +1,32 @@
package web
import (
"context"
"net/url"
"testing"
"go-common/app/interface/main/web-goblin/model/web"
"github.com/smartystreets/goconvey/convey"
)
func TestWebRecruit(t *testing.T) {
convey.Convey("Recruit", t, func(ctx convey.C) {
var (
c = context.Background()
params = url.Values{}
ru = &web.Params{
Route: "v1/jobs",
}
)
params.Set("mode", "social")
ctx.Convey("When everything gose positive", func(ctx convey.C) {
httpMock("GET", "https://api.mokahr.com/v1/jobs/bilibili").Reply(200).JSON(`{jobs:[], "total": 245}`)
res, err := d.Recruit(c, params, ru)
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)
})
})
})
}

View File

@@ -0,0 +1,31 @@
package web
import (
"context"
"time"
webmdl "go-common/app/interface/main/web-goblin/model/web"
"go-common/library/database/elastic"
"go-common/library/log"
)
const _ugcIncre = "web_goblin"
// UgcIncre ugc increment .
func (d *Dao) UgcIncre(ctx context.Context, pn, ps int, start, end int64) (res []*webmdl.SearchAids, err error) {
var (
startStr, endStr string
rs struct {
Result []*webmdl.SearchAids `json:"result"`
}
)
startStr = time.Unix(start, 0).Format("2006-01-02 15:04:05")
endStr = time.Unix(end, 0).Format("2006-01-02 15:04:05")
r := d.ela.NewRequest(_ugcIncre).WhereRange("mtime", startStr, endStr, elastic.RangeScopeLoRo).Fields("aid").Fields("action").Index(_ugcIncre).Pn(pn).Ps(ps)
if err = r.Scan(ctx, &rs); err != nil {
log.Error("r.Scan error(%v)", err)
return
}
res = rs.Result
return
}

View File

@@ -0,0 +1,27 @@
package web
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestWebUgcIncre(t *testing.T) {
convey.Convey("UgcIncre", t, func(ctx convey.C) {
var (
c = context.Background()
pn = int(1)
ps = int(10)
start = int64(1505876448)
end = int64(1505876450)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.UgcIncre(c, pn, ps, start, end)
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)
})
})
})
}