Commit f29a17e2 by 孙龙

init

parent 3fa092d8
package common
import (
"github.com/gin-gonic/gin"
"strings"
)
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"`
}
//获取所有请求参数放到字典里面
func GetAllRequestParams(c *gin.Context) (request map[string]string){
request = make(map[string]string)
c.MultipartForm()
for name, value := range c.Request.Form {
if value[0] != "" {
request[name] = strings.TrimSpace(value[0])
}
}
return
}
package common
import (
"github.com/gin-gonic/gin"
"strings"
)
type Response struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
Data interface{} `json:"data"`
}
type BomResponse struct {
ErrCode int `json:"error_code"`
ErrMsg string `json:"error_msg"`
Flag int `json:"flag"`
Total int `json:"total"`
Data interface{} `json:"data"`
}
func SuccessResponse(errCode int, errMsg string, data interface{}) Response {
return Response{
ErrCode: errCode,
ErrMsg: errMsg,
Data: data,
}
}
func ErrorResponse(errCode int, errMsg string) Response {
return Response{
ErrCode: errCode,
ErrMsg: errMsg,
Data: []string{},
}
}
//统一输出,里面还要去处理jsonp
func Output(ctx *gin.Context,errCode int, errMsg string, data interface{}) {
if data == nil {
data = []string{}
}
response := Response{
ErrCode: errCode,
ErrMsg: errMsg,
Data: data,
}
//if errCode >= 100 {
// //SearchApiLogger(ctx,errCode, errMsg)
//}
if ctx.DefaultQuery("callback", "") != "" {
ctx.JSONP(200, response)
} else {
referer := ctx.Request.Referer()
referer = strings.TrimRight(referer, "/")
ctx.Header("Access-Control-Allow-Origin", referer)
ctx.Header("Access-Control-Allow-Credentials", "true")
//允许跨站访问的站点域名
//跨域请求头设置
ctx.JSON(200, response)
}
}
//简单的返回数据方法
func ReturnData(ctx *gin.Context,errCode int, errMsg string, data interface{}) {
if data == nil {
data = []string{}
if errCode == 0 {
errCode = 1
}
}
response := Response{
ErrCode: errCode,
ErrMsg: errMsg,
Data: data,
}
ctx.JSON(200, response)
}
package common
import (
"reflect"
"sort"
)
func getCommon(array interface{}) (reflect.Type, reflect.Value, int) {
t := reflect.TypeOf(array)
v := reflect.ValueOf(array)
l := v.Len()
return t, v, l
}
func SortSlice(array interface{}) {
t, v, _ := getCommon(array)
// res := make([]interface{}, l)
if t.Kind() == reflect.Slice {
switch v.Index(0).Kind() {
case reflect.Int:
array := array.([]int)
sort.Ints(array)
case reflect.String:
array := array.([]string)
sort.Strings(array)
case reflect.Float64:
array := array.([]float64)
sort.Float64s(array)
default:
panic("the param can only be int/string/float64 array")
}
} else {
panic("expects parameter 1 to be array")
}
}
package controller
import (
"github.com/gin-gonic/gin"
common "ichunt_golang/app/common/function"
xlog "github.com/ichunt2019/lxLog/log"
)
func Ping(ctx *gin.Context) {
xlog.Instance("sku").Info("ping")
common.Output(ctx, 0, "success",nil)
}
func Hbsdata(ctx *gin.Context) {
xlog.Instance("spu").Info("Hbsdata")
common.Output(ctx, 0, "success",nil)
}
\ No newline at end of file
[INFO][2021/03/01 14:32:04][log.go:58] _com_request_in||method=GET||args=map[]||cspanid=||spanid=1f3c8a6578629a0f||uri=/hbsdata||body=||from=192.168.2.246||traceid=7f000001603c8a64780c87f0658221b0
[INFO][2021/03/01 14:32:04][log.go:58] _com_request_out||uri=/hbsdata||from=192.168.2.246||proc_time=0.0289498||cspanid=||spanid=1f3c8a6578629a0f||method=GET||args=map[]||response=<nil>||traceid=7f000001603c8a64780c87f0658221b0
[INFO][2021/03/01 14:35:42][log.go:58] _com_request_in||args=map[]||body=||traceid=7f000001603c8b3e9a74af70658221b0||cspanid=||uri=/hbsdata||method=GET||spanid=1f3c8b3f78629a0f||from=192.168.2.246
[INFO][2021/03/01 14:35:42][log.go:58] _com_request_out||traceid=7f000001603c8b3e9a74af70658221b0||uri=/hbsdata||args=map[]||from=192.168.2.246||cspanid=||spanid=1f3c8b3f78629a0f||method=GET||response=<nil>||proc_time=0.0269554
[INFO][2021/03/01 14:35:44][log.go:58] _com_request_in||uri=/hbsdata||body=||traceid=7f000001603c8b40ce30af70104dc7b0||cspanid=||spanid=1f3c8b41380704bb||method=GET||args=map[]||from=192.168.2.246
[INFO][2021/03/01 14:35:44][log.go:58] _com_request_out||method=GET||cspanid=||proc_time=0||traceid=7f000001603c8b40ce30af70104dc7b0||spanid=1f3c8b41380704bb||uri=/hbsdata||args=map[]||from=192.168.2.246||response=<nil>
[INFO][2021/03/01 14:36:06][log.go:58] _com_request_in||body=||traceid=7f000001603c8b56767c8314658221b0||cspanid=||spanid=1f3c8b5778629a0f||method=GET||args=map[]||uri=/hbsdata||from=192.168.2.246
[INFO][2021/03/01 14:36:06][log.go:58] _com_request_out||proc_time=0.0299192||traceid=7f000001603c8b56767c8314658221b0||spanid=1f3c8b5778629a0f||uri=/hbsdata||from=192.168.2.246||response=<nil>||cspanid=||method=GET||args=map[]
[INFO][2021/03/01 14:36:08][log.go:58] _com_request_in||from=192.168.2.246||cspanid=||uri=/hbsdata||method=GET||body=||args=map[]||traceid=7f000001603c8b5860fc8314104dc7b0||spanid=1f3c8b59380704bb
[INFO][2021/03/01 14:36:08][log.go:58] _com_request_out||args=map[]||from=192.168.2.246||response=<nil>||proc_time=0||uri=/hbsdata||method=GET||cspanid=||spanid=1f3c8b59380704bb||traceid=7f000001603c8b5860fc8314104dc7b0
[INFO][2021/03/01 14:36:50][log.go:58] _com_request_in||body=||from=192.168.2.246||traceid=7f000001603c8b82d63058f8658221b0||spanid=1f3c8b8378629a0f||uri=/hbsdata||method=GET||args=map[]||cspanid=
[INFO][2021/03/01 14:36:50][log.go:58] _com_request_out||spanid=1f3c8b8378629a0f||uri=/hbsdata||proc_time=0.0279402||response=<nil>||traceid=7f000001603c8b82d63058f8658221b0||cspanid=||method=GET||args=map[]||from=192.168.2.246
[INFO][2021/03/01 14:36:50][log.go:58] _com_request_in||method=GET||args=map[]||cspanid=||spanid=1f3c8b83380704bb||uri=/hbsdata||body=||from=192.168.2.246||traceid=7f000001603c8b8259b458f8104dc7b0
[INFO][2021/03/01 14:36:50][log.go:58] _com_request_out||cspanid=||spanid=1f3c8b83380704bb||method=GET||args=map[]||response=<nil>||proc_time=0||uri=/hbsdata||from=192.168.2.246||traceid=7f000001603c8b8259b458f8104dc7b0
...@@ -2,6 +2,7 @@ package router ...@@ -2,6 +2,7 @@ package router
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"ichunt_golang/app/controller"
) )
func InitRouter(middlewares ...gin.HandlerFunc) *gin.Engine { func InitRouter(middlewares ...gin.HandlerFunc) *gin.Engine {
...@@ -9,14 +10,8 @@ func InitRouter(middlewares ...gin.HandlerFunc) *gin.Engine { ...@@ -9,14 +10,8 @@ func InitRouter(middlewares ...gin.HandlerFunc) *gin.Engine {
//router := gin.Default() //router := gin.Default()
router := gin.New() router := gin.New()
router.Use(middlewares...) router.Use(middlewares...)
router.GET("/ping", func(c *gin.Context) { router.GET("/ping", controller.Ping)
c.JSON(200, gin.H{ router.GET("/hbsdata",controller.Hbsdata)
"message": "pong",
})
})
router.GET("/hbsdata", func(c *gin.Context) {
c.Writer.Write([]byte("ok"))
})
router.Use( router.Use(
......
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