5 Star 22 Fork 25

go-course/go12

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
Mr.Yu 提交于 2023-11-18 11:52 +08:00 . 补充基于简单框架处理
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/ioc"
"github.com/infraboard/mcube/ioc/config/logger"
"github.com/rs/zerolog"
"gitee.com/go-course/go12/devcloud-mini/example/apps/helloworld"
)
func init() {
ioc.Api().Registry(&HelloServiceApiHandler{})
}
// 3. 暴露HTTP接口
type HelloServiceApiHandler struct {
// 依赖业务控制器
// 使用ioc注解来从自动加载依赖对象, 等同于手动执行:
// h.svc = ioc.Controller().Get("*impl.HelloService").(helloworld.HelloService)
// @autowire
Svc helloworld.HelloService `ioc:"autowire=true;namespace=controllers"`
// 日志相关配置已经托管到Ioc中, 由于是私有属性,所有受到注入, 具体见下边初始化方法
log *zerolog.Logger
// 继承自Ioc对象
ioc.ObjectImpl
}
// 对象自定义初始化
func (h *HelloServiceApiHandler) Init() error {
h.log = logger.Sub("helloworld.api")
return nil
}
// API路由
func (h *HelloServiceApiHandler) Registry(r gin.IRouter) {
r.GET("/", h.Hello)
}
// API接口具体实现
func (h *HelloServiceApiHandler) Hello(c *gin.Context) {
// 业务处理
resp := h.Svc.Hello()
h.log.Debug().Msg(resp)
// 业务响应
c.JSON(http.StatusOK, gin.H{
"data": resp,
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/go-course/go12.git
git@gitee.com:go-course/go12.git
go-course
go12
go12
8a8b3aa3c2e4

搜索帮助