Browse Source

Better way for calling actions

Vova Tkach 6 years ago
parent
commit
412553862c
4 changed files with 40 additions and 52 deletions
  1. 16 16
      engine/actions/action_mysql.go
  2. 3 3
      engine/actions/action_signin.go
  3. 21 32
      engine/actions/actions.go
  4. 0 1
      main.go

+ 16 - 16
engine/actions/action_mysql.go

@@ -10,60 +10,60 @@ import (
 	utils "golang-fave/engine/wrapper/utils"
 )
 
-func action_mysql(e *Action) {
-	pf_host := e.w.R.FormValue("host")
-	pf_port := e.w.R.FormValue("port")
-	pf_name := e.w.R.FormValue("name")
-	pf_user := e.w.R.FormValue("user")
-	pf_password := e.w.R.FormValue("password")
+func (this *Action) Action_mysql() {
+	pf_host := this.wrapper.R.FormValue("host")
+	pf_port := this.wrapper.R.FormValue("port")
+	pf_name := this.wrapper.R.FormValue("name")
+	pf_user := this.wrapper.R.FormValue("user")
+	pf_password := this.wrapper.R.FormValue("password")
 
 	if pf_host == "" {
-		e.msg_error(`Please specify host for MySQL connection`)
+		this.msg_error(`Please specify host for MySQL connection`)
 		return
 	}
 
 	if pf_port == "" {
-		e.msg_error(`Please specify host port for MySQL connection`)
+		this.msg_error(`Please specify host port for MySQL connection`)
 		return
 	}
 
 	if _, err := strconv.Atoi(pf_port); err != nil {
-		e.msg_error(`MySQL host port must be integer number`)
+		this.msg_error(`MySQL host port must be integer number`)
 		return
 	}
 
 	if pf_name == "" {
-		e.msg_error(`Please specify MySQL database name`)
+		this.msg_error(`Please specify MySQL database name`)
 		return
 	}
 
 	if pf_user == "" {
-		e.msg_error(`Please specify MySQL user`)
+		this.msg_error(`Please specify MySQL user`)
 		return
 	}
 
 	// Try connect to mysql
 	db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
 	if err != nil {
-		e.msg_error(err.Error())
+		this.msg_error(err.Error())
 		return
 	}
 	defer db.Close()
 	err = db.Ping()
 	if err != nil {
-		e.msg_error(err.Error())
+		this.msg_error(err.Error())
 		return
 	}
 
 	// Try to install all tables
 
 	// Save mysql config file
-	err = utils.MySqlConfigWrite(e.w.DirVhostHome, pf_host, pf_port, pf_name, pf_user, pf_password)
+	err = utils.MySqlConfigWrite(this.wrapper.DirVhostHome, pf_host, pf_port, pf_name, pf_user, pf_password)
 	if err != nil {
-		e.msg_error(err.Error())
+		this.msg_error(err.Error())
 		return
 	}
 
 	// Reload current page
-	e.write(fmt.Sprintf(`window.location.reload(false);`))
+	this.write(fmt.Sprintf(`window.location.reload(false);`))
 }

+ 3 - 3
engine/actions/action_signin.go

@@ -4,9 +4,9 @@ import (
 	"fmt"
 )
 
-func action_signin(e *Action) {
-	action := e.w.R.FormValue("action")
-	e.write(fmt.Sprintf(`
+func (this *Action) Action_signin() {
+	action := this.wrapper.R.FormValue("action")
+	this.write(fmt.Sprintf(`
 		ModalShowMsg('Login Action', 'Hello from web server (%s)');
 	`, action))
 }

+ 21 - 32
engine/actions/actions.go

@@ -3,62 +3,51 @@ package actions
 import (
 	"fmt"
 	"strings"
+	"reflect"
 
 	"golang-fave/engine/wrapper"
 )
 
-type hRun func(e *Action)
+type hRun func(this *Action)
 
 type Action struct {
-	w    *wrapper.Wrapper
-	list map[string]hRun
+	wrapper *wrapper.Wrapper
 }
 
-func (e *Action) register(name string, handle hRun) {
-	e.list[name] = handle
+func (this *Action) write(data string) {
+	(*this.wrapper.W).Write([]byte(data))
 }
 
-func (e *Action) write(data string) {
-	(*e.w.W).Write([]byte(data))
-}
-
-func (e *Action) msg_show(title string, msg string) {
-	e.write(fmt.Sprintf(
+func (this *Action) msg_show(title string, msg string) {
+	this.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 (this *Action) msg_success(msg string) {
+	this.msg_show("Success", msg)
 }
 
-func (e *Action) msg_error(msg string) {
-	e.msg_show("Error", msg)
+func (this *Action) msg_error(msg string) {
+	this.msg_show("Error", msg)
 }
 
-func New(w *wrapper.Wrapper) *Action {
-	act := Action{w, make(map[string]hRun)}
-
-	// Register all action here
-	act.register("mysql", action_mysql)
-	act.register("signin", action_signin)
-
-	return &act
+func New(wrapper *wrapper.Wrapper) *Action {
+	return &Action{wrapper}
 }
 
-func (e *Action) Call() bool {
-	if e.w.R.Method != "POST" {
+func (this *Action) Call() bool {
+	if this.wrapper.R.Method != "POST" {
 		return false
 	}
-	if err := e.w.R.ParseForm(); err == nil {
-		action := e.w.R.FormValue("action")
+	if err := this.wrapper.R.ParseForm(); err == nil {
+		action := this.wrapper.R.FormValue("action")
 		if action != "" {
-			function, ok := e.list[action]
-			if ok {
-				(*e.w.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-				(*e.w.W).Header().Set("Content-Type", "text/html; charset=utf-8")
-				function(e)
+			if _, ok := reflect.TypeOf(this).MethodByName("Action_" + action); ok {
+				(*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+				(*this.wrapper.W).Header().Set("Content-Type", "text/html; charset=utf-8")
+				reflect.ValueOf(this).MethodByName("Action_" + action).Call([]reflect.Value{})
 				return true
 			}
 		}

+ 0 - 1
main.go

@@ -121,7 +121,6 @@ func handler(w http.ResponseWriter, r *http.Request) {
 			// Actions
 			action := actions.New(wrapper)
 			if action.Call() {
-				wrapper.Log("200")
 				wrapper.Session.Save()
 				return true
 			}