goProject/trunk/center/common/webserver/apiHandlerMgr.go

70 lines
1.3 KiB
Go
Raw Normal View History

2025-01-06 16:01:02 +08:00
package webServer
import (
"fmt"
"net/http"
)
var (
// 处理方法集合
// Key:API名
handlerData = make(map[string]*ApiHandler)
// 文档方法
remarkFunc func(w http.ResponseWriter, r *http.Request)
)
// RegisterRemarkFunc
2025-01-23 16:12:49 +08:00
//
// @description: 注册文档api方法
//
2025-01-06 16:01:02 +08:00
// parameter:
2025-01-23 16:12:49 +08:00
//
2025-01-06 16:01:02 +08:00
// @_remarkFunc: _remarkFunc
2025-01-23 16:12:49 +08:00
//
2025-01-06 16:01:02 +08:00
// return:
func RegisterRemarkFunc(_remarkFunc func(w http.ResponseWriter, r *http.Request)) {
remarkFunc = _remarkFunc
}
// RegisterHandleFunc
2025-01-23 16:12:49 +08:00
//
// @description: 注册处理方法
//
2025-01-06 16:01:02 +08:00
// parameter:
2025-01-23 16:12:49 +08:00
//
2025-01-06 16:01:02 +08:00
// @apiName: API名称
// @callback: 回调方法
// @paramNames: 参数名称集合
2025-01-23 16:12:49 +08:00
//
2025-01-06 16:01:02 +08:00
// 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
2025-01-23 16:12:49 +08:00
//
// @description: 获取请求方法
//
2025-01-06 16:01:02 +08:00
// parameter:
2025-01-23 16:12:49 +08:00
//
2025-01-06 16:01:02 +08:00
// @apiFullName: 方法名称
2025-01-23 16:12:49 +08:00
//
2025-01-06 16:01:02 +08:00
// return:
2025-01-23 16:12:49 +08:00
//
2025-01-06 16:01:02 +08:00
// @*ApiHandler: 请求方法
// @bool: 是否存在
func GetHandleFunc(apiFullName string) (*ApiHandler, bool) {
if requestFuncObj, exists := handlerData[apiFullName]; exists {
return requestFuncObj, exists
}
return nil, false
}