服务端 admin-core 引入
admin-core 是管理后台项目 vue3-element-admi 的后端支持项目。本身也是 niuhe 插件生成的项目. 可在项目中通过简单的设置来接入它
1. main 中引入
以 demo
项目为例, 当前 src/demo/main.go
内容为:
package main
// Generated by niuhe.idl
import (
apiViews "demo/app/api/views"
"demo/config"
"fmt"
"github.com/ma-guo/niuhe"
"os"
)
type baseBoot struct{}
type BaseBoot struct {
baseBoot
}
func (baseBoot) LoadConfig() error {
if len(os.Args) < 2 {
return fmt.Errorf("Usage: %s <config-path>", os.Args[0])
}
return config.LoadConfig(os.Args[1])
}
func (baseBoot) BeforeBoot(svr *niuhe.Server) {}
func (baseBoot) RegisterModules(svr *niuhe.Server) {
svr.RegisterModule(apiViews.GetModule())
}
func (baseBoot) Serve(svr *niuhe.Server) {
svr.Serve(config.Config.ServerAddr)
}
func main() {
boot := BaseBoot{}
if err := boot.LoadConfig(); err != nil {
panic(err)
}
svr := niuhe.NewServer()
boot.BeforeBoot(svr)
boot.RegisterModules(svr)
boot.Serve(svr)
}
接入步骤:
- 在 import 中添加依赖
adminBoot "github.com/ma-guo/admin-core/boot"
- 加载 admin-core 配置文件
- 修改
RegisterModules
方法, 注册 admin-core 模块 接入后代码如下
package main
// Generated by niuhe.idl
import (
apiViews "demo/app/api/views"
"demo/config"
"fmt"
"os"
adminBoot "github.com/ma-guo/admin-core/boot" // 第一步
"github.com/ma-guo/niuhe"
)
type baseBoot struct{}
type BaseBoot struct {
baseBoot
}
func (baseBoot) LoadConfig() error {
if len(os.Args) < 2 {
return fmt.Errorf("Usage: %s <config-path>", os.Args[0])
}
return config.LoadConfig(os.Args[1])
}
func (baseBoot) BeforeBoot(svr *niuhe.Server) {}
func (baseBoot) RegisterModules(svr *niuhe.Server) {
svr.RegisterModule(apiViews.GetModule())
}
func (baseBoot) Serve(svr *niuhe.Server) {
svr.Serve(config.Config.ServerAddr)
}
func main() {
boot := BaseBoot{}
if err := boot.LoadConfig(); err != nil {
panic(err)
}
// 第二步, 加载配置文件
admin := adminBoot.AdminBoot{}
if err := admin.LoadConfig(os.Args[1]); err != nil {
niuhe.LogInfo("admin config error: %v", err)
return
}
svr := niuhe.NewServer()
boot.BeforeBoot(svr)
// 第三步, 注册 admin-core 模块
admin.RegisterModules(svr)
boot.RegisterModules(svr)
boot.Serve(svr)
}
其他
接入后续在 src/demo
目录下运行 go mod tidy
和 go mod vendor
来下载依赖