Links
之前的ioc
map形式,注册name:“interface"
如果掉用其方法,就是实现init,这样才调
现在的ioc
切片,只需要注册 name,version
如果掉用其方法,就是实现init,这样才调,但是这种方式只要name和version,比上面更复杂好用
ioc 2.0
优化隐试接口
app1 ioc1 是老的版本 map
app ioc 是新的版本 slice
补充业务异常
req := ioc.NewLoadConfigRequest()
req.ConfigFile.Enabled = true
req.ConfigFile.Path = "/Users/kade.chen/go12-project/sso/etc/application.toml"
ioc.DevelopmentSetup(req)
自动加载注册init所有ioc
更快到官网
git tag v1.3.8
git push -f https://gitee.com/go-kade/library.git v1.3.8
https://pkg.go.dev/gitee.com/go-kade/library/tree/v1.3.8
https://pkg.go.dev/gitee.com/go-kade/library@v1.3.8
备注:https://deps.dev/go/gitee.com%2Fgo-kade%2Flibrary/v1.0.1-0.20240201092113-6e4b7db5c891/versions
如果不知道版本可以从这里看
最新 13分钟同步完成 21分钟前 https://deps.dev/go/gitee.com%2Fgo-kade%2Flibrary/v1.2.1/versions 这个出来
_ "gitee.com/go-kade/library/ioc/config/cors"
_ "gitee.com/go-kade/library/ioc/apps/apidoc/restful"
想用apidoc得闲注入cors
_ "gitee.com/go-kade/library/ioc/apps/apidoc/restful" //依赖cors,不然访问会失败,里面只有apidoc
_ "gitee.com/go-kade/library/ioc/apps/health/restful"
_ "gitee.com/go-kade/library/ioc/apps/metric/restful" //promethus的指标暴露
_ "gitee.com/go-kade/library/ioc/config/cors" //如果不加这个,跨域可能会有问题,另外apidoc也用不了
_ "gitee.com/go-kade/library/ioc/apps/apidoc/swaggo" gin
go-restful
// 开启apidoc 必须开启cors
_ "gitee.com/go-kade/library/ioc/apps/apidoc/restful"
// 开启Health健康检查
_ "gitee.com/go-kade/library/ioc/apps/health/restful"
// 开启Metric
// promethus的指标暴露
_ "gitee.com/go-kade/library/ioc/apps/metric/restful"
// 开启CORS, 允许资源跨域共享
_ "gitee.com/go-kade/library/ioc/config/cors/gorestful"
go-gin
// 引入生成好的API Doc代码 必须开启cors
_ "gitee.com/go-kade/library/examples/http_gin/docs"
// 引入集成工程
_ "gitee.com/go-kade/library/ioc/apps/apidoc/gin"
// 开启Health健康检查
_ "gitee.com/go-kade/library/ioc/apps/health/gin"
// 开启Metric
// promethus的指标暴露
_ "gitee.com/go-kade/library/ioc/apps/metric/gin"
// 开启CORS, 允许资源跨域共享
_ "gitee.com/go-kade/library/ioc/config/cors/gin"
package main
import (
"context"
"gitee.com/go-kade/library/ioc"
"gitee.com/go-kade/library/ioc/server"
"github.com/emicklei/go-restful/v3"
// 开启apidoc
_ "gitee.com/go-kade/library/ioc/apps/apidoc/restful"
// 开启Health健康检查
_ "gitee.com/go-kade/library/ioc/apps/health/restful"
// 开启Metric
_ "gitee.com/go-kade/library/ioc/apps/metric/restful"
// 开启CORS, 允许资源跨域共享
_ "gitee.com/go-kade/library/ioc/config/cors/gorestful"
// _ "gitee.com/go-kade/library/ioc/apps/apidoc/swaggo" gin
_ "gitee.com/go-kade/library/ioc/config/cors/gorestful"
"gitee.com/go-kade/library/ioc/config/gorestful"
)
func main() {
// 注册HTTP接口类
ioc.Api().Registry(&HelloServiceApiHandler{})
// 启动应用
// req := ioc.NewLoadConfigRequest()
// req.ConfigFile.Enabled = true
// req.ConfigFile.Path = "/Users/kade.chen/go12-project/library/etc/application.toml"
err := server.Run(context.Background(), nil)
if err != nil {
panic(err)
}
}
type HelloServiceApiHandler struct {
// 继承自Ioc对象
ioc.ObjectImpl
}
// 模块的名称, 会作为路径的一部分比如: /mcube_service/api/v1/hello_module/
// 路径构成规则 <service_name>/<path_prefix>/<service_version>/<module_name>
// http://127.0.0.1:8080/default/api/v1/hello_module
func (h *HelloServiceApiHandler) Name() string {
return "hello_module"
}
func (h *HelloServiceApiHandler) Version() string {
return "v1"
}
func (h *HelloServiceApiHandler) Init() error {
h.Registry()
return nil
}
// API路由
func (h *HelloServiceApiHandler) Registry() error {
ws := gorestful.InitRouter(h)
///default/api/v1/hello_module/
ws.Route(ws.GET("/").To(func(r *restful.Request, w *restful.Response) {
w.WriteAsJson(map[string]string{
"data": "hello mcube1111111",
})
}))
//default/api/v1/hello_module/cc
ws.Route(ws.GET("/cc").To(func(r *restful.Request, w *restful.Response) {
w.WriteAsJson(map[string]string{
"data": "hello mcube1111111",
})
}))
//default/api/v1/hello_module/cc/111
ws.Route(ws.GET("/cc/111").To(func(r *restful.Request, w *restful.Response) {
w.WriteAsJson(map[string]string{
"data": "hello mcube1111111",
})
}))
}
//自定义路由
func (i *SwaggerApiDoc) Meta() ioc.ObjectMeta {
return DefaultObjectMeta1()
}
func DefaultObjectMeta1() ioc.ObjectMeta {
return ioc.ObjectMeta{
// CustomPathPrefix: "/s", 必须要/号 http://127.0.0.1:8080/s
CustomPathPrefix: "/swagger.json",
// CustomPathPrefix: "/s",
Extra: map[string]string{},
}
}
package main
import (
"context"
"net/http"
"gitee.com/go-kade/library/ioc"
"gitee.com/go-kade/library/ioc/server"
"github.com/gin-gonic/gin"
// 引入生成好的API Doc代码
_ "gitee.com/go-kade/library/examples/http_gin/docs"
// 引入集成工程
_ "gitee.com/go-kade/library/ioc/apps/apidoc/gin"
// 开启Health健康检查
_ "gitee.com/go-kade/library/ioc/apps/health/gin"
// 开启Metric
_ "gitee.com/go-kade/library/ioc/apps/metric/gin"
// 开启CORS, 允许资源跨域共享
_ "gitee.com/go-kade/library/ioc/config/cors/gin"
"gitee.com/go-kade/library/ioc/config/gogin"
)
// @title Swagger Example API
// @version 1.0
// @description This is a sample server celler server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /api/v1
// @securityDefinitions.basic BasicAuth
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
func main() {
// 注册HTTP接口类
ioc.Api().Registry(&HelloServiceApiHandler{})
// 启动应用
err := server.Run(context.Background(), nil)
if err != nil {
panic(err)
}
}
type HelloServiceApiHandler struct {
// 继承自Ioc对象
ioc.ObjectImpl
}
// 模块的名称, 会作为路径的一部分比如: /mcube_service/api/v1/hello_module/
// 路径构成规则 <service_name>/<path_prefix>/<service_version>/<module_name>
func (h *HelloServiceApiHandler) Name() string {
return "hello_module"
}
func (h *HelloServiceApiHandler) Version() string {
return "v1"
}
func (h *HelloServiceApiHandler) Init() error {
h.Registry()
return nil
}
// API路由
func (h *HelloServiceApiHandler) Registry() {
r := gogin.InitRouter(h)
///default/api/v1/hello_module/
r.GET("/", h.Hello)
//default/api/v1/hello_module/cc
r.GET("/cc", h.Hello)
///default/api/v1/hello_module/cc/111
r.GET("/cc/111", h.Hello)
}
// @Summary 修改文章标签
// @Description 修改文章标签
// @Tags 文章管理
// @Produce json
// @Param id path int true "ID"
// @Param name query string true "ID"
// @Param state query int false "State"
// @Param modified_by query string true "ModifiedBy"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Router /api/v1/tags/{id} [put]
func (h *HelloServiceApiHandler) Hello(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"data": "hello mcube",
})
}
func (i *HelloServiceApiHandler) Meta() ioc.ObjectMeta {
return ioc.ObjectMeta(DefaultObjectMeta())
}
func DefaultObjectMeta() ObjectMeta {
return ObjectMeta{
// CustomPathPrefix: "/s", 必须要/号 http://127.0.0.1:8080/s
CustomPathPrefix: "/bb",
// CustomPathPrefix: "/s",
Extra: map[string]string{},
}
}
type ObjectMeta struct {
CustomPathPrefix string
Extra map[string]string
}