goProject/trunk/center/common/httpServer/apiHandlerMgr.go
皮蛋13361098506 1b77f62820 初始化项目
2025-01-06 16:01:02 +08:00

57 lines
1.3 KiB
Go

package httpServer
import (
"fmt"
"net/http"
)
var (
// 处理方法集合
// Key:API名
handlerData = make(map[string]*ApiHandler)
// 文档方法
remarkFunc func(w http.ResponseWriter, r *http.Request)
)
// RegisterRemarkFunc
// @description: 注册文档api方法
// parameter:
// @_remarkFunc: _remarkFunc
// return:
func RegisterRemarkFunc(_remarkFunc func(w http.ResponseWriter, r *http.Request)) {
remarkFunc = _remarkFunc
}
// RegisterHandleFunc
// @description: 注册处理方法
// parameter:
// @apiName: API名称
// @callback: 回调方法
// @paramNames: 参数名称集合
// return:
func RegisterHandleFunc(apiName string, callback HandleFunc, paramNames ...string) {
apiFullName := fmt.Sprintf("/API/%s", apiName)
if _, exist := handlerData[apiFullName]; exist {
panic(fmt.Errorf("重复注册处理函数:%s", apiFullName))
}
handlerData[apiFullName] = newApiHandler(apiFullName, callback, paramNames...)
}
// GetHandleFunc
// @description: 获取请求方法
// parameter:
// @apiFullName: 方法名称
// return:
// @*ApiHandler: 请求方法
// @bool: 是否存在
func GetHandleFunc(apiFullName string) (*ApiHandler, bool) {
if requestFuncObj, exists := handlerData[apiFullName]; exists {
return requestFuncObj, exists
}
return nil, false
}