Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Cache 结构体代表缓存
|
||||
type Cache struct {
|
||||
data map[string]any
|
||||
rwmu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewCache 创建并返回一个新的缓存实例
|
||||
func NewCache() *Cache {
|
||||
return &Cache{
|
||||
data: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
// Set 方法用于向缓存中设置键值对,写操作会加写锁保证并发安全
|
||||
func (c *Cache) set(key string, value any) {
|
||||
c.rwmu.Lock()
|
||||
defer c.rwmu.Unlock()
|
||||
c.data[key] = value
|
||||
}
|
||||
|
||||
// Get 方法用于从缓存中根据键获取对应的值,读操作会加读锁允许并发读
|
||||
func (c *Cache) get(key string) any {
|
||||
c.rwmu.RLock()
|
||||
defer c.rwmu.RUnlock()
|
||||
value := c.data[key]
|
||||
return value
|
||||
}
|
||||
|
||||
// Delete 方法用于从缓存中删除指定的键值对,写操作会加写锁保证并发安全
|
||||
func (c *Cache) Delete(key string) {
|
||||
c.rwmu.Lock()
|
||||
defer c.rwmu.Unlock()
|
||||
delete(c.data, key)
|
||||
}
|
||||
|
||||
// GetData 方法用于从缓存中根据键获取对应的值,读操作会加读锁允许并发读
|
||||
func GetData[V any](c *Cache, key string) (value V, ok bool) {
|
||||
v := c.get(key)
|
||||
value, ok = v.(V)
|
||||
return
|
||||
}
|
||||
|
||||
// SetData 方法用于向缓存中设置键值对,写操作会加写锁保证并发安全
|
||||
func SetData(c *Cache, key string, value any) {
|
||||
c.set(key, value)
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
package httpServer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"goutil/logUtilPlus"
|
||||
"goutil/typeUtil"
|
||||
"goutil/zlibUtil"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user