Browse Source

Messages optimisation, mysql configure action

Vova Tkach 6 years ago
parent
commit
6e581d85d8
2 changed files with 37 additions and 5 deletions
  1. 20 5
      engine/actions/action_mysql.go
  2. 17 0
      engine/actions/actions.go

+ 20 - 5
engine/actions/action_mysql.go

@@ -1,8 +1,11 @@
 package actions
 
 import (
+	"database/sql"
 	"fmt"
 
+	_ "github.com/go-sql-driver/mysql"
+
 	utils "golang-fave/engine/wrapper/utils"
 )
 
@@ -13,28 +16,40 @@ func action_mysql(e *Action) {
 	pf_password := e.R.FormValue("password")
 
 	if pf_host == "" {
-		e.write(fmt.Sprintf(`ModalShowMsg('Error', 'Please specify host for mysql connection');`))
+		e.msg_error(`Please specify host for mysql connection`)
 		return
 	}
 
 	if pf_name == "" {
-		e.write(fmt.Sprintf(`ModalShowMsg('Error', 'Please specify mysql database name');`))
+		e.msg_error(`Please specify mysql database name`)
 		return
 	}
 
 	if pf_user == "" {
-		e.write(fmt.Sprintf(`ModalShowMsg('Error', 'Please specify mysql user');`))
+		e.msg_error(`Please specify mysql user`)
 		return
 	}
 
 	// Try connect to mysql
+	db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":3306)/"+pf_name)
+	if err != nil {
+		e.msg_error(err.Error())
+		return
+	}
+	defer db.Close()
+	err = db.Ping()
+	if err != nil {
+		e.msg_error(err.Error())
+		return
+	}
 
 	// Try to install all tables
 
 	// Save mysql config file
-	err := utils.MySqlConfigWrite(e.VHostHome, pf_host, pf_name, pf_user, pf_password)
+	err = utils.MySqlConfigWrite(e.VHostHome, pf_host, pf_name, pf_user, pf_password)
 	if err != nil {
-		e.write(fmt.Sprintf(`ModalShowMsg('Error', '%s');`, err))
+		e.msg_error(err.Error())
+		return
 	}
 
 	// Reload current page

+ 17 - 0
engine/actions/actions.go

@@ -1,7 +1,9 @@
 package actions
 
 import (
+	"fmt"
 	"net/http"
+	"strings"
 )
 
 type hRun func(e *Action)
@@ -23,6 +25,21 @@ func (e *Action) write(data string) {
 	(*e.W).Write([]byte(data))
 }
 
+func (e *Action) msg_show(title string, msg string) {
+	e.write(fmt.Sprintf(
+		`ModalShowMsg('%s', '%s');`,
+		strings.Replace(strings.Replace(title, `'`, `’`, -1), `"`, `”`, -1),
+		strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
+}
+
+func (e *Action) msg_success(msg string) {
+	e.msg_show("Success", msg)
+}
+
+func (e *Action) msg_error(msg string) {
+	e.msg_show("Error", msg)
+}
+
 func New(w *http.ResponseWriter, r *http.Request, vhost string, vhosthome string, remoteip string) *Action {
 	act := Action{w, r, vhost, vhosthome, remoteip, make(map[string]hRun)}