Browse Source

Restructure code

Vova Tkach 6 years ago
parent
commit
cc2bf03c27

+ 2 - 1
backend.go

@@ -4,11 +4,12 @@ import (
 	"golang-fave/engine/wrapper"
 
 	Templates "golang-fave/engine/wrapper/resources/templates"
+	utils "golang-fave/engine/wrapper/utils"
 )
 
 func handleBackEnd(e *wrapper.Wrapper) bool {
 	// MySQL config page
-	if !e.IsMySqlConfigExists() {
+	if !utils.IsMySqlConfigExists(e.DirVhostHome) {
 		return e.TmplBackEnd(Templates.CpMySQL, nil)
 	}
 

+ 5 - 16
engine/actions/action_mysql.go

@@ -2,16 +2,11 @@ package actions
 
 import (
 	"fmt"
+
+	utils "golang-fave/engine/wrapper/utils"
 )
 
 func action_mysql(e *Action) {
-	/*
-	action := e.R.FormValue("action")
-	e.write(fmt.Sprintf(`
-		ModalShowMsg('MySQL Action', 'Hello from web server (%s)');
-	`, action))
-	*/
-
 	pf_host := e.R.FormValue("host")
 	pf_name := e.R.FormValue("name")
 	pf_user := e.R.FormValue("user")
@@ -34,19 +29,13 @@ func action_mysql(e *Action) {
 
 	// Try connect to mysql
 
-
 	// Try to install all tables
 
 	// Save mysql config file
-	err := e.MySqlConfigWrite(pf_host, pf_name, pf_user, pf_password)
-	
-
-	/*
-	if pf_host == "" || pf_name == "" || pf_user == "" || pf_password == "" {
-		e.write(fmt.Sprintf(`ModalShowMsg('Error', 'Not all fields are filed');`))
-		return
+	err := utils.MySqlConfigWrite(e.VHostHome, pf_host, pf_name, pf_user, pf_password)
+	if err != nil {
+		e.write(fmt.Sprintf(`ModalShowMsg('Error', '%s');`, err))
 	}
-	*/
 
 	// Reload current page
 	e.write(fmt.Sprintf(`window.location.reload(false);`))

+ 1 - 1
engine/actions/actions.go

@@ -10,7 +10,7 @@ type Action struct {
 	W         *http.ResponseWriter
 	R         *http.Request
 	VHost     string
-	vhosthome string
+	VHostHome string
 	RemoteIp  string
 	list      map[string]hRun
 }

+ 8 - 8
engine/wrapper/config_mysql.go → engine/wrapper/utils/config_mysql.go

@@ -1,4 +1,4 @@
-package wrapper
+package utils
 
 import (
 	"encoding/json"
@@ -12,11 +12,11 @@ type ConfigMySql struct {
 	Password string
 }
 
-func (e *Wrapper) IsMySqlConfigExists() bool {
-	f, err := os.Open(e.DirVhostHome + "/config/mysql.json")
+func IsMySqlConfigExists(homedir string) bool {
+	f, err := os.Open(homedir + "/config/mysql.json")
 	if err == nil {
 		defer f.Close()
-		st, err := os.Stat(e.DirVhostHome + "/config/mysql.json")
+		st, err := os.Stat(homedir + "/config/mysql.json")
 		if err == nil {
 			if !st.Mode().IsDir() {
 				return true
@@ -26,8 +26,8 @@ func (e *Wrapper) IsMySqlConfigExists() bool {
 	return false
 }
 
-func (e *Wrapper) MySqlConfigRead() (*ConfigMySql, error) {
-	f, err := os.Open(e.DirVhostHome + "/config/mysql.json")
+func MySqlConfigRead(homedir string) (*ConfigMySql, error) {
+	f, err := os.Open(homedir + "/config/mysql.json")
 	if err == nil {
 		defer f.Close()
 		dec := json.NewDecoder(f)
@@ -40,7 +40,7 @@ func (e *Wrapper) MySqlConfigRead() (*ConfigMySql, error) {
 	return nil, err
 }
 
-func (e *Wrapper) MySqlConfigWrite(host string, name string, user string, password string) error {
+func MySqlConfigWrite(homedir string, host string, name string, user string, password string) error {
 	r, err := json.Marshal(&ConfigMySql{
 		Host:     host,
 		Name:     name,
@@ -48,7 +48,7 @@ func (e *Wrapper) MySqlConfigWrite(host string, name string, user string, passwo
 		Password: password,
 	})
 	if err == nil {
-		f, err := os.Create(e.DirVhostHome + "/config/mysql.json")
+		f, err := os.Create(homedir + "/config/mysql.json")
 		if err == nil {
 			defer f.Close()
 			_, err = f.WriteString(string(r))

+ 1 - 0
engine/wrapper/utils/utils.go

@@ -0,0 +1 @@
+package utils

+ 3 - 1
frontend.go

@@ -3,6 +3,8 @@ package main
 import (
 	"golang-fave/engine/wrapper"
 	"net/http"
+
+	utils "golang-fave/engine/wrapper/utils"
 )
 
 type MenuItem struct {
@@ -20,7 +22,7 @@ type TmplData struct {
 
 func handleFrontEnd(e *wrapper.Wrapper) bool {
 	// Redirect to CP, if MySQL config file is not exists
-	if !e.IsMySqlConfigExists() {
+	if !utils.IsMySqlConfigExists(e.DirVhostHome) {
 		(*e.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 		http.Redirect(*e.W, e.R, e.R.URL.Scheme+"://"+e.R.Host+"/cp/", 302)
 		return true