diff --git a/cmd/install.go b/cmd/install.go index d06c586..caf5eee 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "github.com/spf13/cobra" "github.com/taoshihan1991/imaptool/common" "github.com/taoshihan1991/imaptool/models" @@ -21,11 +20,15 @@ var installCmd = &cobra.Command{ } func install() { + if ok, _ := tools.IsFileNotExist("./install.lock"); !ok { + log.Println("请先删除./install.lock") + os.Exit(1) + } sqlFile := common.Dir + "go-fly.sql" isExit, _ := tools.IsFileExist(common.MysqlConf) dataExit, _ := tools.IsFileExist(sqlFile) if !isExit || !dataExit { - fmt.Println("config/mysql.json 数据库配置文件或者数据库文件go-fly.sql不存在") + log.Println("config/mysql.json 数据库配置文件或者数据库文件go-fly.sql不存在") os.Exit(1) } sqls, _ := ioutil.ReadFile(sqlFile) @@ -42,4 +45,6 @@ func install() { os.Exit(1) } } + installFile, _ := os.OpenFile("./install.lock", os.O_RDWR|os.O_CREATE, os.ModePerm) + installFile.WriteString("gofly live chat") } diff --git a/config/mysql.json b/config/mysql.json index bfc2cdb..7d2415d 100644 --- a/config/mysql.json +++ b/config/mysql.json @@ -1,5 +1,5 @@ { - "Server":"127.0.0.1", + "Server":"localhost", "Port":"3306", "Database":"go-fly", "Username":"go-fly", diff --git a/controller/main.go b/controller/main.go index ef6fb76..2fe5a79 100644 --- a/controller/main.go +++ b/controller/main.go @@ -1,11 +1,100 @@ package controller import ( + "errors" + "fmt" "github.com/gin-gonic/gin" + "github.com/jinzhu/gorm" + "github.com/taoshihan1991/imaptool/common" "github.com/taoshihan1991/imaptool/models" + "github.com/taoshihan1991/imaptool/tools" "github.com/taoshihan1991/imaptool/ws" + "io/ioutil" + "log" + "os" + "strings" ) +func PostInstall(c *gin.Context) { + notExist, _ := tools.IsFileNotExist("./install.lock") + if !notExist { + c.JSON(200, gin.H{ + "code": 400, + "msg": "系统已经安装过了", + }) + return + } + server := c.PostForm("server") + port := c.PostForm("port") + database := c.PostForm("database") + username := c.PostForm("username") + password := c.PostForm("password") + dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, server, port, database) + _, err := gorm.Open("mysql", dsn) + if err != nil { + log.Println(err) + tools.Logger().Println(err) + c.JSON(200, gin.H{ + "code": 400, + "msg": "数据库连接失败:" + err.Error(), + }) + return + } + isExist, _ := tools.IsFileExist(common.Dir) + if !isExist { + os.Mkdir(common.Dir, os.ModePerm) + } + fileConfig := common.MysqlConf + file, _ := os.OpenFile(fileConfig, os.O_RDWR|os.O_CREATE, os.ModePerm) + + format := `{ + "Server":"%s", + "Port":"%s", + "Database":"%s", + "Username":"%s", + "Password":"%s" +} +` + data := fmt.Sprintf(format, server, port, database, username, password) + file.WriteString(data) + models.Connect() + installFile, _ := os.OpenFile("./install.lock", os.O_RDWR|os.O_CREATE, os.ModePerm) + installFile.WriteString("gofly live chat") + ok, err := install() + if !ok { + c.JSON(200, gin.H{ + "code": 200, + "msg": err.Error(), + }) + return + } + c.JSON(200, gin.H{ + "code": 200, + "msg": "安装成功", + }) +} +func install() (bool, error) { + sqlFile := common.Dir + "go-fly.sql" + isExit, _ := tools.IsFileExist(common.MysqlConf) + dataExit, _ := tools.IsFileExist(sqlFile) + if !isExit || !dataExit { + return false, errors.New("config/mysql.json 数据库配置文件或者数据库文件go-fly.sql不存在") + } + sqls, _ := ioutil.ReadFile(sqlFile) + sqlArr := strings.Split(string(sqls), "|") + for _, sql := range sqlArr { + if sql == "" { + continue + } + err := models.Execute(sql) + if err == nil { + log.Println(sql, "\t success!") + } else { + log.Println(sql, err, "\t failed!") + } + } + return true, nil +} func MainCheckAuth(c *gin.Context) { id, _ := c.Get("kefu_id") userinfo := models.FindUserRole("user.avator,user.name,user.id, role.name role_name", id) diff --git a/install.lock b/install.lock new file mode 100644 index 0000000..1253fc6 --- /dev/null +++ b/install.lock @@ -0,0 +1 @@ +gofly live chat \ No newline at end of file diff --git a/models/models.go b/models/models.go index 175ac8e..a5f8994 100644 --- a/models/models.go +++ b/models/models.go @@ -19,21 +19,26 @@ type Model struct { } func init() { + Connect() +} +func Connect() error { mysql := common.GetMysqlConf() dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysql.Username, mysql.Password, mysql.Server, mysql.Port, mysql.Database) var err error DB, err = gorm.Open("mysql", dsn) if err != nil { log.Println(err) - panic("数据库连接失败!") + tools.Logger().Println(err) + return err + //panic("数据库连接失败!") } DB.SingularTable(true) DB.LogMode(true) DB.SetLogger(tools.Logger()) DB.DB().SetMaxIdleConns(10) DB.DB().SetMaxOpenConns(100) - InitConfig() + return nil } func Execute(sql string) error { return DB.Exec(sql).Error diff --git a/router/api.go b/router/api.go index 113dc08..2b3d97f 100644 --- a/router/api.go +++ b/router/api.go @@ -25,6 +25,7 @@ func InitApiRouter(engine *gin.Engine) { engine.POST("/check_auth", middleware.JwtApiMiddleware, controller.MainCheckAuth) engine.GET("/userinfo", middleware.JwtApiMiddleware, controller.GetKefuInfoAll) engine.POST("/register", middleware.Ipblack, controller.PostKefuRegister) + engine.POST("/install", controller.PostInstall) //前后聊天 engine.GET("/ws_kefu", middleware.JwtApiMiddleware, ws.NewKefuServer) engine.GET("/ws_visitor", middleware.Ipblack, ws.NewVisitorServer) diff --git a/router/view.go b/router/view.go index 7ec8c7b..b9b54ce 100644 --- a/router/view.go +++ b/router/view.go @@ -8,6 +8,7 @@ import ( func InitViewRouter(engine *gin.Engine) { engine.GET("/index_:lang", middleware.SetLanguage, tmpl.PageIndex) + engine.GET("/install", tmpl.PageInstall) engine.GET("/detail_:page", middleware.SetLanguage, tmpl.PageDetail) engine.GET("/login", tmpl.PageLogin) engine.GET("/chat_page", middleware.SetLanguage, tmpl.PageChat) diff --git a/static/html/install.html b/static/html/install.html new file mode 100644 index 0000000..0c16be9 --- /dev/null +++ b/static/html/install.html @@ -0,0 +1,162 @@ + +
+ + + + + +陶士涵版权所有 © 2020-2021
+ +