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,33 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["server.go"],
importpath = "go-common/app/service/openplatform/anti-fraud/server/grpc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/openplatform/anti-fraud/service:go_default_library",
"//library/net/rpc/warden:go_default_library",
"@org_golang_google_grpc//: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,27 @@
package grpc
import (
"context"
"go-common/app/service/openplatform/anti-fraud/service"
"go-common/library/net/rpc/warden"
"google.golang.org/grpc"
)
//New 生成rpc服务
func New(svc *service.Service) *warden.Server {
s := warden.NewServer(nil)
s.Use(middleware())
_, err := s.Start()
if err != nil {
panic("run server failed!" + err.Error())
}
return s
}
func middleware() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
//call chain
resp, err = handler(ctx, req)
return
}
}

View File

@@ -0,0 +1,64 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"http_test.go",
"question_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/openplatform/anti-fraud/conf:go_default_library",
"//app/service/openplatform/anti-fraud/model:go_default_library",
"//library/ecode:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"graph.go",
"http.go",
"question.go",
"risk.go",
],
importpath = "go-common/app/service/openplatform/anti-fraud/server/http",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/openplatform/anti-fraud/api/grpc/v1:go_default_library",
"//app/service/openplatform/anti-fraud/api/http/v1:go_default_library",
"//app/service/openplatform/anti-fraud/conf:go_default_library",
"//app/service/openplatform/anti-fraud/model:go_default_library",
"//app/service/openplatform/anti-fraud/service:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/binding:go_default_library",
"//library/net/http/blademaster/middleware/verify: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,24 @@
package http
import (
"go-common/app/service/openplatform/anti-fraud/api/grpc/v1"
bm "go-common/library/net/http/blademaster"
)
//graphPrepare 拉起图形验证
func graphPrepare(c *bm.Context) {
params := new(v1.GraphPrepareRequest)
if err := c.Bind(params); err != nil {
return
}
c.JSON(svc.GraphPrepare(c, params))
}
//graphCheck 图形验证
func graphCheck(c *bm.Context) {
params := new(v1.GraphCheckRequest)
if err := c.Bind(params); err != nil {
return
}
c.JSON(svc.GraphCheck(c, params))
}

View File

@@ -0,0 +1,91 @@
package http
import (
"go-common/app/service/openplatform/anti-fraud/conf"
"go-common/app/service/openplatform/anti-fraud/service"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/verify"
"net/http"
)
var (
vfySvc *verify.Verify
svc *service.Service
)
// Init init
func Init(c *conf.Config) {
initService(c)
// init router
engine := bm.DefaultServer(nil)
innerRouter(engine)
if err := engine.Start(); err != nil {
log.Error("engine.Start error(%v)", err)
panic(err)
}
}
// initService init services.
func initService(c *conf.Config) {
vfySvc = verify.New(nil)
svc = service.New(c)
}
// innerRouter init inner router api path.
func innerRouter(e *bm.Engine) {
e.Ping(ping)
//group := e.Group("/openplatform/internal/anti/fraud",idfSvc.Verify)
group := e.Group("/openplatform/internal/antifraud")
{
group.GET("/qusb/info", qusBankInfo) //题库单条信息
group.GET("/qusb/list", qusBankList) //题库列表
group.POST("/qusb/add", qusBankAdd) //题库添加
group.POST("/qusb/del", qusBankDel) //题库删除
group.POST("/qusb/update", qusBankEdit) //题库修改
group.POST("/qusb/check", qusBankCheck) // 答题
group.GET("/qs/info", qusInfo) //题目信息
group.GET("/qs/list", qusList) //题目列表
group.POST("/qs/add", qusAdd) //题目添加
group.POST("/qs/update", qusUpdate) //题目更新
group.POST("/qs/del", qusDel) //题目删除
group.GET("/qs/get", getQuestion) //题目获取
group.POST("/qs/answer", answerQuestion) // 答题
group.POST("/bind", questionBankBind) // 绑定题库
group.POST("/unbind", questionBankUnbind) // 解绑题库
group.POST("/bind/bank", getBankBind) // 查询已绑定的题库
group.GET("/bind/items", getBindItems) // 查询绑定到题库的 items
group.GET("/risk/check", riskCheck) //风险检验
group.POST("/risk/check/v2", riskCheckV2) //风险检验v2
group.GET("/graph/prepare", graphPrepare) //拉起图形验证
group.POST("/graph/check", graphCheck) //图形验证
}
group2 := e.Group("/openplatform/admin/antifraud/shield")
{
group2.GET("/risk/ip/list", ipList) //ip列表
group2.GET("/risk/ip/detail", ipDetail) //ip详情列表
group2.GET("/risk/uid/list", uidList) //uid列表
group2.GET("/risk/uid/detail", uidDetail) //uid详情列表
group2.POST("/risk/ip/black", ipBlack) //设置ip黑名单
group2.POST("/risk/uid/black", uidBlack) //设置uid黑名单
}
}
// outerRouter init outer router.
//func outerRouter(e *bm.Engine) {
//}
// ping check server ok.
func ping(c *bm.Context) {
if err := svc.Ping(c); err != nil {
log.Error("open-abtest http service ping error(%v)", err)
c.AbortWithStatus(http.StatusServiceUnavailable)
}
}

