代码拉取完成,页面将自动刷新
package controller
import (
"gitee.com/kristas/booting-go/framework/common/util/lang"
"gitee.com/kristas/booting-go/framework/common/util/reflectx"
"gitee.com/kristas/booting-go/framework/core/component"
"gitee.com/kristas/booting-go/framework/web"
"reflect"
)
func HandleRestApi(controller component.Controller, handler func(apiInfo web.ApiInfo, svm web.ServiceMethod)) {
typ := reflect.TypeOf(controller)
val := reflect.ValueOf(controller)
reflectx.WalkStructField(controller, func(field reflect.StructField, value reflect.Value) error {
if isRestAPI(field) {
if method, ok := typ.MethodByName(methodName(field)); ok {
httpMethod, url := findHttpMethodAndUrl(field)
url = controller.Controller() + url
methodVal := val.MethodByName(methodName(field))
paramMap := buildParam(field.Tag.Get("param"), method.Type)
description := field.Tag.Get("desc")
apiInfo := &ApiInfo{
HttpMethod: httpMethod,
URL: url,
Description: description,
Params: paramMap,
}
svm := NewServiceMethod(&method, &methodVal)
NewApiInfo(controller.Controller(), field, svm)
handler(apiInfo, svm)
}
}
return nil
}, false)
}
func HandleFsApi(controller component.Controller, handler func(prefix, dir string)) {
typ := reflect.TypeOf(controller)
val := reflect.ValueOf(controller)
reflectx.WalkStructField(controller, func(field reflect.StructField, value reflect.Value) error {
if isFsAPI(field) {
if _, ok := typ.MethodByName(methodName(field)); ok {
url := field.Tag.Get("url")
url = controller.Controller() + url
methodVal := val.MethodByName(methodName(field))
handler(url, methodVal.Call(nil)[0].String())
}
}
return nil
}, false)
}
func isRestAPI(field reflect.StructField) bool {
return field.Type.Implements(reflect.TypeOf(new(web.Rest)).Elem())
}
func isFsAPI(field reflect.StructField) bool {
return field.Type == reflect.TypeOf(web.FS{})
}
func findHttpMethodAndUrl(field reflect.StructField) (method string, url string) {
m := reflect.New(field.Type).Interface().(web.Rest)
method = m.GetType()
url = field.Tag.Get("url")
return
}
func methodName(field reflect.StructField) string {
method := lang.NewString(field.Name).FirstLetterUpper()
return method.String()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。