goProject/trunk/center/common/webserver/apiHandler.go
2025-02-20 17:46:57 +08:00

113 lines
1.9 KiB
Go

package webServer
import (
"net/http"
"common/resultStatus"
)
// 处理函数
type HandleFunc func(context *ApiContext) *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,
}
}