161 lines
4.7 KiB
Go
161 lines
4.7 KiB
Go
|
// Package construct ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 1:23 下午 2021/8/7
|
||
|
package construct
|
||
|
|
||
|
import (
|
||
|
"git.zhangdeman.cn/zhangdeman/center-config/dao"
|
||
|
"git.zhangdeman.cn/zhangdeman/center-config/define/config"
|
||
|
"git.zhangdeman.cn/zhangdeman/center-config/manager"
|
||
|
"git.zhangdeman.cn/zhangdeman/center-config/response"
|
||
|
"git.zhangdeman.cn/zhangdeman/gopkg/middleware/mysql"
|
||
|
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"go.uber.org/zap/zapcore"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
// ProjectPath 项目路径
|
||
|
ProjectPath string
|
||
|
// Env 运行环境
|
||
|
Env string
|
||
|
)
|
||
|
|
||
|
// Bootstrap 引导服务启动
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 1:25 下午 2021/8/7
|
||
|
func Bootstrap(router *gin.Engine) error {
|
||
|
// 初始化
|
||
|
if len(Env) == 0 {
|
||
|
Env = "product"
|
||
|
}
|
||
|
ProjectPath, _ = util.GetProjectPath()
|
||
|
// 初始化数据库
|
||
|
if err := InitDatabase(); nil != err {
|
||
|
return nil
|
||
|
}
|
||
|
// 初始化各种管理实例
|
||
|
manager.Init()
|
||
|
// 初始化请求路由
|
||
|
initRouter(router)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// InitDatabase 初始化数据库
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 1:27 下午 2021/8/7
|
||
|
func InitDatabase() error {
|
||
|
// 读取数据库配置, 并初始化
|
||
|
var (
|
||
|
err error
|
||
|
dbConfig config.Database
|
||
|
)
|
||
|
if err = util.ReadYmlConfig(ProjectPath+"/etc/database/"+Env+".yml", &dbConfig); nil != err {
|
||
|
return err
|
||
|
}
|
||
|
if err = dao.InitDatabase(dbConfig.DB, &mysql.LogConfig{
|
||
|
Level: zapcore.Level(dbConfig.Log.Level),
|
||
|
ConsoleOutput: dbConfig.Log.Console,
|
||
|
SplitConfig: dbConfig.Log.Split,
|
||
|
ExtractFieldList: dbConfig.Log.ExtractFieldList,
|
||
|
TraceFieldName: dbConfig.Log.TraceFieldName,
|
||
|
}); nil != err {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// initRouter 初始化路由
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 6:34 下午 2021/8/9
|
||
|
func initRouter(router *gin.Engine) {
|
||
|
// 创建命名空间
|
||
|
router.POST("/namespace/create", func(ctx *gin.Context) {
|
||
|
createForm, err := manager.Namespace.ValidateCreateForm(ctx)
|
||
|
if nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
if err := manager.Namespace.Create(createForm); nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
response.ManagerAPISuccess(ctx, map[string]interface{}{})
|
||
|
})
|
||
|
// 激活命名空间
|
||
|
router.POST("/namespace/active", func(ctx *gin.Context) {
|
||
|
activeForm, err := manager.Namespace.ValidateActiveForm(ctx)
|
||
|
if nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
if err := manager.Namespace.Active(activeForm); nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
response.ManagerAPISuccess(ctx, map[string]interface{}{})
|
||
|
})
|
||
|
// 新增配置
|
||
|
router.POST("/config/create", func(ctx *gin.Context) {
|
||
|
createForm, err := manager.Config.ValidateCreateOrUpdateConfigForm(ctx)
|
||
|
if nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
if err := manager.Config.Create(createForm, nil); nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
response.ManagerAPISuccess(ctx, map[string]interface{}{})
|
||
|
})
|
||
|
// 修改配置
|
||
|
router.POST("/config/update", func(ctx *gin.Context) {
|
||
|
updateForm, err := manager.Config.ValidateCreateOrUpdateConfigForm(ctx)
|
||
|
if nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
if err := manager.Config.Update(updateForm, nil, nil); nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
response.ManagerAPISuccess(ctx, map[string]interface{}{})
|
||
|
})
|
||
|
// 设置配置
|
||
|
router.POST("/config/set", func(ctx *gin.Context) {
|
||
|
createOrUpdateForm, err := manager.Config.ValidateCreateOrUpdateConfigForm(ctx)
|
||
|
if nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
if err := manager.Config.CreateOrUpdateConfig(createOrUpdateForm); nil != err {
|
||
|
response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
response.ManagerAPISuccess(ctx, map[string]interface{}{})
|
||
|
})
|
||
|
|
||
|
// 读取配置(从内存读取)
|
||
|
router.GET("/config/read", func(ctx *gin.Context) {
|
||
|
namespaceName := ctx.DefaultQuery("namespace", "")
|
||
|
key := ctx.DefaultQuery("key", "")
|
||
|
if len(namespaceName) == 0 || len(key) == 0 {
|
||
|
response.ReadConfigAPISuccess(ctx, `param fail : namespace and key are required`)
|
||
|
return
|
||
|
}
|
||
|
response.ReadConfigAPISuccess(ctx, manager.Storage.GetConf(namespaceName, key))
|
||
|
})
|
||
|
if err := router.Run(":10998"); nil != err {
|
||
|
panic("端口监听失败 : " + err.Error())
|
||
|
}
|
||
|
}
|