View File

@@ -0,0 +1,22 @@
package http
import (
"flag"
"fmt"
"go-common/app/service/openplatform/anti-fraud/conf"
httpx "go-common/library/net/http/blademaster"
_ "github.com/smartystreets/goconvey/convey"
)
var client *httpx.Client
func init() {
flag.Parse()
if err := conf.Init(); err != nil {
panic(fmt.Errorf("conf.Init() error(%v)", err))
}
client = httpx.NewClient(conf.Conf.HTTPClient.Read)
Init(conf.Conf)
}

View File

@@ -0,0 +1,245 @@
package http
import (
"go-common/app/service/openplatform/anti-fraud/model"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/binding"
)
// qusBankInfo 题库信息
func qusBankInfo(c *bm.Context) {
params := new(model.ArgGetQusBank)
if err := c.Bind(params); err != nil {
return
}
c.JSON(svc.GetQusBankInfo(c, params.QsBId))
}
// qusBankList 题库列表
func qusBankList(c *bm.Context) {
params := new(model.ArgBankList)
if err := c.Bind(params); err != nil {
return
}
result := model.RespList{}
list, err := svc.GetQusBanklist(c, params.PageNo, params.PageSize, params.Name)
if err != nil {
c.JSON(nil, err)
return
}
total, err := svc.GetQusBankTotal(c, params.Name)
if err != nil {
c.JSON(nil, err)
return
}
result.Items = list
result.PageSize = params.PageSize
result.PageNo = params.PageNo
result.Total = total
c.JSON(result, nil)
}
// qusBankAdd 添加题库
func qusBankAdd(c *bm.Context) {
params := new(model.ArgAddQusBank)
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
c.JSON(svc.AddQusBank(c, params))
}
// qusBankEdit 更新题库
func qusBankEdit(c *bm.Context) {
params := new(model.ArgUpdateQusBank)
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
c.JSON(svc.UpdataQusBank(c, params.QsBId, params.QBName, params.MaxRetryTime, params.CdTime))
}
// qusBankDel 删除题库
func qusBankDel(c *bm.Context) {
params := new(model.ArgBaseBank)
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
c.JSON(svc.DelQusBank(c, params.QsBId, model.DeledStatus))
}
// qusInfo 题目详情
func qusInfo(c *bm.Context) {
params := new(model.ArgGetQus)
if err := c.Bind(params); err != nil {
return
}
data := &model.QuestionAll{}
answers, err := svc.GetAnswerList(c, params.QsID)
if err != nil {
c.JSON(nil, ecode.QusIDInvalid)
return
}
info, err := svc.GetQusInfo(c, params.QsID)
if err != nil || info == nil {
c.JSON(nil, ecode.QusIDInvalid)
return
}
data.Question = *info
data.AnswersList = answers
c.JSON(data, err)
}
// qusList 题目列表
func qusList(c *bm.Context) {
params := new(model.ArgQusList)
if err := c.Bind(params); err != nil {
return
}
result := model.RespList{}
list, err := svc.GetQuslist(c, params.PageNo, params.PageSize, params.QsBId)
if err != nil {
c.JSON(nil, err)
return
}
total, err := svc.GetQusTotal(c, params.QsBId)
if err != nil {
c.JSON(nil, err)
return
}
result.Items = list
result.PageSize = params.PageSize
result.PageNo = params.PageNo
result.Total = total
c.JSON(result, nil)
}
// qusAdd 题目添加
func qusAdd(c *bm.Context) {
params := new(model.ArgAddQus)
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
anlist := params.Answer
msg, err := svc.CheckAnswer(c, 0, params.Type, anlist)
if err != nil {
c.JSON(msg, err)
return
}
addQus := &model.AddQus{
BId: params.BId,
Type: params.Type,
Name: params.Name,
Dif: params.Dif,
AnType: params.AnType,
Answers: anlist,
QsID: 0,
}
c.JSON(svc.AddQus(c, addQus, anlist))
}
// qusUpdate 题目更新
func qusUpdate(c *bm.Context) {
params := new(model.ArgUpdateQus)
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
answers := params.Answer
msg, err := svc.CheckAnswer(c, params.QsID, params.Type, answers)
if err != nil {
c.JSON(msg, err)
return
}
c.JSON(svc.UpdateQus(c, params, answers))
}
// questionBankBind 项目关联题库
func questionBankBind(c *bm.Context) {
params := &model.ArgQuestionBankBinds{}
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
err := svc.QuestionBankBind(c, params)
if err != nil {
log.Error("questionBankBind(%v) error(%v)", params, err)
}
c.JSON(nil, err)
}
// questionBankUnbind 解绑
func questionBankUnbind(c *bm.Context) {
params := &model.ArgQuestionBankUnbind{}
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
c.JSON(nil, svc.QuestionBankUnbind(c, params))
}
// qusDel 删除题目
func qusDel(c *bm.Context) {
params := new(model.ArgGetQus)
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
c.JSON(svc.DelQus(c, params.QsID))
}
// getBankBind 绑定关系
func getBankBind(c *bm.Context) {
params := &model.ArgGetBankBind{}
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
c.JSON(svc.GetQuestionBankBind(c, params))
}
// getBindItems 查询绑定到题库的
func getBindItems(c *bm.Context) {
params := &model.ArgGetBindItems{}
if err := c.Bind(params); err != nil {
return
}
c.JSON(svc.GetBindItem(c, params))
}
// getQuestion 随机获取一个问题
func getQuestion(c *bm.Context) {
params := &model.ArgGetQuestion{}
if err := c.Bind(params); err != nil {
return
}
c.JSON(svc.GetQuestion(c, params))
}
// answerQuestion 答题
func answerQuestion(c *bm.Context) {
// 判断是否可答 返回冷却时间
params := &model.ArgCheckAnswer{}
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
c.JSON(svc.UserAnswer(c, params))
}
// qusBankCheck 题库检查
func qusBankCheck(c *bm.Context) {
// 判断是否可答 返回冷却时间
params := &model.ArgCheckQus{}
if err := c.BindWith(params, binding.JSON); err != nil {
return
}
c.JSON(svc.QusBankCheck(c, params))
}

