-
Notifications
You must be signed in to change notification settings - Fork 44
/
http.go
52 lines (42 loc) · 1.22 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/examples/project/apps/helloworld"
"github.com/infraboard/mcube/v2/ioc"
"github.com/infraboard/mcube/v2/ioc/config/log"
"github.com/rs/zerolog"
)
func init() {
ioc.Api().Registry(&HelloServiceApiHandler{})
}
// 3. 暴露HTTP接口
type HelloServiceApiHandler struct {
// 依赖业务控制器
// 使用ioc注解来从自动加载依赖对象, 等同于手动执行:
// h.svc = ioc.Controller().Get("*impl.HelloService").(helloworld.HelloService)
Svc helloworld.HelloService `ioc:"autowire=true;namespace=controllers"`
// 日志相关配置已经托管到Ioc中, 由于是私有属性,所有受到注入, 具体见下边初始化方法
log *zerolog.Logger
// 继承自Ioc对象
ioc.ObjectImpl
}
// 对象自定义初始化
func (h *HelloServiceApiHandler) Init() error {
h.log = log.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,
})
}