-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dbf778
commit aeae749
Showing
4 changed files
with
87 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[app] | ||
name = "simple" | ||
key = "this is your app key" | ||
|
||
[http] | ||
host = "127.0.0.1" | ||
port = 8020 | ||
|
||
[datasource] | ||
host = "127.0.0.1" | ||
port = 3306 | ||
username = "root" | ||
password = "123456" | ||
database = "test" | ||
|
||
[log] | ||
level = "debug" | ||
|
||
[log.file] | ||
enable = true | ||
file_path = "logs/app.log" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/infraboard/mcube/v2/ioc" | ||
"github.com/infraboard/mcube/v2/ioc/config/datasource" | ||
"github.com/infraboard/mcube/v2/ioc/server" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func main() { | ||
// 注册HTTP接口类 | ||
ioc.Api().Registry(&ApiHandler{}) | ||
|
||
// 开启配置文件读取配置 | ||
server.DefaultConfig.ConfigFile.Enabled = true | ||
server.DefaultConfig.ConfigFile.Path = "etc/application.toml" | ||
|
||
// 启动应用 | ||
err := server.Run(context.Background()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
type ApiHandler struct { | ||
// 继承自Ioc对象 | ||
ioc.ObjectImpl | ||
|
||
// mysql db依赖 | ||
db *gorm.DB | ||
} | ||
|
||
// 覆写对象的名称, 该名称名称会体现在API的路径前缀里面 | ||
// 比如: /simple/api/v1/module_a/db_stats | ||
// 其中/simple/api/v1/module_a 就是对象API前缀, 命名规则如下: | ||
// <service_name>/<path_prefix>/<object_version>/<object_name> | ||
func (h *ApiHandler) Name() string { | ||
return "module_a" | ||
} | ||
|
||
// 初始化db属性, 从ioc的配置区域获取共用工具 gorm db对象 | ||
func (h *ApiHandler) Init() error { | ||
h.db = datasource.DB() | ||
return nil | ||
} | ||
|
||
// API路由 | ||
func (h *ApiHandler) Registry(r gin.IRouter) { | ||
r.GET("/db_stats", func(ctx *gin.Context) { | ||
db, _ := h.db.DB() | ||
ctx.JSON(http.StatusOK, gin.H{ | ||
"data": db.Stats(), | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters