Commit b48939c0 by mushishixian

简单http启动

parent 4d37b46a
...@@ -3,3 +3,4 @@ ...@@ -3,3 +3,4 @@
config.ini config.ini
gowatch.yml gowatch.yml
*.exe *.exe
*.exe~
\ No newline at end of file
The file could not be displayed because it is too large.
...@@ -5,8 +5,8 @@ import ( ...@@ -5,8 +5,8 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/web" "github.com/micro/go-micro/v2/web"
"search_server/boot" "search_server/boot"
"search_server/controller"
_ "search_server/controller" _ "search_server/controller"
"search_server/framework/gin_"
"search_server/pkg/config" "search_server/pkg/config"
) )
...@@ -19,7 +19,11 @@ func main() { ...@@ -19,7 +19,11 @@ func main() {
} }
gin.SetMode(config.Get("web.mode").String()) gin.SetMode(config.Get("web.mode").String())
r := gin.New() r := gin.New()
gin_.BootStrap(r)
//路由
r.POST("/search/bom/autospu", controller.AutoSpu)
r.POST("/search/bom/recommend", controller.Recommend)
port := config.Get("web.port").String() port := config.Get("web.port").String()
//web改成micro 就是grpc,并直接注册到etcd里面 //web改成micro 就是grpc,并直接注册到etcd里面
......
package controller package controller
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"search_server/framework/gin_" "search_server/pkg/common"
"search_server/protopb/bom"
"search_server/service" "search_server/service"
) )
func init() { //搜索型号
bomService := service.NewBomServiceImpl() func AutoSpu(c *gin.Context) {
gin_.NewBuilder().WithService(bomService). goodsName, _ := c.GetPostForm("goods_name")
WithMiddleware(Check_Middleware()). goods := service.AutoSpu(goodsName)
WithMiddleware(Cors_Middleware()). var errCode int
WithEndpoint(AutoSpuEndpoint(bomService)). if len(goods) == 0 {
WithRequest(AutoSpuRequest()). errCode = 1
WithResponse(AutoSpuResponse()).Build("/search/bom/autospu", "POST")
gin_.NewBuilder().WithService(bomService).
WithMiddleware(Check_Middleware()).
WithMiddleware(Cors_Middleware()).
WithEndpoint(RecommendEndpoint(bomService)).
WithRequest(RecommendRequest()).
WithResponse(RecommendResponse()).Build("/search/bom/recommend", "POST")
}
//获取列表相关
func AutoSpuEndpoint(c *service.BomServiceImpl) gin_.Endpoint {
return func(context *gin.Context, request interface{}) (response interface{}, err error) {
rsp := &bom.AutoSpuResponse{}
err = c.AutoSpu(context, request.(*bom.AutoSpuRequest), rsp)
return rsp, err
}
}
//这个函数的作用是怎么处理请求
func AutoSpuRequest() gin_.EncodeRequestFunc {
return func(context *gin.Context) (i interface{}, e error) {
bReq := &bom.AutoSpuRequest{}
err := context.BindQuery(bReq) //使用的是query 参数
bReq.GoodsName, _ = context.GetPostForm("goods_name")
if err != nil {
return nil, err
}
return bReq, nil
} }
} res := common.Response{
ErrCode: errCode,
//这个函数作用是:怎么处理响应结果 ErrMsg: "",
func AutoSpuResponse() gin_.DecodeResponseFunc { Data: goods,
return func(context *gin.Context, res interface{}) error {
context.JSON(200, res)
return nil
} }
c.JSON(200, res)
} }
//获取列表相关 //获取推荐列表
func RecommendEndpoint(c *service.BomServiceImpl) gin_.Endpoint { func Recommend(c *gin.Context) {
return func(context *gin.Context, request interface{}) (response interface{}, err error) { req := &common.RecommendRequest{}
rsp := &bom.RecommendResponse{} err := c.Bind(&req) //使用的是post参数
err = c.Recommend(context, request.(*bom.RecommendRequest), rsp)
return rsp, err
}
}
//这个函数的作用是怎么处理请求
func RecommendRequest() gin_.EncodeRequestFunc {
return func(context *gin.Context) (i interface{}, e error) {
bReq := &bom.RecommendRequest{}
err := context.Bind(bReq) //使用的是post参数
if err != nil { if err != nil {
return nil, err fmt.Println(err)
} }
return bReq, nil if req.Flag == 0 {
res := common.BomResponse{
ErrCode: 101,
ErrMsg: "参数不全",
Data: []string{},
} }
} c.JSON(200, res)
return
//这个函数作用是:怎么处理响应结果
func RecommendResponse() gin_.DecodeResponseFunc {
return func(context *gin.Context, res interface{}) error {
context.JSON(200, res)
return nil
} }
data := service.Recommend(req)
c.JSON(200, data)
} }
package common
type RecommendRequest struct {
GoodsName string `form:"goods_name"`
Attrs string `form:"attrs"`
Encap string `form:"encap"`
Num int `form:"num"`
DeliveryType int `form:"delivery_type"`
Flag int `form:"flag"`
BrandName string `form:"brand_name"`
}
package common package common
type Response struct {
ErrCode int `json:"err_code"`
ErrMsg string `json:"err_msg"`
Data interface{} `json:"data"`
}
type BomResponse struct {
ErrCode int `json:"err_code"`
ErrMsg string `json:"err_msg"`
Flag int `json:"flag"`
Total int `json:"total"`
Data interface{} `json:"data"`
}
\ No newline at end of file
...@@ -24,16 +24,6 @@ func (bs *BomServiceImpl) AutoSpu(ctx context.Context, req *bom.AutoSpuRequest, ...@@ -24,16 +24,6 @@ func (bs *BomServiceImpl) AutoSpu(ctx context.Context, req *bom.AutoSpuRequest,
return nil return nil
} }
func (bs *BomServiceImpl) Recommend(ctx context.Context, req *bom.RecommendRequest, rsp *bom.RecommendResponse) error {
if req.Flag == 0 {
rsp.ErrorCode = 101
rsp.ErrorMessage = "参数不全"
return nil
}
rsp.Data = Recommend(req)
return nil
}
func NewBomServiceImpl() *BomServiceImpl { func NewBomServiceImpl() *BomServiceImpl {
return &BomServiceImpl{} return &BomServiceImpl{}
} }
...@@ -74,7 +64,7 @@ func getTermQuery(goodsName string) (query *elastic.BoolQuery) { ...@@ -74,7 +64,7 @@ func getTermQuery(goodsName string) (query *elastic.BoolQuery) {
} }
//推荐商品搜索 //推荐商品搜索
func Recommend(req *bom.RecommendRequest) (rsp *bom.ResponseData) { func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
var err error var err error
//先去请求参数,看是否是参数,如果是参数的话,需要修改请求的商品名称 //先去请求参数,看是否是参数,如果是参数的话,需要修改请求的商品名称
var goodsName string var goodsName string
...@@ -100,8 +90,8 @@ func Recommend(req *bom.RecommendRequest) (rsp *bom.ResponseData) { ...@@ -100,8 +90,8 @@ func Recommend(req *bom.RecommendRequest) (rsp *bom.ResponseData) {
} }
//获取需要的数据 //获取需要的数据
total := gjson.Get(result, "hits.total").Int() total := gjson.Get(result, "hits.total").Int()
var response bom.ResponseData var response common.BomResponse
response.Total = total response.Total = int(total)
//获取goods_id列表去商品服务获取商品 //获取goods_id列表去商品服务获取商品
list := gjson.Get(result, "hits.hits.#._source").Array() list := gjson.Get(result, "hits.hits.#._source").Array()
var goodsIdList []string var goodsIdList []string
...@@ -118,7 +108,7 @@ func Recommend(req *bom.RecommendRequest) (rsp *bom.ResponseData) { ...@@ -118,7 +108,7 @@ func Recommend(req *bom.RecommendRequest) (rsp *bom.ResponseData) {
} }
//处理recommend接口的请求参数 //处理recommend接口的请求参数
func changeRecommendReq(req *bom.RecommendRequest) (res *bom.RecommendRequest) { func changeRecommendReq(req *common.RecommendRequest) (res *common.RecommendRequest) {
//设置默认值 //设置默认值
if req.Num == 0 { if req.Num == 0 {
req.Num = 1 req.Num = 1
...@@ -136,7 +126,7 @@ func changeRecommendReq(req *bom.RecommendRequest) (res *bom.RecommendRequest) { ...@@ -136,7 +126,7 @@ func changeRecommendReq(req *bom.RecommendRequest) (res *bom.RecommendRequest) {
} }
//获取recommend接口需要请求的索引 //获取recommend接口需要请求的索引
func getRecommendSearchIndex(req *bom.RecommendRequest) (index string) { func getRecommendSearchIndex(req *common.RecommendRequest) (index string) {
index = config.Get("es.hk_delivery_type_supplier").String() index = config.Get("es.hk_delivery_type_supplier").String()
//如果是内地收货的情况下 //如果是内地收货的情况下
if req.DeliveryType == 1 { if req.DeliveryType == 1 {
...@@ -146,13 +136,13 @@ func getRecommendSearchIndex(req *bom.RecommendRequest) (index string) { ...@@ -146,13 +136,13 @@ func getRecommendSearchIndex(req *bom.RecommendRequest) (index string) {
} }
//获取请求es的参数 //获取请求es的参数
func getRecommendSearchParams(isRawSearch bool, req *bom.RecommendRequest) (result string) { func getRecommendSearchParams(isRawSearch bool, req *common.RecommendRequest) (result string) {
result = getBomTermQuery(req.GoodsName, "", req.Num, isRawSearch) result = getBomTermQuery(req.GoodsName, "", req.Num, isRawSearch)
return return
} }
//获取bom单的term查询 //获取bom单的term查询
func getBomTermQuery(goodsName, brandName string, number int32, isRawSearch bool) (result string) { func getBomTermQuery(goodsName, brandName string, number int, isRawSearch bool) (result string) {
query := elastic.NewBoolQuery() query := elastic.NewBoolQuery()
if isRawSearch { if isRawSearch {
field := "auto_goods_name.raw" field := "auto_goods_name.raw"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment