检测mysql配置

pull/30/head
陶士涵 5 years ago
parent 7886dad432
commit 80e2e20ef9

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/database"
"github.com/taoshihan1991/imaptool/tools"
"os"
)
@ -22,6 +23,17 @@ func MysqlSetConf(c *gin.Context) {
mysqlDb:=c.PostForm("database")
mysqlUsername:=c.PostForm("username")
mysqlPassword:=c.PostForm("password")
dsn:=fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", mysqlUsername,mysqlPassword, mysqlServer, mysqlPort, mysqlDb)
mysql:=database.NewMysql()
mysql.Dsn=dsn
err:=mysql.Ping()
if err!=nil{
c.JSON(200, gin.H{
"code": 403,
"msg": "数据库连接失败:"+err.Error(),
})
return
}
isExist, _ := tools.IsFileExist(config.Dir)
if !isExist {
os.Mkdir(config.Dir, os.ModePerm)

@ -0,0 +1,25 @@
package database
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/taoshihan1991/imaptool/config"
)
type Mysql struct{
SqlDB *sql.DB
Dsn string
}
func NewMysql()*Mysql{
mysqlInfo:=config.GetMysql()
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", mysqlInfo["Username"], mysqlInfo["Password"], mysqlInfo["Server"], mysqlInfo["Port"], mysqlInfo["Database"])
return &Mysql{
Dsn:dsn,
}
}
func (db *Mysql)Ping()error{
sqlDb, _ := sql.Open("mysql", db.Dsn)
db.SqlDB=sqlDb
return db.SqlDB.Ping()
}

@ -11,6 +11,7 @@ require (
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
github.com/emersion/go-smtp v0.13.0
github.com/gin-gonic/gin v1.6.3
github.com/go-sql-driver/mysql v1.5.0
github.com/gobuffalo/packr/v2 v2.5.1
github.com/gorilla/websocket v1.4.2
golang.org/x/net v0.0.0-20200513185701-a91f0712d120

@ -37,6 +37,8 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gobuffalo/envy v1.7.0 h1:GlXgaiBkmrYMHco6t4j7SacKO4XUjvh5pwXh0f4uxXU=
github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI=
github.com/gobuffalo/logger v1.0.0 h1:xw9Ko9EcC5iAFprrjJ6oZco9UpzS5MQ4jAwghsLHdy4=

@ -65,11 +65,10 @@
let _this=this;
this.$refs[formName].validate((valid) => {
if (valid) {
let _this=this;
$.ajax({
type:"POST",
url:"/mysql",
data:this.mysql,
data:_this.mysql,
headers:{
"token":localStorage.getItem("token")
},

Loading…
Cancel
Save