View File

@@ -0,0 +1,180 @@
package http
import (
"context"
"net/url"
"testing"
"go-common/app/service/openplatform/anti-fraud/model"
"go-common/library/ecode"
. "github.com/smartystreets/goconvey/convey"
)
const (
_qusBankInfoURL = "http://localhost:8801/openplatform/internal/antifraud/qusb/info?qbid=100"
_qusBanklistURL = "http://localhost:8801/openplatform/internal/antifraud/qusb/list"
_qslistURL = "http://localhost:8801/openplatform/internal/antifraud/qs/list"
_qsInfoURL = "http://localhost:8801/openplatform/internal/antifraud/qs/info"
_qsGetURL = "http://localhost:8801/openplatform/internal/antifraud/qs/get"
)
type TestData map[string]string
type Shoulds []interface{}
type TestCase struct {
tag string
testData TestData
should Shoulds
}
var glcs = []TestCase{
{tag: "TestQusBankList: valid parameters", testData: TestData{"page": "1", "page_size": "20"}, should: Shoulds{-0}},
{tag: "TestQusBankList: no page", testData: TestData{"page_size": "1"}, should: Shoulds{-400}},
{tag: "TestQusBankList: no page_size", testData: TestData{"page": "1"}, should: Shoulds{-400}},
{tag: "TestQusBankList: no mstatus", testData: TestData{"page": "a", "page_size": "b"}, should: Shoulds{-400}},
{tag: "TestQusBankList: invalid page", testData: TestData{"page": "a", "page_size": "20"}, should: Shoulds{-400}},
{tag: "TestQusBankList: invalid page_size", testData: TestData{"page": "1", "page_size": "a"}, should: Shoulds{-400}},
}
func TestQusBankList(t *testing.T) {
for _, td := range glcs {
Convey(td.tag, t, func() {
params := url.Values{}
for k, v := range td.testData {
params.Set(k, v)
}
req, _ := client.NewRequest("GET", _qusBanklistURL, "127.0.0.1", params)
var res struct {
Code int `json:"code"`
Data struct {
Result interface{} `json:"result"`
Total interface{} `json:"total"`
PageNo interface{} `json:"page_no"`
PageSize interface{} `json:"page_size"`
Items interface{} `json:"items"`
} `json:"data"`
}
if err := client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
So(res.Code, ShouldEqual, td.should[0])
})
}
}
func TestQusList(t *testing.T) {
for _, td := range glcs {
Convey(td.tag, t, func() {
params := url.Values{}
for k, v := range td.testData {
params.Set(k, v)
}
req, _ := client.NewRequest("GET", _qslistURL, "127.0.0.1", params)
var res struct {
Code int `json:"code"`
Data struct {
Result interface{} `json:"result"`
Total interface{} `json:"total"`
PageNo interface{} `json:"page_no"`
PageSize interface{} `json:"page_size"`
Items interface{} `json:"items"`
} `json:"data"`
}
if err := client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
So(res.Code, ShouldEqual, td.should[0])
})
}
}
var argsBankInfo = []TestCase{
{tag: "TestQusBankInfo: valid parameters", testData: TestData{"qb_id": "1111"}, should: Shoulds{0}},
{tag: "TestQusBankInfo: no qb_id", testData: TestData{"qb_id": "1"}, should: Shoulds{0}},
{tag: "TestQusBankInfo: invalid qb_id", testData: TestData{"qb_id": "a"}, should: Shoulds{-400}},
}
func TestQusBankInfo(t *testing.T) {
for _, td := range argsBankInfo {
Convey(td.tag, t, func() {
params := url.Values{}
for k, v := range td.testData {
params.Set(k, v)
}
req, _ := client.NewRequest("GET", _qusBankInfoURL, "127.0.0.1", params)
var res struct {
Code int `json:"code"`
Data model.QuestionBank `json:"data"`
}
if err := client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
So(res.Code, ShouldEqual, td.should[0])
})
}
}
var argsQusInfo = []TestCase{
{tag: "TestQusBankInfo: valid parameters", testData: TestData{"qid": "1111"}, should: Shoulds{20001005}},
{tag: "TestQusBankInfo: invalid qid", testData: TestData{"qid": "a"}, should: Shoulds{-400}},
}
func TestQusInfo(t *testing.T) {
for _, td := range argsQusInfo {
Convey(td.tag, t, func() {
params := url.Values{}
for k, v := range td.testData {
params.Set(k, v)
}
req, _ := client.NewRequest("GET", _qsInfoURL, "127.0.0.1", params)
var res struct {
Code int `json:"code"`
Data model.QuestionBank `json:"data"`
}
if err := client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
So(res.Code, ShouldEqual, td.should[0])
})
}
}
var argsGetQuestion = []TestCase{
{tag: "TestQusBankInfo: valid parameters", testData: TestData{"uid": "1111", "target_item": "11111", "target_item_type": "1", "source": "1", "platform": "1", "component_id": "122"},
should: Shoulds{ecode.BindBankNotFound.Code(), ecode.GetComponentIDErr.Code(), ecode.SetComponentIDErr.Code(), 0}},
{tag: "TestQusBankInfo: invalid ", testData: TestData{"uid": "a"}, should: Shoulds{-400, -400}},
}
func TestGetQuestion(t *testing.T) {
for _, td := range argsGetQuestion {
Convey(td.tag, t, func() {
params := url.Values{}
for k, v := range td.testData {
params.Set(k, v)
}
req, _ := client.NewRequest("GET", _qsGetURL, "127.0.0.1", params)
var res struct {
Code int `json:"code"`
Data model.QuestionBank `json:"data"`
}
if err := client.Do(context.TODO(), req, &res); err != nil {
t.Errorf("client.Do() error(%v)", err)
t.FailNow()
}
So(res.Code, ShouldBeIn, td.should...)
})
}
}

