action_mysql.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package actions
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strconv"
  6. _ "github.com/go-sql-driver/mysql"
  7. utils "golang-fave/engine/wrapper/utils"
  8. )
  9. func (this *Action) Action_mysql() {
  10. pf_host := this.wrapper.R.FormValue("host")
  11. pf_port := this.wrapper.R.FormValue("port")
  12. pf_name := this.wrapper.R.FormValue("name")
  13. pf_user := this.wrapper.R.FormValue("user")
  14. pf_password := this.wrapper.R.FormValue("password")
  15. if pf_host == "" {
  16. this.msg_error(`Please specify host for MySQL connection`)
  17. return
  18. }
  19. if pf_port == "" {
  20. this.msg_error(`Please specify host port for MySQL connection`)
  21. return
  22. }
  23. if _, err := strconv.Atoi(pf_port); err != nil {
  24. this.msg_error(`MySQL host port must be integer number`)
  25. return
  26. }
  27. if pf_name == "" {
  28. this.msg_error(`Please specify MySQL database name`)
  29. return
  30. }
  31. if pf_user == "" {
  32. this.msg_error(`Please specify MySQL user`)
  33. return
  34. }
  35. // Try connect to mysql
  36. db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
  37. if err != nil {
  38. this.msg_error(err.Error())
  39. return
  40. }
  41. defer db.Close()
  42. err = db.Ping()
  43. if err != nil {
  44. this.msg_error(err.Error())
  45. return
  46. }
  47. // Try to install all tables
  48. // Save mysql config file
  49. err = utils.MySqlConfigWrite(this.wrapper.DirVhostHome, pf_host, pf_port, pf_name, pf_user, pf_password)
  50. if err != nil {
  51. this.msg_error(err.Error())
  52. return
  53. }
  54. // Reload current page
  55. this.write(fmt.Sprintf(`window.location.reload(false);`))
  56. }