goProject/.svn/pristine/5d/5df6cb671df6614fa95300a808373169e18b0950.svn-base

88 lines
1.8 KiB
Plaintext
Raw Normal View History

2025-01-06 16:21:36 +08:00
package httpServer
import (
"common/webServer"
"net/http"
"common/resultStatus"
)
// 处理函数
type HandleFunc func(context *ApiContext) *webServer.ResponseObject
// ApiHandler
// @description: API处理结构
type ApiHandler struct {
// API完整路径名称
apiFullName string
// 方法定义
handleFun HandleFunc
// 方法参数名称集合
funcParamNames []string
}
// ApiFullName
// @description: API完整路径名称
// parameter:
// @receiver this: this
// return:
// @string:
func (this *ApiHandler) ApiFullName() string {
return this.apiFullName
}
// HandleFun
// @description: 方法定义
// parameter:
// @receiver this: this
// return:
// @HandleFunc: 方法
func (this *ApiHandler) HandleFun() HandleFunc {
return this.handleFun
}
// FuncParamNames
// @description: 方法参数名称集合
// parameter:
// @receiver this: this
// return:
// @[]string: 方法参数名称集合
func (this *ApiHandler) FuncParamNames() []string {
return this.funcParamNames
}
// CheckParam
// @description: 检测参数数量
// parameter:
// @receiver this: this
// @r:
// return:
// @resultStatus.ResultStatus: 状态码数据
func (this *ApiHandler) CheckParam(r *http.Request) resultStatus.ResultStatus {
for _, name := range this.funcParamNames {
if r.Form[name] == nil || len(r.Form[name]) == 0 {
return resultStatus.APIParamError
}
}
return resultStatus.Success
}
// newApiHandler
// @description: 创建新的请求方法对象
// parameter:
// @_apiFullName: API完整路径名称
// @_funcDefinition: 方法定义
// @_funcParamNames: 方法参数名称集合
// return:
// @*ApiHandler: 请求方法对象
func newApiHandler(_apiFullName string, _funcDefinition HandleFunc, _funcParamNames ...string) *ApiHandler {
return &ApiHandler{
apiFullName: _apiFullName,
handleFun: _funcDefinition,
funcParamNames: _funcParamNames,
}
}