View File

@@ -0,0 +1,97 @@
package http
import (
grpcv1 "go-common/app/service/openplatform/anti-fraud/api/grpc/v1"
"go-common/app/service/openplatform/anti-fraud/api/http/v1"
"go-common/app/service/openplatform/anti-fraud/model"
bm "go-common/library/net/http/blademaster"
)
//riskCheck 风险检查
func riskCheck(c *bm.Context) {
params := new(grpcv1.RiskCheckRequest)
if err := c.Bind(params); err != nil {
return
}
res, err := svc.RiskCheck(c, params)
svc.Ticket2PayShield(c, params, res)
if res.Rank == model.RankDoubt {
res.Rank = model.RankNormal
res.Method = model.MethodPass
res.Desc = model.CheckPass
}
c.JSON(res, err)
}
//riskCheckV2 风险检查
func riskCheckV2(c *bm.Context) {
params := new(v1.RiskCheckV2Request)
if err := c.Bind(params); err != nil {
return
}
res, err := svc.RiskCheckV2(c, params)
if err == nil {
svc.PayShield(c, params, res)
}
c.JSON(res, err)
}
// ipList IP列表接口
func ipList(c *bm.Context) {
params := new(v1.IPListRequest)
c.JSON(svc.IPList(c, params))
}
//ipDetail ip详情列表
func ipDetail(c *bm.Context) {
params := new(v1.IPDetailRequest)
if err := c.Bind(params); err != nil {
return
}
c.JSON(svc.IPDetail(c, params))
}
// uidList uid列表接口
func uidList(c *bm.Context) {
params := new(v1.UIDListRequest)
c.JSON(svc.UIDList(c, params))
}
//uidDetail uid详情列表
func uidDetail(c *bm.Context) {
params := new(v1.UIDDetailRequest)
if err := c.Bind(params); err != nil {
return
}
c.JSON(svc.UIDDetail(c, params))
}
// ipBlack ip黑名单
func ipBlack(c *bm.Context) {
params := new(v1.IPBlackRequest)
if err := c.Bind(params); err != nil {
return
}
c.JSON(svc.IPBlack(c, params))
}
// uidBlack uid黑名单
func uidBlack(c *bm.Context) {
params := new(v1.UIDBlackRequest)
if err := c.Bind(params); err != nil {
return
}
c.JSON(svc.UIDBlack(c, params))
}