193 lines
4.4 KiB
Go
193 lines
4.4 KiB
Go
package httpServer
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"goutil/logUtilPlus"
|
||
"goutil/typeUtil"
|
||
"goutil/zlibUtil"
|
||
"io/ioutil"
|
||
"net/http"
|
||
)
|
||
|
||
// ApiContext
|
||
// @description: Api请求上下文对象
|
||
type ApiContext struct {
|
||
// 请求对象
|
||
request *http.Request
|
||
|
||
// 应答写对象
|
||
responseWriter http.ResponseWriter
|
||
|
||
// 请求数据
|
||
requestBytes []byte
|
||
|
||
// 字典形式的请求数据
|
||
requestDataByMap typeUtil.MapData
|
||
}
|
||
|
||
// GetRequest
|
||
// @description: 获取请求对象
|
||
// parameter:
|
||
// @receiver this: this
|
||
// return:
|
||
// @*http.Request: 请求对象
|
||
func (this *ApiContext) GetRequest() *http.Request {
|
||
return this.request
|
||
}
|
||
|
||
// GetResponseWriter
|
||
// @description: 获取应答写对象
|
||
// parameter:
|
||
// @receiver this: this
|
||
// return:
|
||
// @http.ResponseWriter: 应答写对象
|
||
func (this *ApiContext) GetResponseWriter() http.ResponseWriter {
|
||
return this.responseWriter
|
||
}
|
||
|
||
// GetRequestBytes
|
||
// @description: 获取请求字节数据
|
||
// parameter:
|
||
// @receiver this: this
|
||
// return:
|
||
// @[]byte: 请求字节数组
|
||
func (this *ApiContext) GetRequestBytes() []byte {
|
||
return this.requestBytes
|
||
}
|
||
|
||
// readContent
|
||
// @description: 读取内容
|
||
// parameter:
|
||
// @receiver this: this 请求对象
|
||
// @isZlib: 是否数据压缩
|
||
// return:
|
||
// @content: 二进制格式的内容
|
||
// @err: 错误对象
|
||
func (this *ApiContext) readContent(isZlib bool) (content []byte, err error) {
|
||
var buffer []byte
|
||
|
||
defer this.request.Body.Close()
|
||
if buffer, err = ioutil.ReadAll(this.request.Body); err != nil {
|
||
logUtilPlus.ErrorLog(fmt.Sprintf("url:%s,读取数据出错,错误信息为:%s", this.request.RequestURI, err))
|
||
return
|
||
}
|
||
|
||
// 不压缩,则直接返回
|
||
if isZlib == false {
|
||
this.requestBytes = buffer
|
||
|
||
return buffer, err
|
||
}
|
||
|
||
// 解压数据
|
||
if content, err = zlibUtil.Decompress(buffer); err != nil {
|
||
logUtilPlus.ErrorLog(fmt.Sprintf("url:%s,解压缩数据出错,错误信息为:%s", this.request.RequestURI, err))
|
||
return
|
||
}
|
||
|
||
this.requestBytes = content
|
||
|
||
return
|
||
}
|
||
|
||
// Unmarshal
|
||
// @description: 反序列化
|
||
// parameter:
|
||
// @receiver this: this
|
||
// @obj: 反序列化结果数据
|
||
// return:
|
||
// @error: 反序列化错误数据
|
||
func (this *ApiContext) Unmarshal(obj interface{}) error {
|
||
contentData := this.GetRequestBytes()
|
||
|
||
// 反序列化
|
||
if errMsg := json.Unmarshal(contentData, &obj); errMsg != nil {
|
||
logUtilPlus.ErrorLog(fmt.Sprintf("反序列化%s出错,错误信息为:%s", string(contentData), errMsg.Error()))
|
||
return errMsg
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// RequestDataByMap
|
||
// @description: 获取请求的map格式数据
|
||
// parameter:
|
||
// @receiver this: this
|
||
// return:
|
||
// @typeUtil.MapData: map数据
|
||
// @error: 错误信息
|
||
func (this *ApiContext) RequestDataByMap() (typeUtil.MapData, error) {
|
||
if this.requestDataByMap != nil {
|
||
return this.requestDataByMap, nil
|
||
}
|
||
|
||
var data typeUtil.MapData
|
||
if err := this.Unmarshal(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
this.requestDataByMap = data
|
||
|
||
return this.requestDataByMap, nil
|
||
}
|
||
|
||
// RequestDataBySlice
|
||
// @description: 获取请求的slice格式数据
|
||
// parameter:
|
||
// @receiver this: this
|
||
// return:
|
||
// @[]interface{}: 返回的数组数据
|
||
func (this *ApiContext) RequestDataBySlice() []interface{} {
|
||
result := make([]interface{}, 0, 8)
|
||
for _, value := range this.requestDataByMap {
|
||
result = append(result, value)
|
||
}
|
||
return result
|
||
}
|
||
|
||
// RequestDataBySlice2
|
||
// @description: 获取请求的slice格式数据
|
||
// parameter:
|
||
// @receiver this: this
|
||
// return:
|
||
// @[]interface{}: 返回的数组数据
|
||
// @error:
|
||
func (this *ApiContext) RequestDataBySlice2() ([]interface{}, error) {
|
||
contentData := this.GetRequestBytes()
|
||
|
||
// 反序列化
|
||
result := make([]interface{}, 0, 8)
|
||
if errMsg := json.Unmarshal(contentData, &result); errMsg != nil {
|
||
logUtilPlus.ErrorLog(fmt.Sprintf("用[]interface{}反序列化%s出错,错误信息为:%s", string(contentData), errMsg.Error()))
|
||
return nil, errMsg
|
||
}
|
||
|
||
return result, nil
|
||
}
|
||
|
||
// NewApiContext
|
||
// @description: 新建API上下文对象
|
||
// parameter:
|
||
// @_request: 请求对象
|
||
// @_responseWriter: 应答写对象
|
||
// @isZlib: 数据是否压缩
|
||
// return:
|
||
// @*ApiContext: 上下文
|
||
// @error: 错误信息
|
||
func NewApiContext(_request *http.Request, _responseWriter http.ResponseWriter, isZlib bool) (*ApiContext, error) {
|
||
context := &ApiContext{
|
||
request: _request,
|
||
responseWriter: _responseWriter,
|
||
}
|
||
|
||
// 读取数据
|
||
_, errMsg := context.readContent(isZlib)
|
||
if errMsg != nil {
|
||
|
||
return nil, errMsg
|
||
}
|
||
|
||
return context, nil
|
||
}
|