1 Star 0 Fork 0

kade / mcube

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
api.go 1.80 KB
一键复制 编辑 原始数据 按行查看 历史
kadegolang 提交于 2023-12-13 17:31 . copy
package ioc
import (
"fmt"
"path"
"strings"
"github.com/emicklei/go-restful/v3"
"github.com/gin-gonic/gin"
"gitee.com/go-kade/mcube/http/restful/accessor/form"
"gitee.com/go-kade/mcube/http/restful/accessor/yaml"
"gitee.com/go-kade/mcube/http/restful/accessor/yamlk8s"
)
const (
API_NAMESPACE = "apis"
)
// 用于托管RestApi对象的Ioc空间, 最后初始化
func Api() StoreUser {
return store.Namespace(API_NAMESPACE)
}
type GinApiObject interface {
Object
Registry(gin.IRouter)
}
type GoRestfulApiObject interface {
Object
Registry(*restful.WebService)
}
// 注册API对象
func RegistryApi(obj Object) {
RegistryObjectWithNs(API_NAMESPACE, obj)
}
// 获取API对象
func GetApi(name string) Object {
return GetObjectWithNs(API_NAMESPACE, name)
}
// LoadGinApi 装载所有的gin app
func LoadGinApi(pathPrefix string, root gin.IRouter) {
objects := store.Namespace(API_NAMESPACE)
objects.ForEach(func(w *ObjectWrapper) {
api, ok := w.Value.(GinApiObject)
if !ok {
return
}
if pathPrefix != "" && !strings.HasPrefix(pathPrefix, "/") {
pathPrefix = "/" + pathPrefix
}
api.Registry(root.Group(path.Join(pathPrefix, api.Version(), api.Name())))
})
}
// LoadHttpApp 装载所有的http app
func LoadGoRestfulApi(pathPrefix string, root *restful.Container) {
objects := store.Namespace(API_NAMESPACE)
objects.ForEach(func(w *ObjectWrapper) {
api, ok := w.Value.(GoRestfulApiObject)
if !ok {
return
}
pathPrefix = strings.TrimSuffix(pathPrefix, "/")
ws := new(restful.WebService)
ws.
Path(fmt.Sprintf("%s/%s/%s", pathPrefix, api.Version(), api.Name())).
Consumes(restful.MIME_JSON, form.MIME_POST_FORM, form.MIME_MULTIPART_FORM, yaml.MIME_YAML, yamlk8s.MIME_YAML).
Produces(restful.MIME_JSON, yaml.MIME_YAML, yamlk8s.MIME_YAML)
api.Registry(ws)
root.Add(ws)
})
}
1
https://gitee.com/go-kade/mcube.git
git@gitee.com:go-kade/mcube.git
go-kade
mcube
mcube
1225d9a674f1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891