Commit b48939c0 by mushishixian

简单http启动

parent 4d37b46a
......@@ -3,3 +3,4 @@
config.ini
gowatch.yml
*.exe
*.exe~
\ No newline at end of file
The file could not be displayed because it is too large.
......@@ -5,8 +5,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/v2/web"
"search_server/boot"
"search_server/controller"
_ "search_server/controller"
"search_server/framework/gin_"
"search_server/pkg/config"
)
......@@ -19,7 +19,11 @@ func main() {
}
gin.SetMode(config.Get("web.mode").String())
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()
//web改成micro 就是grpc,并直接注册到etcd里面
......
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"search_server/framework/gin_"
"search_server/protopb/bom"
"search_server/pkg/common"
"search_server/service"
)
func init() {
bomService := service.NewBomServiceImpl()
gin_.NewBuilder().WithService(bomService).
WithMiddleware(Check_Middleware()).
WithMiddleware(Cors_Middleware()).
WithEndpoint(AutoSpuEndpoint(bomService)).
WithRequest(AutoSpuRequest()).
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
//搜索型号
func AutoSpu(c *gin.Context) {
goodsName, _ := c.GetPostForm("goods_name")
goods := service.AutoSpu(goodsName)
var errCode int
if len(goods) == 0 {
errCode = 1
}
}
//这个函数作用是:怎么处理响应结果
func AutoSpuResponse() gin_.DecodeResponseFunc {
return func(context *gin.Context, res interface{}) error {
context.JSON(200, res)
return nil
res := common.Response{
ErrCode: errCode,
ErrMsg: "",
Data: goods,
}
c.JSON(200, res)
}
//获取列表相关
func RecommendEndpoint(c *service.BomServiceImpl) gin_.Endpoint {
return func(context *gin.Context, request interface{}) (response interface{}, err error) {
rsp := &bom.RecommendResponse{}
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参数
//获取推荐列表
func Recommend(c *gin.Context) {
req := &common.RecommendRequest{}
err := c.Bind(&req) //使用的是post参数
if err != nil {
return nil, err
fmt.Println(err)
}
return bReq, nil
if req.Flag == 0 {
res := common.BomResponse{
ErrCode: 101,
ErrMsg: "参数不全",
Data: []string{},
}
}
//这个函数作用是:怎么处理响应结果
func RecommendResponse() gin_.DecodeResponseFunc {
return func(context *gin.Context, res interface{}) error {
context.JSON(200, res)
return nil
c.JSON(200, res)
return
}
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
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,
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 {
return &BomServiceImpl{}
}
......@@ -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 goodsName string
......@@ -100,8 +90,8 @@ func Recommend(req *bom.RecommendRequest) (rsp *bom.ResponseData) {
}
//获取需要的数据
total := gjson.Get(result, "hits.total").Int()
var response bom.ResponseData
response.Total = total
var response common.BomResponse
response.Total = int(total)
//获取goods_id列表去商品服务获取商品
list := gjson.Get(result, "hits.hits.#._source").Array()
var goodsIdList []string
......@@ -118,7 +108,7 @@ func Recommend(req *bom.RecommendRequest) (rsp *bom.ResponseData) {
}
//处理recommend接口的请求参数
func changeRecommendReq(req *bom.RecommendRequest) (res *bom.RecommendRequest) {
func changeRecommendReq(req *common.RecommendRequest) (res *common.RecommendRequest) {
//设置默认值
if req.Num == 0 {
req.Num = 1
......@@ -136,7 +126,7 @@ func changeRecommendReq(req *bom.RecommendRequest) (res *bom.RecommendRequest) {
}
//获取recommend接口需要请求的索引
func getRecommendSearchIndex(req *bom.RecommendRequest) (index string) {
func getRecommendSearchIndex(req *common.RecommendRequest) (index string) {
index = config.Get("es.hk_delivery_type_supplier").String()
//如果是内地收货的情况下
if req.DeliveryType == 1 {
......@@ -146,13 +136,13 @@ func getRecommendSearchIndex(req *bom.RecommendRequest) (index string) {
}
//获取请求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)
return
}
//获取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()
if isRawSearch {
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