feat: add auto migrate and init account
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"message-nest/migrate"
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/logging"
|
||||
"message-nest/pkg/setting"
|
||||
@@ -26,9 +27,10 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
logging.Setup()
|
||||
migrate.Setup()
|
||||
setting.Setup()
|
||||
models.Setup()
|
||||
logging.Setup()
|
||||
table.Setup()
|
||||
env_service.Setup()
|
||||
cron_service.Setup()
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/jinzhu/gorm"
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/logging"
|
||||
"message-nest/pkg/setting"
|
||||
)
|
||||
|
||||
func InitAuthTableData() {
|
||||
initSection := "init"
|
||||
initAuthKey := "account"
|
||||
initAccount := "admin"
|
||||
initAccountPasswd := "123456"
|
||||
|
||||
settingO, err := models.GetSettingByKey(initSection, initAuthKey)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logging.Logger.Error(fmt.Sprintf("查询账号初始化失败!"))
|
||||
return
|
||||
}
|
||||
if settingO.Value == "1" {
|
||||
// 已经初始化过
|
||||
return
|
||||
}
|
||||
err = models.AddUser(initAccount, initAccountPasswd)
|
||||
if err != nil {
|
||||
logging.Logger.Error(fmt.Sprintf("添加初始化admin账号失败!"))
|
||||
return
|
||||
} else {
|
||||
logging.Logger.Error(fmt.Sprintf("初始化admin账号成功!您的账号:%s 密码:%s", initAccount, initAccountPasswd))
|
||||
}
|
||||
|
||||
err = models.AddOneSetting(models.Settings{Section: initSection, Key: initAuthKey, Value: "1"})
|
||||
if err != nil {
|
||||
logging.Logger.Error(fmt.Sprintf("标记admin账号初始化状态失败!"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Setup() {
|
||||
setting.Setup()
|
||||
db := models.Setup()
|
||||
defer func(db *gorm.DB) {
|
||||
err := db.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(db)
|
||||
|
||||
db.AutoMigrate(
|
||||
&models.Auth{},
|
||||
&models.SendTasks{},
|
||||
&models.SendWays{},
|
||||
&models.SendTasksLogs{},
|
||||
&models.SendTasksIns{},
|
||||
&models.Settings{},
|
||||
)
|
||||
|
||||
InitAuthTableData()
|
||||
|
||||
}
|
||||
+19
-5
@@ -1,18 +1,21 @@
|
||||
package models
|
||||
|
||||
import "github.com/jinzhu/gorm"
|
||||
import (
|
||||
"errors"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type Auth struct {
|
||||
ID int `gorm:"primary_key" json:"id"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
ID int `json:"id" gorm:"type:int(11) AUTO_INCREMENT comment 'id';primary_key" json:"id"`
|
||||
Username string `json:"username" gorm:"type:varchar(100) comment '用户名';default:'';"`
|
||||
Password string `json:"password" gorm:"type:varchar(100) comment '密码';default:'';"`
|
||||
}
|
||||
|
||||
// CheckAuth 检查用户信息
|
||||
func CheckAuth(username, password string) (bool, error) {
|
||||
var auth Auth
|
||||
err := db.Select("id").Where(Auth{Username: username, Password: password}).First(&auth).Error
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
@@ -30,3 +33,14 @@ func EditUser(username string, data interface{}) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddUser(account string, password string) error {
|
||||
auth := Auth{
|
||||
Username: account,
|
||||
Password: password,
|
||||
}
|
||||
if err := db.Create(&auth).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+12
-11
@@ -14,25 +14,25 @@ import (
|
||||
var db *gorm.DB
|
||||
|
||||
type IDModel struct {
|
||||
ID int `gorm:"primary_key" json:"id"`
|
||||
ID int `gorm:"type:int(11) AUTO_INCREMENT comment 'id';primary_key" json:"id"`
|
||||
|
||||
CreatedBy string `json:"created_by"`
|
||||
ModifiedBy string `json:"modified_by"`
|
||||
CreatedOn util.Time `json:"created_on"`
|
||||
ModifiedOn util.Time `json:"modified_on"`
|
||||
CreatedBy string `json:"created_by" gorm:"type:varchar(100) comment '创建人';default:'';"`
|
||||
ModifiedBy string `json:"modified_by" gorm:"type:varchar(100) comment '修改人';default:'';"`
|
||||
CreatedOn util.Time `json:"created_on" gorm:"type:timestamp comment '创建时间';default:current_timestamp;"`
|
||||
ModifiedOn util.Time `json:"modified_on" gorm:"type:timestamp comment '更新时间';default:current_timestamp on update current_timestamp;"`
|
||||
}
|
||||
|
||||
type UUIDModel struct {
|
||||
ID uuid.UUID `gorm:"primary_key" json:"id"`
|
||||
ID uuid.UUID `gorm:"type:varchar(36) comment 'id';primary_key" json:"id"`
|
||||
|
||||
CreatedBy string `json:"created_by"`
|
||||
ModifiedBy string `json:"modified_by"`
|
||||
CreatedOn util.Time `json:"created_on"`
|
||||
ModifiedOn util.Time `json:"modified_on"`
|
||||
CreatedBy string `json:"created_by" gorm:"type:varchar(100) comment '创建人';default:'';"`
|
||||
ModifiedBy string `json:"modified_by" gorm:"type:varchar(100) comment '修改人';default:'';"`
|
||||
CreatedOn util.Time `json:"created_on" gorm:"type:timestamp comment '创建时间';default:current_timestamp;"`
|
||||
ModifiedOn util.Time `json:"modified_on" gorm:"type:timestamp comment '更新时间';default:current_timestamp on update current_timestamp;"`
|
||||
}
|
||||
|
||||
// Setup initializes the database instance
|
||||
func Setup() {
|
||||
func Setup() *gorm.DB {
|
||||
var err error
|
||||
connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
setting.DatabaseSetting.User,
|
||||
@@ -58,6 +58,7 @@ func Setup() {
|
||||
db.Callback().Delete().Replace("gorm:delete", deleteCallback)
|
||||
db.DB().SetMaxIdleConns(10)
|
||||
db.DB().SetMaxOpenConns(100)
|
||||
return db
|
||||
}
|
||||
|
||||
// CloseDB closes database connection (unnecessary)
|
||||
|
||||
+6
-6
@@ -3,12 +3,12 @@ package models
|
||||
type SendTasksIns struct {
|
||||
UUIDModel
|
||||
|
||||
TaskID string `json:"task_id"`
|
||||
WayID string `json:"way_id"`
|
||||
WayType string `json:"way_type"`
|
||||
ContentType string `json:"content_type"`
|
||||
Config string `json:"config"`
|
||||
Extra string `json:"extra"`
|
||||
TaskID string `json:"task_id" gorm:"type:varchar(36) comment '任务id';default:'';index:task_id"`
|
||||
WayID string `json:"way_id" gorm:"type:varchar(36) comment '渠道id';default:'';index:way_id"`
|
||||
WayType string `json:"way_type" gorm:"type:varchar(100) comment '渠道类型';default:'';index:way_type"`
|
||||
ContentType string `json:"content_type" gorm:"type:varchar(100) comment '实例类型';default:'';index:content_type"`
|
||||
Config string `json:"config" gorm:"type:text comment '实例配置';"`
|
||||
Extra string `json:"extra" gorm:"type:text comment '额外信息';"`
|
||||
}
|
||||
|
||||
// InsEmailConfig 实例里面的邮箱config
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
type SendTasks struct {
|
||||
UUIDModel
|
||||
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name" gorm:"type:varchar(100) comment '任务名称';default:'';"`
|
||||
}
|
||||
|
||||
// AddSendTaskWithID 添加实例的时候添加任务
|
||||
|
||||
@@ -7,13 +7,13 @@ import (
|
||||
)
|
||||
|
||||
type SendTasksLogs struct {
|
||||
ID int `gorm:"primary_key" json:"id"`
|
||||
TaskID string `json:"task_id"`
|
||||
Log string `json:"log"`
|
||||
Status int `json:"status"`
|
||||
ID int `gorm:"primary_key" json:"id" `
|
||||
TaskID string `json:"task_id" gorm:"type:varchar(36) comment '任务id';default:'';index:task_id"`
|
||||
Log string `json:"log" gorm:"type:text comment '日志';"`
|
||||
Status int `json:"status" gorm:"type:int comment '状态';default:0;"`
|
||||
|
||||
CreatedOn util.Time `json:"created_on"`
|
||||
ModifiedOn util.Time `json:"modified_on"`
|
||||
CreatedOn util.Time `json:"created_on" gorm:"type:timestamp comment '创建时间';default:current_timestamp;"`
|
||||
ModifiedOn util.Time `json:"modified_on" gorm:"type:timestamp comment '更新时间';default:current_timestamp on update current_timestamp;"`
|
||||
}
|
||||
|
||||
// Add 添加日志记录
|
||||
|
||||
+3
-3
@@ -10,9 +10,9 @@ import (
|
||||
type SendWays struct {
|
||||
UUIDModel
|
||||
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Auth string `gorm:"not null" json:"auth"`
|
||||
Name string `json:"name" gorm:"type:varchar(100) comment '渠道名称';default:'';"`
|
||||
Type string `json:"type" gorm:"type:varchar(100) comment '渠道类型';default:'';index:type"`
|
||||
Auth string `json:"auth" gorm:"type:varchar(2048) comment '认证信息';default:'';"`
|
||||
}
|
||||
|
||||
func AddSendWay(name string, auth string, wayType string, createdBy string, modifiedBy string) error {
|
||||
|
||||
+3
-3
@@ -9,9 +9,9 @@ import (
|
||||
type Settings struct {
|
||||
IDModel
|
||||
|
||||
Section string `json:"section"`
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
Section string `json:"section" gorm:"type:varchar(100) comment '实例类型';default:'';index:section"`
|
||||
Key string `json:"key" gorm:"type:varchar(100) comment '实例类型';default:'';"`
|
||||
Value string `json:"value" gorm:"type:varchar(4096) comment '实例类型';default:'';"`
|
||||
}
|
||||
|
||||
// AddOneSetting 添加一条设置
|
||||
|
||||
Reference in New Issue
Block a user