1\
This commit is contained in:
8
cmd/api/jobs.go
Normal file
8
cmd/api/jobs.go
Normal file
@ -0,0 +1,8 @@
|
||||
package api
|
||||
|
||||
import "go-admin/app/jobs/router"
|
||||
|
||||
func init() {
|
||||
//注册路由 fixme 其他应用的路由,在本目录新建文件放在init方法
|
||||
AppRouters = append(AppRouters, router.InitRouter)
|
||||
}
|
||||
8
cmd/api/other.go
Normal file
8
cmd/api/other.go
Normal file
@ -0,0 +1,8 @@
|
||||
package api
|
||||
|
||||
import "go-admin/app/other/router"
|
||||
|
||||
func init() {
|
||||
//注册路由 fixme 其他应用的路由,在本目录新建文件放在init方法
|
||||
AppRouters = append(AppRouters, router.InitRouter)
|
||||
}
|
||||
221
cmd/api/server.go
Normal file
221
cmd/api/server.go
Normal file
@ -0,0 +1,221 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/logger"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/runtime"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-admin-team/go-admin-core/config/source/file"
|
||||
"github.com/go-admin-team/go-admin-core/sdk"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/api"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/config"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"go-admin/app/admin/models"
|
||||
"go-admin/app/admin/router"
|
||||
"go-admin/app/admin/service"
|
||||
"go-admin/app/jobs"
|
||||
"go-admin/common/database"
|
||||
"go-admin/common/global"
|
||||
common "go-admin/common/middleware"
|
||||
"go-admin/common/middleware/handler"
|
||||
"go-admin/common/storage"
|
||||
ext "go-admin/config"
|
||||
"go-admin/utils/redishelper"
|
||||
"go-admin/utils/utility"
|
||||
)
|
||||
|
||||
var (
|
||||
configYml string
|
||||
apiCheck bool
|
||||
StartCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Short: "Start API server",
|
||||
Example: "go-admin server -c config/settings.yml",
|
||||
SilenceUsage: true,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setup()
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return run()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
var AppRouters = make([]func(), 0)
|
||||
|
||||
func init() {
|
||||
StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file")
|
||||
StartCmd.PersistentFlags().BoolVarP(&apiCheck, "api", "a", false, "Start server with check api data")
|
||||
|
||||
//注册路由 fixme 其他应用的路由,在本目录新建文件放在init方法
|
||||
AppRouters = append(AppRouters, router.InitRouter)
|
||||
}
|
||||
|
||||
func setup() {
|
||||
// 注入配置扩展项
|
||||
config.ExtendConfig = &ext.ExtConfig
|
||||
//1. 读取配置
|
||||
config.Setup(
|
||||
file.NewSource(file.WithPath(configYml)),
|
||||
database.Setup,
|
||||
storage.Setup,
|
||||
)
|
||||
//注册监听函数
|
||||
queue := sdk.Runtime.GetMemoryQueue("")
|
||||
queue.Register(global.LoginLog, models.SaveLoginLog)
|
||||
queue.Register(global.OperateLog, models.SaveOperaLog)
|
||||
queue.Register(global.ApiCheck, models.SaveSysApi)
|
||||
go queue.Run()
|
||||
|
||||
usageStr := `starting api server...`
|
||||
log.Println(usageStr)
|
||||
}
|
||||
|
||||
func run() error {
|
||||
if config.ApplicationConfig.Mode == pkg.ModeProd.String() {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
initRouter()
|
||||
|
||||
initCommon()
|
||||
|
||||
initBusinesses()
|
||||
|
||||
for _, f := range AppRouters {
|
||||
f()
|
||||
}
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%d", config.ApplicationConfig.Host, config.ApplicationConfig.Port),
|
||||
Handler: sdk.Runtime.GetEngine(),
|
||||
}
|
||||
|
||||
go func() {
|
||||
jobs.InitJob()
|
||||
jobs.Setup(sdk.Runtime.GetDb())
|
||||
|
||||
}()
|
||||
|
||||
if apiCheck {
|
||||
var routers = sdk.Runtime.GetRouter()
|
||||
q := sdk.Runtime.GetMemoryQueue("")
|
||||
mp := make(map[string]interface{}, 0)
|
||||
mp["List"] = routers
|
||||
message, err := sdk.Runtime.GetStreamMessage("", global.ApiCheck, mp)
|
||||
if err != nil {
|
||||
log.Printf("GetStreamMessage error, %s \n", err.Error())
|
||||
//日志报错错误,不中断请求
|
||||
} else {
|
||||
err = q.Append(message)
|
||||
if err != nil {
|
||||
log.Printf("Append message error, %s \n", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
// 服务连接
|
||||
if config.SslConfig.Enable {
|
||||
if err := srv.ListenAndServeTLS(config.SslConfig.Pem, config.SslConfig.KeyStr); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatal("listen: ", err)
|
||||
}
|
||||
} else {
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatal("listen: ", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
fmt.Println(pkg.Red(string(global.LogoContent)))
|
||||
tip()
|
||||
fmt.Println(pkg.Green("Server run at:"))
|
||||
fmt.Printf("- Local: %s://localhost:%d/ \r\n", "http", config.ApplicationConfig.Port)
|
||||
fmt.Printf("- Network: %s://%s:%d/ \r\n", pkg.GetLocaHonst(), "http", config.ApplicationConfig.Port)
|
||||
fmt.Println(pkg.Green("Swagger run at:"))
|
||||
fmt.Printf("- Local: http://localhost:%d/swagger/admin/index.html \r\n", config.ApplicationConfig.Port)
|
||||
fmt.Printf("- Network: %s://%s:%d/swagger/admin/index.html \r\n", "http", pkg.GetLocaHonst(), config.ApplicationConfig.Port)
|
||||
fmt.Printf("%s Enter Control + C Shutdown Server \r\n", pkg.GetCurrentTimeStr())
|
||||
// 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, os.Interrupt)
|
||||
<-quit
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
fmt.Printf("%s Shutdown Server ... \r\n", pkg.GetCurrentTimeStr())
|
||||
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
log.Fatal("Server Shutdown:", err)
|
||||
}
|
||||
log.Println("Server exiting")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var Router runtime.Router
|
||||
|
||||
func tip() {
|
||||
usageStr := `欢迎使用 ` + pkg.Green(`go-admin `+global.Version) + ` 可以使用 ` + pkg.Red(`-h`) + ` 查看命令`
|
||||
fmt.Printf("%s \n\n", usageStr)
|
||||
}
|
||||
|
||||
func initRouter() {
|
||||
var r *gin.Engine
|
||||
h := sdk.Runtime.GetEngine()
|
||||
if h == nil {
|
||||
h = gin.New()
|
||||
sdk.Runtime.SetEngine(h)
|
||||
}
|
||||
switch h.(type) {
|
||||
case *gin.Engine:
|
||||
r = h.(*gin.Engine)
|
||||
default:
|
||||
log.Fatal("not support other engine")
|
||||
//os.Exit(-1)
|
||||
}
|
||||
if config.SslConfig.Enable {
|
||||
r.Use(handler.TlsHandler())
|
||||
}
|
||||
//r.Use(middleware.Metrics())
|
||||
r.Use(common.Sentinel()).
|
||||
Use(common.RequestId(pkg.TrafficKey)).
|
||||
Use(api.SetRequestLogger)
|
||||
|
||||
common.InitMiddleware(r)
|
||||
|
||||
}
|
||||
|
||||
func initCommon() {
|
||||
//初始化雪花算法
|
||||
utility.InitSnowflake()
|
||||
|
||||
redishelper.InitDefaultRedis(config.CacheConfig.Redis.Addr, config.CacheConfig.Redis.Password, config.CacheConfig.Redis.DB)
|
||||
|
||||
if err := redishelper.DefaultRedis.Ping(); err != nil {
|
||||
fmt.Println("redis链接失败", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
redishelper.InitLockRedisConn(config.CacheConfig.Redis.Addr, config.CacheConfig.Redis.Password, strconv.Itoa(config.CacheConfig.Redis.DB))
|
||||
|
||||
}
|
||||
|
||||
func initBusinesses() {
|
||||
cliProxyService := service.CliProxyService{}
|
||||
cliProxyService.Orm = jobs.GetDb()
|
||||
cliProxyService.Log = logger.NewHelper(logger.DefaultLogger)
|
||||
|
||||
if _, err := cliProxyService.GetTrafficInfo(); err != nil {
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
90
cmd/app/server.go
Normal file
90
cmd/app/server.go
Normal file
@ -0,0 +1,90 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/utils"
|
||||
"github.com/spf13/cobra"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var (
|
||||
appName string
|
||||
StartCmd = &cobra.Command{
|
||||
Use: "app",
|
||||
Short: "Create a new app",
|
||||
Long: "Use when you need to create a new app",
|
||||
Example: "go-admin app -n admin",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
run()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
StartCmd.PersistentFlags().StringVarP(&appName, "name", "n", "", "Start server with provided configuration file")
|
||||
}
|
||||
|
||||
func run() {
|
||||
|
||||
fmt.Println(`start init`)
|
||||
//1. 读取配置
|
||||
|
||||
fmt.Println(`generate migration file`)
|
||||
_ = genFile()
|
||||
|
||||
}
|
||||
|
||||
func genFile() error {
|
||||
if appName == "" {
|
||||
return errors.New("arg `name` invalid :name is empty")
|
||||
}
|
||||
path := "app/"
|
||||
appPath := path + appName
|
||||
err := utils.IsNotExistMkDir(appPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
apiPath := appPath + "/apis/"
|
||||
err = utils.IsNotExistMkDir(apiPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
modelsPath := appPath + "/models/"
|
||||
err = utils.IsNotExistMkDir(modelsPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
routerPath := appPath + "/router/"
|
||||
err = utils.IsNotExistMkDir(routerPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
servicePath := appPath + "/service/"
|
||||
err = utils.IsNotExistMkDir(servicePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dtoPath := appPath + "/service/dto/"
|
||||
err = utils.IsNotExistMkDir(dtoPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t1, err := template.ParseFiles("template/cmd_api.template")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m := map[string]string{}
|
||||
m["appName"] = appName
|
||||
var b1 bytes.Buffer
|
||||
err = t1.Execute(&b1, m)
|
||||
pkg.FileCreate(b1, "./cmd/api/"+appName+".go")
|
||||
t2, err := template.ParseFiles("template/router.template")
|
||||
var b2 bytes.Buffer
|
||||
err = t2.Execute(&b2, nil)
|
||||
pkg.FileCreate(b2, appPath+"/router/router.go")
|
||||
return nil
|
||||
}
|
||||
57
cmd/cobra.go
Normal file
57
cmd/cobra.go
Normal file
@ -0,0 +1,57 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
"go-admin/cmd/app"
|
||||
"go-admin/common/global"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"go-admin/cmd/api"
|
||||
"go-admin/cmd/config"
|
||||
"go-admin/cmd/migrate"
|
||||
"go-admin/cmd/version"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "go-admin",
|
||||
Short: "go-admin",
|
||||
SilenceUsage: true,
|
||||
Long: `go-admin`,
|
||||
Args: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) < 1 {
|
||||
tip()
|
||||
return errors.New(pkg.Red("requires at least one arg"))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
PersistentPreRunE: func(*cobra.Command, []string) error { return nil },
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
tip()
|
||||
},
|
||||
}
|
||||
|
||||
func tip() {
|
||||
usageStr := `欢迎使用 ` + pkg.Green(`go-admin `+global.Version) + ` 可以使用 ` + pkg.Red(`-h`) + ` 查看命令`
|
||||
usageStr1 := `也可以参考 https://doc.go-admin.dev/guide/ksks 的相关内容`
|
||||
fmt.Printf("%s\n", usageStr)
|
||||
fmt.Printf("%s\n", usageStr1)
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(api.StartCmd)
|
||||
rootCmd.AddCommand(migrate.StartCmd)
|
||||
rootCmd.AddCommand(version.StartCmd)
|
||||
rootCmd.AddCommand(config.StartCmd)
|
||||
rootCmd.AddCommand(app.StartCmd)
|
||||
}
|
||||
|
||||
//Execute : apply commands
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
63
cmd/config/server.go
Normal file
63
cmd/config/server.go
Normal file
@ -0,0 +1,63 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/config/source/file"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/sdk/config"
|
||||
)
|
||||
|
||||
var (
|
||||
configYml string
|
||||
StartCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "Get Application config info",
|
||||
Example: "go-admin config -c config/settings.yml",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
run()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file")
|
||||
}
|
||||
|
||||
func run() {
|
||||
config.Setup(file.NewSource(file.WithPath(configYml)))
|
||||
|
||||
application, errs := json.MarshalIndent(config.ApplicationConfig, "", " ") //转换成JSON返回的是byte[]
|
||||
if errs != nil {
|
||||
fmt.Println(errs.Error())
|
||||
}
|
||||
fmt.Println("application:", string(application))
|
||||
|
||||
jwt, errs := json.MarshalIndent(config.JwtConfig, "", " ") //转换成JSON返回的是byte[]
|
||||
if errs != nil {
|
||||
fmt.Println(errs.Error())
|
||||
}
|
||||
fmt.Println("jwt:", string(jwt))
|
||||
|
||||
// todo 需要兼容
|
||||
database, errs := json.MarshalIndent(config.DatabasesConfig, "", " ") //转换成JSON返回的是byte[]
|
||||
if errs != nil {
|
||||
fmt.Println(errs.Error())
|
||||
}
|
||||
fmt.Println("database:", string(database))
|
||||
|
||||
gen, errs := json.MarshalIndent(config.GenConfig, "", " ") //转换成JSON返回的是byte[]
|
||||
if errs != nil {
|
||||
fmt.Println(errs.Error())
|
||||
}
|
||||
fmt.Println("gen:", string(gen))
|
||||
|
||||
loggerConfig, errs := json.MarshalIndent(config.LoggerConfig, "", " ") //转换成JSON返回的是byte[]
|
||||
if errs != nil {
|
||||
fmt.Println(errs.Error())
|
||||
}
|
||||
fmt.Println("logger:", string(loggerConfig))
|
||||
|
||||
}
|
||||
66
cmd/migrate/migration/init.go
Normal file
66
cmd/migrate/migration/init.go
Normal file
@ -0,0 +1,66 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"log"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var Migrate = &Migration{
|
||||
version: make(map[string]func(db *gorm.DB, version string) error),
|
||||
}
|
||||
|
||||
type Migration struct {
|
||||
db *gorm.DB
|
||||
version map[string]func(db *gorm.DB, version string) error
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
func (e *Migration) GetDb() *gorm.DB {
|
||||
return e.db
|
||||
}
|
||||
|
||||
func (e *Migration) SetDb(db *gorm.DB) {
|
||||
e.db = db
|
||||
}
|
||||
|
||||
func (e *Migration) SetVersion(k string, f func(db *gorm.DB, version string) error) {
|
||||
e.mutex.Lock()
|
||||
defer e.mutex.Unlock()
|
||||
e.version[k] = f
|
||||
}
|
||||
|
||||
func (e *Migration) Migrate() {
|
||||
versions := make([]string, 0)
|
||||
for k := range e.version {
|
||||
versions = append(versions, k)
|
||||
}
|
||||
if !sort.StringsAreSorted(versions) {
|
||||
sort.Strings(versions)
|
||||
}
|
||||
var err error
|
||||
var count int64
|
||||
for _, v := range versions {
|
||||
err = e.db.Table("sys_migration").Where("version = ?", v).Count(&count).Error
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
if count > 0 {
|
||||
log.Println(count)
|
||||
count = 0
|
||||
continue
|
||||
}
|
||||
err = (e.version[v])(e.db.Debug(), v)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetFilename(s string) string {
|
||||
s = filepath.Base(s)
|
||||
return s[:13]
|
||||
}
|
||||
22
cmd/migrate/migration/models/by.go
Normal file
22
cmd/migrate/migration/models/by.go
Normal file
@ -0,0 +1,22 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ControlBy struct {
|
||||
CreateBy int `json:"createBy" gorm:"index;comment:创建者"`
|
||||
UpdateBy int `json:"updateBy" gorm:"index;comment:更新者"`
|
||||
}
|
||||
|
||||
type Model struct {
|
||||
Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"`
|
||||
}
|
||||
|
||||
type ModelTime struct {
|
||||
CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"`
|
||||
UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"`
|
||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"`
|
||||
}
|
||||
17
cmd/migrate/migration/models/casbin_rule.go
Normal file
17
cmd/migrate/migration/models/casbin_rule.go
Normal file
@ -0,0 +1,17 @@
|
||||
package models
|
||||
|
||||
// CasbinRule sys_casbin_rule
|
||||
type CasbinRule struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"`
|
||||
Ptype string `gorm:"size:512;uniqueIndex:unique_index"`
|
||||
V0 string `gorm:"size:512;uniqueIndex:unique_index"`
|
||||
V1 string `gorm:"size:512;uniqueIndex:unique_index"`
|
||||
V2 string `gorm:"size:512;uniqueIndex:unique_index"`
|
||||
V3 string `gorm:"size:512;uniqueIndex:unique_index"`
|
||||
V4 string `gorm:"size:512;uniqueIndex:unique_index"`
|
||||
V5 string `gorm:"size:512;uniqueIndex:unique_index"`
|
||||
}
|
||||
|
||||
func (CasbinRule) TableName() string {
|
||||
return "sys_casbin_rule"
|
||||
}
|
||||
72
cmd/migrate/migration/models/initdb.go
Normal file
72
cmd/migrate/migration/models/initdb.go
Normal file
@ -0,0 +1,72 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-admin/common/global"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func InitDb(db *gorm.DB) (err error) {
|
||||
filePath := "config/db.sql"
|
||||
if global.Driver == "postgres" {
|
||||
filePath := "config/db.sql"
|
||||
if err = ExecSql(db, filePath); err != nil {
|
||||
return err
|
||||
}
|
||||
filePath = "config/pg.sql"
|
||||
err = ExecSql(db, filePath)
|
||||
} else if global.Driver == "mysql" {
|
||||
filePath = "config/db-begin-mysql.sql"
|
||||
if err = ExecSql(db, filePath); err != nil {
|
||||
return err
|
||||
}
|
||||
filePath = "config/db.sql"
|
||||
if err = ExecSql(db, filePath); err != nil {
|
||||
return err
|
||||
}
|
||||
filePath = "config/db-end-mysql.sql"
|
||||
err = ExecSql(db, filePath)
|
||||
} else {
|
||||
err = ExecSql(db, filePath)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func ExecSql(db *gorm.DB, filePath string) error {
|
||||
sql, err := Ioutil(filePath)
|
||||
if err != nil {
|
||||
fmt.Println("数据库基础数据初始化脚本读取失败!原因:", err.Error())
|
||||
return err
|
||||
}
|
||||
sqlList := strings.Split(sql, ";")
|
||||
for i := 0; i < len(sqlList)-1; i++ {
|
||||
if strings.Contains(sqlList[i], "--") {
|
||||
fmt.Println(sqlList[i])
|
||||
continue
|
||||
}
|
||||
sql := strings.Replace(sqlList[i]+";", "\n", "", -1)
|
||||
sql = strings.TrimSpace(sql)
|
||||
if err = db.Exec(sql).Error; err != nil {
|
||||
log.Printf("error sql: %s", sql)
|
||||
if !strings.Contains(err.Error(), "Query was empty") {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Ioutil(filePath string) (string, error) {
|
||||
if contents, err := ioutil.ReadFile(filePath); err == nil {
|
||||
//因为contents是[]byte类型,直接转换成string类型后会多一行空格,需要使用strings.Replace替换换行符
|
||||
result := strings.Replace(string(contents), "\n", "", 1)
|
||||
fmt.Println("Use ioutil.ReadFile to read a file:", result)
|
||||
return result, nil
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
11
cmd/migrate/migration/models/model.go
Normal file
11
cmd/migrate/migration/models/model.go
Normal file
@ -0,0 +1,11 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type BaseModel struct {
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt *time.Time `json:"deletedAt"`
|
||||
}
|
||||
10
cmd/migrate/migration/models/role_dept.go
Normal file
10
cmd/migrate/migration/models/role_dept.go
Normal file
@ -0,0 +1,10 @@
|
||||
package models
|
||||
|
||||
type SysRoleDept struct {
|
||||
RoleId int `gorm:"size:11;primaryKey"`
|
||||
DeptId int `gorm:"size:11;primaryKey"`
|
||||
}
|
||||
|
||||
func (SysRoleDept) TableName() string {
|
||||
return "sys_role_dept"
|
||||
}
|
||||
16
cmd/migrate/migration/models/sys_api.go
Normal file
16
cmd/migrate/migration/models/sys_api.go
Normal file
@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
type SysApi struct {
|
||||
Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"`
|
||||
Handle string `json:"handle" gorm:"size:128;comment:handle"`
|
||||
Title string `json:"title" gorm:"size:128;comment:标题"`
|
||||
Path string `json:"path" gorm:"size:128;comment:地址"`
|
||||
Type string `json:"type" gorm:"size:16;comment:接口类型"`
|
||||
Action string `json:"action" gorm:"size:16;comment:请求类型"`
|
||||
ModelTime
|
||||
ControlBy
|
||||
}
|
||||
|
||||
func (SysApi) TableName() string {
|
||||
return "sys_api"
|
||||
}
|
||||
44
cmd/migrate/migration/models/sys_columns.go
Normal file
44
cmd/migrate/migration/models/sys_columns.go
Normal file
@ -0,0 +1,44 @@
|
||||
package models
|
||||
|
||||
type SysColumns struct {
|
||||
ColumnId int `gorm:"primaryKey;autoIncrement" json:"columnId"`
|
||||
TableId int `gorm:"" json:"tableId"`
|
||||
ColumnName string `gorm:"size:128;" json:"columnName"`
|
||||
ColumnComment string `gorm:"column:column_comment;size:128;" json:"columnComment"`
|
||||
ColumnType string `gorm:"column:column_type;size:128;" json:"columnType"`
|
||||
GoType string `gorm:"column:go_type;size:128;" json:"goType"`
|
||||
GoField string `gorm:"column:go_field;size:128;" json:"goField"`
|
||||
JsonField string `gorm:"column:json_field;size:128;" json:"jsonField"`
|
||||
IsPk string `gorm:"column:is_pk;size:4;" json:"isPk"`
|
||||
IsIncrement string `gorm:"column:is_increment;size:4;" json:"isIncrement"`
|
||||
IsRequired string `gorm:"column:is_required;size:4;" json:"isRequired"`
|
||||
IsInsert string `gorm:"column:is_insert;size:4;" json:"isInsert"`
|
||||
IsEdit string `gorm:"column:is_edit;size:4;" json:"isEdit"`
|
||||
IsList string `gorm:"column:is_list;size:4;" json:"isList"`
|
||||
IsQuery string `gorm:"column:is_query;size:4;" json:"isQuery"`
|
||||
QueryType string `gorm:"column:query_type;size:128;" json:"queryType"`
|
||||
HtmlType string `gorm:"column:html_type;size:128;" json:"htmlType"`
|
||||
DictType string `gorm:"column:dict_type;size:128;" json:"dictType"`
|
||||
Sort int `gorm:"column:sort;" json:"sort"`
|
||||
List string `gorm:"column:list;size:1;" json:"list"`
|
||||
Pk bool `gorm:"column:pk;size:1;" json:"pk"`
|
||||
Required bool `gorm:"column:required;size:1;" json:"required"`
|
||||
SuperColumn bool `gorm:"column:super_column;size:1;" json:"superColumn"`
|
||||
UsableColumn bool `gorm:"column:usable_column;size:1;" json:"usableColumn"`
|
||||
Increment bool `gorm:"column:increment;size:1;" json:"increment"`
|
||||
Insert bool `gorm:"column:insert;size:1;" json:"insert"`
|
||||
Edit bool `gorm:"column:edit;size:1;" json:"edit"`
|
||||
Query bool `gorm:"column:query;size:1;" json:"query"`
|
||||
Remark string `gorm:"column:remark;size:255;" json:"remark"`
|
||||
FkTableName string `gorm:"" json:"fkTableName"`
|
||||
FkTableNameClass string `gorm:"" json:"fkTableNameClass"`
|
||||
FkTableNamePackage string `gorm:"" json:"fkTableNamePackage"`
|
||||
FkLabelId string `gorm:"" json:"fkLabelId"`
|
||||
FkLabelName string `gorm:"size:255;" json:"fkLabelName"`
|
||||
ModelTime
|
||||
ControlBy
|
||||
}
|
||||
|
||||
func (SysColumns) TableName() string {
|
||||
return "sys_columns"
|
||||
}
|
||||
17
cmd/migrate/migration/models/sys_config.go
Normal file
17
cmd/migrate/migration/models/sys_config.go
Normal file
@ -0,0 +1,17 @@
|
||||
package models
|
||||
|
||||
type SysConfig struct {
|
||||
Model
|
||||
ConfigName string `json:"configName" gorm:"type:varchar(128);comment:ConfigName"`
|
||||
ConfigKey string `json:"configKey" gorm:"type:varchar(128);comment:ConfigKey"`
|
||||
ConfigValue string `json:"configValue" gorm:"type:varchar(255);comment:ConfigValue"`
|
||||
ConfigType string `json:"configType" gorm:"type:varchar(64);comment:ConfigType"`
|
||||
IsFrontend int `json:"isFrontend" gorm:"type:varchar(64);comment:是否前台"`
|
||||
Remark string `json:"remark" gorm:"type:varchar(128);comment:Remark"`
|
||||
ControlBy
|
||||
ModelTime
|
||||
}
|
||||
|
||||
func (SysConfig) TableName() string {
|
||||
return "sys_config"
|
||||
}
|
||||
19
cmd/migrate/migration/models/sys_dept.go
Normal file
19
cmd/migrate/migration/models/sys_dept.go
Normal file
@ -0,0 +1,19 @@
|
||||
package models
|
||||
|
||||
type SysDept struct {
|
||||
DeptId int `json:"deptId" gorm:"primaryKey;autoIncrement;"` //部门编码
|
||||
ParentId int `json:"parentId" gorm:""` //上级部门
|
||||
DeptPath string `json:"deptPath" gorm:"size:255;"` //
|
||||
DeptName string `json:"deptName" gorm:"size:128;"` //部门名称
|
||||
Sort int `json:"sort" gorm:"size:4;"` //排序
|
||||
Leader string `json:"leader" gorm:"size:128;"` //负责人
|
||||
Phone string `json:"phone" gorm:"size:11;"` //手机
|
||||
Email string `json:"email" gorm:"size:64;"` //邮箱
|
||||
Status int `json:"status" gorm:"size:4;"` //状态
|
||||
ControlBy
|
||||
ModelTime
|
||||
}
|
||||
|
||||
func (SysDept) TableName() string {
|
||||
return "sys_dept"
|
||||
}
|
||||
21
cmd/migrate/migration/models/sys_dict_data.go
Normal file
21
cmd/migrate/migration/models/sys_dict_data.go
Normal file
@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
type DictData struct {
|
||||
DictCode int `gorm:"primaryKey;autoIncrement;" json:"dictCode" example:"1"` //字典编码
|
||||
DictSort int `gorm:"" json:"dictSort"` //显示顺序
|
||||
DictLabel string `gorm:"size:128;" json:"dictLabel"` //数据标签
|
||||
DictValue string `gorm:"size:255;" json:"dictValue"` //数据键值
|
||||
DictType string `gorm:"size:64;" json:"dictType"` //字典类型
|
||||
CssClass string `gorm:"size:128;" json:"cssClass"` //
|
||||
ListClass string `gorm:"size:128;" json:"listClass"` //
|
||||
IsDefault string `gorm:"size:8;" json:"isDefault"` //
|
||||
Status int `gorm:"size:4;" json:"status"` //状态
|
||||
Default string `gorm:"size:8;" json:"default"` //
|
||||
Remark string `gorm:"size:255;" json:"remark"` //备注
|
||||
ControlBy
|
||||
ModelTime
|
||||
}
|
||||
|
||||
func (DictData) TableName() string {
|
||||
return "sys_dict_data"
|
||||
}
|
||||
15
cmd/migrate/migration/models/sys_dict_type.go
Normal file
15
cmd/migrate/migration/models/sys_dict_type.go
Normal file
@ -0,0 +1,15 @@
|
||||
package models
|
||||
|
||||
type DictType struct {
|
||||
DictId int `gorm:"primaryKey;autoIncrement;" json:"dictId"`
|
||||
DictName string `gorm:"size:128;" json:"dictName"` //字典名称
|
||||
DictType string `gorm:"size:128;" json:"dictType"` //字典类型
|
||||
Status int `gorm:"size:4;" json:"status"` //状态
|
||||
Remark string `gorm:"size:255;" json:"remark"` //备注
|
||||
ControlBy
|
||||
ModelTime
|
||||
}
|
||||
|
||||
func (DictType) TableName() string {
|
||||
return "sys_dict_type"
|
||||
}
|
||||
21
cmd/migrate/migration/models/sys_job.go
Normal file
21
cmd/migrate/migration/models/sys_job.go
Normal file
@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
type SysJob struct {
|
||||
JobId int `json:"jobId" gorm:"primaryKey;autoIncrement"` // 编码
|
||||
JobName string `json:"jobName" gorm:"size:255;"` // 名称
|
||||
JobGroup string `json:"jobGroup" gorm:"size:255;"` // 任务分组
|
||||
JobType int `json:"jobType" gorm:"size:1;"` // 任务类型
|
||||
CronExpression string `json:"cronExpression" gorm:"size:255;"` // cron表达式
|
||||
InvokeTarget string `json:"invokeTarget" gorm:"size:255;"` // 调用目标
|
||||
Args string `json:"args" gorm:"size:255;"` // 目标参数
|
||||
MisfirePolicy int `json:"misfirePolicy" gorm:"size:255;"` // 执行策略
|
||||
Concurrent int `json:"concurrent" gorm:"size:1;"` // 是否并发
|
||||
Status int `json:"status" gorm:"size:1;"` // 状态
|
||||
EntryId int `json:"entry_id" gorm:"size:11;"` // job启动时返回的id
|
||||
ModelTime
|
||||
ControlBy
|
||||
}
|
||||
|
||||
func (SysJob) TableName() string {
|
||||
return "sys_job"
|
||||
}
|
||||
26
cmd/migrate/migration/models/sys_login_log.go
Normal file
26
cmd/migrate/migration/models/sys_login_log.go
Normal file
@ -0,0 +1,26 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type SysLoginLog struct {
|
||||
Model
|
||||
Username string `json:"username" gorm:"type:varchar(128);comment:用户名"`
|
||||
Status string `json:"status" gorm:"type:varchar(4);comment:状态"`
|
||||
Ipaddr string `json:"ipaddr" gorm:"type:varchar(255);comment:ip地址"`
|
||||
LoginLocation string `json:"loginLocation" gorm:"type:varchar(255);comment:归属地"`
|
||||
Browser string `json:"browser" gorm:"type:varchar(255);comment:浏览器"`
|
||||
Os string `json:"os" gorm:"type:varchar(255);comment:系统"`
|
||||
Platform string `json:"platform" gorm:"type:varchar(255);comment:固件"`
|
||||
LoginTime time.Time `json:"loginTime" gorm:"type:timestamp;comment:登录时间"`
|
||||
Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"`
|
||||
Msg string `json:"msg" gorm:"type:varchar(255);comment:信息"`
|
||||
CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"`
|
||||
UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"`
|
||||
ControlBy
|
||||
}
|
||||
|
||||
func (SysLoginLog) TableName() string {
|
||||
return "sys_login_log"
|
||||
}
|
||||
27
cmd/migrate/migration/models/sys_menu.go
Normal file
27
cmd/migrate/migration/models/sys_menu.go
Normal file
@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
type SysMenu struct {
|
||||
MenuId int `json:"menuId" gorm:"primaryKey;autoIncrement"`
|
||||
MenuName string `json:"menuName" gorm:"size:128;"`
|
||||
Title string `json:"title" gorm:"size:128;"`
|
||||
Icon string `json:"icon" gorm:"size:128;"`
|
||||
Path string `json:"path" gorm:"size:128;"`
|
||||
Paths string `json:"paths" gorm:"size:128;"`
|
||||
MenuType string `json:"menuType" gorm:"size:1;"`
|
||||
Action string `json:"action" gorm:"size:16;"`
|
||||
Permission string `json:"permission" gorm:"size:255;"`
|
||||
ParentId int `json:"parentId" gorm:"size:11;"`
|
||||
NoCache bool `json:"noCache" gorm:"size:8;"`
|
||||
Breadcrumb string `json:"breadcrumb" gorm:"size:255;"`
|
||||
Component string `json:"component" gorm:"size:255;"`
|
||||
Sort int `json:"sort" gorm:"size:4;"`
|
||||
Visible string `json:"visible" gorm:"size:1;"`
|
||||
IsFrame string `json:"isFrame" gorm:"size:1;DEFAULT:0;"`
|
||||
SysApi []SysApi `json:"sysApi" gorm:"many2many:sys_menu_api_rule"`
|
||||
ControlBy
|
||||
ModelTime
|
||||
}
|
||||
|
||||
func (SysMenu) TableName() string {
|
||||
return "sys_menu"
|
||||
}
|
||||
34
cmd/migrate/migration/models/sys_opera_log.go
Normal file
34
cmd/migrate/migration/models/sys_opera_log.go
Normal file
@ -0,0 +1,34 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type SysOperaLog struct {
|
||||
Model
|
||||
Title string `json:"title" gorm:"type:varchar(255);comment:操作模块"`
|
||||
BusinessType string `json:"businessType" gorm:"type:varchar(128);comment:操作类型"`
|
||||
BusinessTypes string `json:"businessTypes" gorm:"type:varchar(128);comment:BusinessTypes"`
|
||||
Method string `json:"method" gorm:"type:varchar(128);comment:函数"`
|
||||
RequestMethod string `json:"requestMethod" gorm:"type:varchar(128);comment:请求方式: GET POST PUT DELETE"`
|
||||
OperatorType string `json:"operatorType" gorm:"type:varchar(128);comment:操作类型"`
|
||||
OperName string `json:"operName" gorm:"type:varchar(128);comment:操作者"`
|
||||
DeptName string `json:"deptName" gorm:"type:varchar(128);comment:部门名称"`
|
||||
OperUrl string `json:"operUrl" gorm:"type:varchar(255);comment:访问地址"`
|
||||
OperIp string `json:"operIp" gorm:"type:varchar(128);comment:客户端ip"`
|
||||
OperLocation string `json:"operLocation" gorm:"type:varchar(128);comment:访问位置"`
|
||||
OperParam string `json:"operParam" gorm:"type:text;comment:请求参数"`
|
||||
Status string `json:"status" gorm:"type:varchar(4);comment:操作状态 1:正常 2:关闭"`
|
||||
OperTime time.Time `json:"operTime" gorm:"type:timestamp;comment:操作时间"`
|
||||
JsonResult string `json:"jsonResult" gorm:"type:varchar(255);comment:返回数据"`
|
||||
Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"`
|
||||
LatencyTime string `json:"latencyTime" gorm:"type:varchar(128);comment:耗时"`
|
||||
UserAgent string `json:"userAgent" gorm:"type:varchar(255);comment:ua"`
|
||||
CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"`
|
||||
UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"`
|
||||
ControlBy
|
||||
}
|
||||
|
||||
func (SysOperaLog) TableName() string {
|
||||
return "sys_opera_log"
|
||||
}
|
||||
16
cmd/migrate/migration/models/sys_post.go
Normal file
16
cmd/migrate/migration/models/sys_post.go
Normal file
@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
type SysPost struct {
|
||||
PostId int `gorm:"primaryKey;autoIncrement" json:"postId"` //岗位编号
|
||||
PostName string `gorm:"size:128;" json:"postName"` //岗位名称
|
||||
PostCode string `gorm:"size:128;" json:"postCode"` //岗位代码
|
||||
Sort int `gorm:"size:4;" json:"sort"` //岗位排序
|
||||
Status int `gorm:"size:4;" json:"status"` //状态
|
||||
Remark string `gorm:"size:255;" json:"remark"` //描述
|
||||
ControlBy
|
||||
ModelTime
|
||||
}
|
||||
|
||||
func (SysPost) TableName() string {
|
||||
return "sys_post"
|
||||
}
|
||||
20
cmd/migrate/migration/models/sys_role.go
Normal file
20
cmd/migrate/migration/models/sys_role.go
Normal file
@ -0,0 +1,20 @@
|
||||
package models
|
||||
|
||||
type SysRole struct {
|
||||
RoleId int `json:"roleId" gorm:"primaryKey;autoIncrement"` // 角色编码
|
||||
RoleName string `json:"roleName" gorm:"size:128;"` // 角色名称
|
||||
Status string `json:"status" gorm:"size:4;"` //
|
||||
RoleKey string `json:"roleKey" gorm:"size:128;"` //角色代码
|
||||
RoleSort int `json:"roleSort" gorm:""` //角色排序
|
||||
Flag string `json:"flag" gorm:"size:128;"` //
|
||||
Remark string `json:"remark" gorm:"size:255;"` //备注
|
||||
Admin bool `json:"admin" gorm:"size:4;"`
|
||||
DataScope string `json:"dataScope" gorm:"size:128;"`
|
||||
SysMenu []SysMenu `json:"sysMenu" gorm:"many2many:sys_role_menu;foreignKey:RoleId;joinForeignKey:role_id;references:MenuId;joinReferences:menu_id;"`
|
||||
ControlBy
|
||||
ModelTime
|
||||
}
|
||||
|
||||
func (SysRole) TableName() string {
|
||||
return "sys_role"
|
||||
}
|
||||
37
cmd/migrate/migration/models/sys_tables.go
Normal file
37
cmd/migrate/migration/models/sys_tables.go
Normal file
@ -0,0 +1,37 @@
|
||||
package models
|
||||
|
||||
type SysTables struct {
|
||||
TableId int `gorm:"primaryKey;autoIncrement" json:"tableId"` //表编码
|
||||
TBName string `gorm:"column:table_name;size:255;" json:"tableName"` //表名称
|
||||
TableComment string `gorm:"size:255;" json:"tableComment"` //表备注
|
||||
ClassName string `gorm:"size:255;" json:"className"` //类名
|
||||
TplCategory string `gorm:"size:255;" json:"tplCategory"` //
|
||||
PackageName string `gorm:"size:255;" json:"packageName"` //包名
|
||||
ModuleName string `gorm:"size:255;" json:"moduleName"` //go文件名
|
||||
ModuleFrontName string `gorm:"size:255;comment:前端文件名;" json:"moduleFrontName"` //前端文件名
|
||||
BusinessName string `gorm:"size:255;" json:"businessName"` //
|
||||
FunctionName string `gorm:"size:255;" json:"functionName"` //功能名称
|
||||
FunctionAuthor string `gorm:"size:255;" json:"functionAuthor"` //功能作者
|
||||
PkColumn string `gorm:"size:255;" json:"pkColumn"`
|
||||
PkGoField string `gorm:"size:255;" json:"pkGoField"`
|
||||
PkJsonField string `gorm:"size:255;" json:"pkJsonField"`
|
||||
Options string `gorm:"size:255;" json:"options"`
|
||||
TreeCode string `gorm:"size:255;" json:"treeCode"`
|
||||
TreeParentCode string `gorm:"size:255;" json:"treeParentCode"`
|
||||
TreeName string `gorm:"size:255;" json:"treeName"`
|
||||
Tree bool `gorm:"size:1;default:0;" json:"tree"`
|
||||
Crud bool `gorm:"size:1;default:1;" json:"crud"`
|
||||
Remark string `gorm:"size:255;" json:"remark"`
|
||||
IsDataScope int `gorm:"size:1;" json:"isDataScope"`
|
||||
IsActions int `gorm:"size:1;" json:"isActions"`
|
||||
IsAuth int `gorm:"size:1;" json:"isAuth"`
|
||||
IsLogicalDelete string `gorm:"size:1;" json:"isLogicalDelete"`
|
||||
LogicalDelete bool `gorm:"size:1;" json:"logicalDelete"`
|
||||
LogicalDeleteColumn string `gorm:"size:128;" json:"logicalDeleteColumn"`
|
||||
ModelTime
|
||||
ControlBy
|
||||
}
|
||||
|
||||
func (SysTables) TableName() string {
|
||||
return "sys_tables"
|
||||
}
|
||||
48
cmd/migrate/migration/models/sys_user.go
Normal file
48
cmd/migrate/migration/models/sys_user.go
Normal file
@ -0,0 +1,48 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SysUser struct {
|
||||
UserId int `gorm:"primaryKey;autoIncrement;comment:编码" json:"userId"`
|
||||
Username string `json:"username" gorm:"type:varchar(64);comment:用户名"`
|
||||
Password string `json:"-" gorm:"type:varchar(128);comment:密码"`
|
||||
NickName string `json:"nickName" gorm:"type:varchar(128);comment:昵称"`
|
||||
Phone string `json:"phone" gorm:"type:varchar(11);comment:手机号"`
|
||||
RoleId int `json:"roleId" gorm:"type:bigint;comment:角色ID"`
|
||||
Salt string `json:"-" gorm:"type:varchar(255);comment:加盐"`
|
||||
Avatar string `json:"avatar" gorm:"type:varchar(255);comment:头像"`
|
||||
Sex string `json:"sex" gorm:"type:varchar(255);comment:性别"`
|
||||
Email string `json:"email" gorm:"type:varchar(128);comment:邮箱"`
|
||||
DeptId int `json:"deptId" gorm:"type:bigint;comment:部门"`
|
||||
PostId int `json:"postId" gorm:"type:bigint;comment:岗位"`
|
||||
Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"`
|
||||
Status string `json:"status" gorm:"type:varchar(4);comment:状态"`
|
||||
ControlBy
|
||||
ModelTime
|
||||
}
|
||||
|
||||
func (*SysUser) TableName() string {
|
||||
return "sys_user"
|
||||
}
|
||||
|
||||
// Encrypt 加密
|
||||
func (e *SysUser) Encrypt() (err error) {
|
||||
if e.Password == "" {
|
||||
return
|
||||
}
|
||||
|
||||
var hash []byte
|
||||
if hash, err = bcrypt.GenerateFromPassword([]byte(e.Password), bcrypt.DefaultCost); err != nil {
|
||||
return
|
||||
} else {
|
||||
e.Password = string(hash)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *SysUser) BeforeCreate(_ *gorm.DB) error {
|
||||
return e.Encrypt()
|
||||
}
|
||||
12
cmd/migrate/migration/models/tb_demo.go
Normal file
12
cmd/migrate/migration/models/tb_demo.go
Normal file
@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
type TbDemo struct {
|
||||
Model
|
||||
Name string `json:"name" gorm:"type:varchar(128);comment:名称"`
|
||||
ModelTime
|
||||
ControlBy
|
||||
}
|
||||
|
||||
func (TbDemo) TableName() string {
|
||||
return "tb_demo"
|
||||
}
|
||||
8
cmd/migrate/migration/version-local/doc.go
Normal file
8
cmd/migrate/migration/version-local/doc.go
Normal file
@ -0,0 +1,8 @@
|
||||
package version_local
|
||||
|
||||
func init() {
|
||||
}
|
||||
|
||||
/**
|
||||
开发者项目的迁移脚本放在这个目录里,init写法参考version目录里的migrate或者自动生成
|
||||
*/
|
||||
53
cmd/migrate/migration/version/1599190683659_tables.go
Normal file
53
cmd/migrate/migration/version/1599190683659_tables.go
Normal file
@ -0,0 +1,53 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"github.com/go-admin-team/go-admin-core/sdk/config"
|
||||
"runtime"
|
||||
|
||||
"go-admin/cmd/migrate/migration"
|
||||
"go-admin/cmd/migrate/migration/models"
|
||||
common "go-admin/common/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
_, fileName, _, _ := runtime.Caller(0)
|
||||
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1599190683659Tables)
|
||||
}
|
||||
|
||||
func _1599190683659Tables(db *gorm.DB, version string) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
if config.DatabaseConfig.Driver == "mysql" {
|
||||
tx = tx.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4")
|
||||
}
|
||||
err := tx.Migrator().AutoMigrate(
|
||||
new(models.SysDept),
|
||||
new(models.SysConfig),
|
||||
new(models.SysTables),
|
||||
new(models.SysColumns),
|
||||
new(models.SysMenu),
|
||||
new(models.SysLoginLog),
|
||||
new(models.SysOperaLog),
|
||||
new(models.SysRoleDept),
|
||||
new(models.SysUser),
|
||||
new(models.SysRole),
|
||||
new(models.SysPost),
|
||||
new(models.DictData),
|
||||
new(models.DictType),
|
||||
new(models.SysJob),
|
||||
new(models.SysConfig),
|
||||
new(models.SysApi),
|
||||
new(models.TbDemo),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := models.InitDb(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Create(&common.Migration{
|
||||
Version: version,
|
||||
}).Error
|
||||
})
|
||||
}
|
||||
48
cmd/migrate/migration/version/1653638869132_migrate.go
Normal file
48
cmd/migrate/migration/version/1653638869132_migrate.go
Normal file
@ -0,0 +1,48 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"go-admin/cmd/migrate/migration/models"
|
||||
common "go-admin/common/models"
|
||||
"gorm.io/gorm"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
"go-admin/cmd/migrate/migration"
|
||||
)
|
||||
|
||||
func init() {
|
||||
_, fileName, _, _ := runtime.Caller(0)
|
||||
migration.Migrate.SetVersion(migration.GetFilename(fileName), _1653638869132Test)
|
||||
}
|
||||
|
||||
func _1653638869132Test(db *gorm.DB, version string) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
var list []models.SysMenu
|
||||
err := tx.Model(&models.SysMenu{}).Order("parent_id,menu_id").Find(&list).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range list {
|
||||
if v.ParentId == 0 {
|
||||
v.Paths = "/0/" + strconv.Itoa(v.MenuId)
|
||||
} else {
|
||||
var e models.SysMenu
|
||||
err = tx.Model(&models.SysMenu{}).Where("menu_id=?", v.ParentId).First(&e).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
v.Paths = e.Paths + "/" + strconv.Itoa(v.MenuId)
|
||||
}
|
||||
err = tx.Model(&v).Update("paths", v.Paths).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Create(&common.Migration{
|
||||
Version: version,
|
||||
}).Error
|
||||
})
|
||||
}
|
||||
106
cmd/migrate/server.go
Normal file
106
cmd/migrate/server.go
Normal file
@ -0,0 +1,106 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/go-admin-team/go-admin-core/sdk"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
"strconv"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/config/source/file"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/sdk/config"
|
||||
"go-admin/cmd/migrate/migration"
|
||||
_ "go-admin/cmd/migrate/migration/version"
|
||||
_ "go-admin/cmd/migrate/migration/version-local"
|
||||
"go-admin/common/database"
|
||||
"go-admin/common/models"
|
||||
)
|
||||
|
||||
var (
|
||||
configYml string
|
||||
generate bool
|
||||
goAdmin bool
|
||||
host string
|
||||
StartCmd = &cobra.Command{
|
||||
Use: "migrate",
|
||||
Short: "Initialize the database",
|
||||
Example: "go-admin migrate -c config/settings.yml",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
run()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// fixme 在您看不见代码的时候运行迁移,我觉得是不安全的,所以编译后最好不要去执行迁移
|
||||
func init() {
|
||||
StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file")
|
||||
StartCmd.PersistentFlags().BoolVarP(&generate, "generate", "g", false, "generate migration file")
|
||||
StartCmd.PersistentFlags().BoolVarP(&goAdmin, "goAdmin", "a", false, "generate go-admin migration file")
|
||||
StartCmd.PersistentFlags().StringVarP(&host, "domain", "d", "*", "select tenant host")
|
||||
}
|
||||
|
||||
func run() {
|
||||
|
||||
if !generate {
|
||||
fmt.Println(`start init`)
|
||||
//1. 读取配置
|
||||
config.Setup(
|
||||
file.NewSource(file.WithPath(configYml)),
|
||||
initDB,
|
||||
)
|
||||
} else {
|
||||
fmt.Println(`generate migration file`)
|
||||
_ = genFile()
|
||||
}
|
||||
}
|
||||
|
||||
func migrateModel() error {
|
||||
if host == "" {
|
||||
host = "*"
|
||||
}
|
||||
db := sdk.Runtime.GetDbByKey(host)
|
||||
if config.DatabasesConfig[host].Driver == "mysql" {
|
||||
//初始化数据库时候用
|
||||
db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4")
|
||||
}
|
||||
err := db.Debug().AutoMigrate(&models.Migration{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
migration.Migrate.SetDb(db.Debug())
|
||||
migration.Migrate.Migrate()
|
||||
return err
|
||||
}
|
||||
func initDB() {
|
||||
//3. 初始化数据库链接
|
||||
database.Setup()
|
||||
//4. 数据库迁移
|
||||
fmt.Println("数据库迁移开始")
|
||||
_ = migrateModel()
|
||||
fmt.Println(`数据库基础数据初始化成功`)
|
||||
}
|
||||
|
||||
func genFile() error {
|
||||
t1, err := template.ParseFiles("template/migrate.template")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m := map[string]string{}
|
||||
m["GenerateTime"] = strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
|
||||
m["Package"] = "version_local"
|
||||
if goAdmin {
|
||||
m["Package"] = "version"
|
||||
}
|
||||
var b1 bytes.Buffer
|
||||
err = t1.Execute(&b1, m)
|
||||
if goAdmin {
|
||||
pkg.FileCreate(b1, "./cmd/migrate/migration/version/"+m["GenerateTime"]+"_migrate.go")
|
||||
} else {
|
||||
pkg.FileCreate(b1, "./cmd/migrate/migration/version-local/"+m["GenerateTime"]+"_migrate.go")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
BIN
cmd/proxy-server
Normal file
BIN
cmd/proxy-server
Normal file
Binary file not shown.
26
cmd/version/server.go
Normal file
26
cmd/version/server.go
Normal file
@ -0,0 +1,26 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"go-admin/common/global"
|
||||
)
|
||||
|
||||
var (
|
||||
StartCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Get version info",
|
||||
Example: "go-admin version",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return run()
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func run() error {
|
||||
fmt.Println(global.Version)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user