File tree Expand file tree Collapse file tree 4 files changed +87
-10
lines changed Expand file tree Collapse file tree 4 files changed +87
-10
lines changed Original file line number Diff line number Diff line change @@ -6,9 +6,6 @@ key = "this is your app key"
66host = " 127.0.0.1"
77port = 8020
88
9- [metric ]
10- enable = true
11-
129[datasource ]
1310host = " 127.0.0.1"
1411port = 3306
Original file line number Diff line number Diff line change 1+ [app ]
2+ name = " simple"
3+ key = " this is your app key"
4+
5+ [http ]
6+ host = " 127.0.0.1"
7+ port = 8020
8+
9+ [datasource ]
10+ host = " 127.0.0.1"
11+ port = 3306
12+ username = " root"
13+ password = " 123456"
14+ database = " test"
15+
16+ [log ]
17+ level = " debug"
18+
19+ [log .file ]
20+ enable = true
21+ file_path = " logs/app.log"
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "context"
5+ "net/http"
6+
7+ "github.com/gin-gonic/gin"
8+ "github.com/infraboard/mcube/v2/ioc"
9+ "github.com/infraboard/mcube/v2/ioc/config/datasource"
10+ "github.com/infraboard/mcube/v2/ioc/server"
11+ "gorm.io/gorm"
12+ )
13+
14+ func main () {
15+ // 注册HTTP接口类
16+ ioc .Api ().Registry (& ApiHandler {})
17+
18+ // 开启配置文件读取配置
19+ server .DefaultConfig .ConfigFile .Enabled = true
20+ server .DefaultConfig .ConfigFile .Path = "etc/application.toml"
21+
22+ // 启动应用
23+ err := server .Run (context .Background ())
24+ if err != nil {
25+ panic (err )
26+ }
27+ }
28+
29+ type ApiHandler struct {
30+ // 继承自Ioc对象
31+ ioc.ObjectImpl
32+
33+ // mysql db依赖
34+ db * gorm.DB
35+ }
36+
37+ // 覆写对象的名称, 该名称名称会体现在API的路径前缀里面
38+ // 比如: /simple/api/v1/module_a/db_stats
39+ // 其中/simple/api/v1/module_a 就是对象API前缀, 命名规则如下:
40+ // <service_name>/<path_prefix>/<object_version>/<object_name>
41+ func (h * ApiHandler ) Name () string {
42+ return "module_a"
43+ }
44+
45+ // 初始化db属性, 从ioc的配置区域获取共用工具 gorm db对象
46+ func (h * ApiHandler ) Init () error {
47+ h .db = datasource .DB ()
48+ return nil
49+ }
50+
51+ // API路由
52+ func (h * ApiHandler ) Registry (r gin.IRouter ) {
53+ r .GET ("/db_stats" , func (ctx * gin.Context ) {
54+ db , _ := h .db .DB ()
55+ ctx .JSON (http .StatusOK , gin.H {
56+ "data" : db .Stats (),
57+ })
58+ })
59+ }
Original file line number Diff line number Diff line change @@ -27,21 +27,21 @@ type StoreManage interface {
2727 LoadFromEnv (prefix string ) error
2828}
2929
30- // Object 内部服务实例, 不需要暴露
30+ // Object接口, 需要注册到ioc空间托管的对象需要实现的方法
3131type Object interface {
32- // 对象初始化
32+ // 对象初始化, 初始化对象的属性
3333 Init () error
34- // 对象的名称
34+ // 对象的名称, 根据名称可以从空间中取出对象
3535 Name () string
36- // 对象版本
36+ // 对象版本, 默认v1
3737 Version () string
38- // 对象优先级
38+ // 对象优先级, 根据优先级 控制对象初始化的顺序
3939 Priority () int
40- // 对象的销毁
40+ // 对象的销毁, 服务关闭时调用
4141 Close (ctx context.Context ) error
4242 // 是否允许同名对象被替换, 默认不允许被替换
4343 AllowOverwrite () bool
44- // 对象一些元数据
44+ // 对象一些元数据, 对象的更多描述信息, 扩展使用
4545 Meta () ObjectMeta
4646}
4747
You can’t perform that action at this time.
0 commit comments