123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package install
- import (
- "fmt"
- "net/http"
- "os"
- "runtime"
- "github.com/xinliangnote/go-gin-api/configs"
- "github.com/xinliangnote/go-gin-api/internal/code"
- "github.com/xinliangnote/go-gin-api/internal/pkg/core"
- "github.com/xinliangnote/go-gin-api/internal/proposal/tablesqls"
- "github.com/go-redis/redis/v7"
- "github.com/spf13/cast"
- "github.com/spf13/viper"
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
- "gorm.io/gorm/schema"
- )
- type initExecuteRequest struct {
- Language string `form:"language" ` // 语言包
- RedisAddr string `form:"redis_addr"` // 连接地址,例如:127.0.0.1:6379
- RedisPass string `form:"redis_pass"` // 连接密码
- RedisDb string `form:"redis_db"` // 连接 db
- MySQLAddr string `form:"mysql_addr"`
- MySQLUser string `form:"mysql_user"`
- MySQLPass string `form:"mysql_pass"`
- MySQLName string `form:"mysql_name"`
- }
- func (h *handler) Execute() core.HandlerFunc {
- installTableList := map[string]map[string]string{
- "authorized": {
- "table_sql": tablesqls.CreateAuthorizedTableSql(),
- "table_data_sql": tablesqls.CreateAuthorizedTableDataSql(),
- },
- "authorized_api": {
- "table_sql": tablesqls.CreateAuthorizedAPITableSql(),
- "table_data_sql": tablesqls.CreateAuthorizedAPITableDataSql(),
- },
- "admin": {
- "table_sql": tablesqls.CreateAdminTableSql(),
- "table_data_sql": tablesqls.CreateAdminTableDataSql(),
- },
- "admin_menu": {
- "table_sql": tablesqls.CreateAdminMenuTableSql(),
- "table_data_sql": tablesqls.CreateAdminMenuTableDataSql(),
- },
- "menu": {
- "table_sql": tablesqls.CreateMenuTableSql(),
- "table_data_sql": tablesqls.CreateMenuTableDataSql(),
- },
- "menu_action": {
- "table_sql": tablesqls.CreateMenuActionTableSql(),
- "table_data_sql": tablesqls.CreateMenuActionTableDataSql(),
- },
- "cron_task": {
- "table_sql": tablesqls.CreateCronTaskTableSql(),
- "table_data_sql": "",
- },
- }
- return func(ctx core.Context) {
- req := new(initExecuteRequest)
- if err := ctx.ShouldBindForm(req); err != nil {
- ctx.AbortWithError(core.Error(
- http.StatusBadRequest,
- code.ParamBindError,
- code.Text(code.ParamBindError)).WithError(err),
- )
- return
- }
- // region 验证 version
- versionStr := runtime.Version()
- version := cast.ToFloat32(versionStr[2:6])
- if version < configs.MinGoVersion {
- ctx.AbortWithError(core.Error(
- http.StatusBadRequest,
- code.GoVersionError,
- code.Text(code.GoVersionError)),
- )
- return
- }
- // endregion
- // region 验证 Redis 配置
- cfg := configs.Get()
- redisClient := redis.NewClient(&redis.Options{
- Addr: req.RedisAddr,
- Password: req.RedisPass,
- DB: cast.ToInt(req.RedisDb),
- MaxRetries: cfg.Redis.MaxRetries,
- PoolSize: cfg.Redis.PoolSize,
- MinIdleConns: cfg.Redis.MinIdleConns,
- })
- if err := redisClient.Ping().Err(); err != nil {
- ctx.AbortWithError(core.Error(
- http.StatusBadRequest,
- code.RedisConnectError,
- code.Text(code.RedisConnectError)).WithError(err),
- )
- return
- }
- defer redisClient.Close()
- outPutString := "已检测 Redis 配置可用。\n"
- // endregion
- // region 验证 MySQL 配置
- dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=%t&loc=%s",
- req.MySQLUser,
- req.MySQLPass,
- req.MySQLAddr,
- req.MySQLName,
- true,
- "Local")
- db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
- NamingStrategy: schema.NamingStrategy{
- SingularTable: true,
- },
- //Logger: logger.Default.LogMode(logger.Info), // 日志配置
- })
- if err != nil {
- ctx.AbortWithError(core.Error(
- http.StatusBadRequest,
- code.MySQLConnectError,
- code.Text(code.MySQLConnectError)).WithError(err),
- )
- return
- }
- db.Set("gorm:table_options", "CHARSET=utf8mb4")
- dbClient, _ := db.DB()
- defer dbClient.Close()
- outPutString += "已检测 MySQL 配置可用。\n"
- // endregion
- // region 写入配置文件
- viper.Set("language.local", req.Language)
- viper.Set("redis.addr", req.RedisAddr)
- viper.Set("redis.pass", req.RedisPass)
- viper.Set("redis.db", req.RedisDb)
- viper.Set("mysql.read.addr", req.MySQLAddr)
- viper.Set("mysql.read.user", req.MySQLUser)
- viper.Set("mysql.read.pass", req.MySQLPass)
- viper.Set("mysql.read.name", req.MySQLName)
- viper.Set("mysql.write.addr", req.MySQLAddr)
- viper.Set("mysql.write.user", req.MySQLUser)
- viper.Set("mysql.write.pass", req.MySQLPass)
- viper.Set("mysql.write.name", req.MySQLName)
- if viper.WriteConfig() != nil {
- ctx.AbortWithError(core.Error(
- http.StatusBadRequest,
- code.WriteConfigError,
- code.Text(code.WriteConfigError)).WithError(err),
- )
- return
- }
- outPutString += "语言包 " + req.Language + " 配置成功。\n"
- outPutString += "配置项 Redis、MySQL 配置成功。\n"
- // endregion
- // region 初始化表结构 + 默认数据
- for k, v := range installTableList {
- if v["table_sql"] != "" {
- // region 初始化表结构
- if err = db.Exec(v["table_sql"]).Error; err != nil {
- ctx.AbortWithError(core.Error(
- http.StatusBadRequest,
- code.MySQLExecError,
- code.Text(code.MySQLExecError)+" "+err.Error()).WithError(err),
- )
- return
- }
- outPutString += "初始化 MySQL 数据表:" + k + " 成功。\n"
- // endregion
- // region 初始化默认数据
- if v["table_data_sql"] != "" {
- if err = db.Exec(v["table_data_sql"]).Error; err != nil {
- ctx.AbortWithError(core.Error(
- http.StatusBadRequest,
- code.MySQLExecError,
- code.Text(code.MySQLExecError)+" "+err.Error()).WithError(err),
- )
- return
- }
- outPutString += "初始化 MySQL 数据表:" + k + " 默认数据成功。\n"
- }
- // endregion
- }
- }
- // endregion
- // region 生成 install 完成标识
- f, err := os.Create(configs.ProjectInstallMark)
- if err != nil {
- ctx.AbortWithError(core.Error(
- http.StatusBadRequest,
- code.MySQLExecError,
- code.Text(code.MySQLExecError)+" "+err.Error()).WithError(err),
- )
- return
- }
- defer f.Close()
- // endregion
- ctx.Payload(outPutString)
- }
- }
|