Browse Source

Move actions upper

Vova Tkach 6 years ago
parent
commit
f0dd508828
7 changed files with 34 additions and 43 deletions
  1. 0 1
      backend.go
  2. 6 6
      engine/actions/action_mysql.go
  3. 1 1
      engine/actions/action_signin.go
  4. 13 19
      engine/actions/actions.go
  5. 0 12
      engine/wrapper/wrapper.go
  6. 1 1
      frontend.go
  7. 13 3
      main.go

+ 0 - 1
backend.go

@@ -2,7 +2,6 @@ package main
 
 import (
 	"golang-fave/engine/wrapper"
-
 	Templates "golang-fave/engine/wrapper/resources/templates"
 	utils "golang-fave/engine/wrapper/utils"
 )

+ 6 - 6
engine/actions/action_mysql.go

@@ -11,11 +11,11 @@ import (
 )
 
 func action_mysql(e *Action) {
-	pf_host := e.R.FormValue("host")
-	pf_port := e.R.FormValue("port")
-	pf_name := e.R.FormValue("name")
-	pf_user := e.R.FormValue("user")
-	pf_password := e.R.FormValue("password")
+	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")
 
 	if pf_host == "" {
 		e.msg_error(`Please specify host for MySQL connection`)
@@ -58,7 +58,7 @@ func action_mysql(e *Action) {
 	// Try to install all tables
 
 	// Save mysql config file
-	err = utils.MySqlConfigWrite(e.VHostHome, pf_host, pf_port, pf_name, pf_user, pf_password)
+	err = utils.MySqlConfigWrite(e.w.DirVhostHome, pf_host, pf_port, pf_name, pf_user, pf_password)
 	if err != nil {
 		e.msg_error(err.Error())
 		return

+ 1 - 1
engine/actions/action_signin.go

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

+ 13 - 19
engine/actions/actions.go

@@ -2,22 +2,16 @@ package actions
 
 import (
 	"fmt"
-	"net/http"
 	"strings"
 
-	"golang-fave/engine/sessions"
+	"golang-fave/engine/wrapper"
 )
 
 type hRun func(e *Action)
 
 type Action struct {
-	W         *http.ResponseWriter
-	R         *http.Request
-	VHost     string
-	VHostHome string
-	RemoteIp  string
-	Session   *sessions.Session
-	list      map[string]hRun
+	w    *wrapper.Wrapper
+	list map[string]hRun
 }
 
 func (e *Action) register(name string, handle hRun) {
@@ -25,7 +19,7 @@ func (e *Action) register(name string, handle hRun) {
 }
 
 func (e *Action) write(data string) {
-	(*e.W).Write([]byte(data))
+	(*e.w.W).Write([]byte(data))
 }
 
 func (e *Action) msg_show(title string, msg string) {
@@ -43,8 +37,8 @@ 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, session *sessions.Session) *Action {
-	act := Action{w, r, vhost, vhosthome, remoteip, session, make(map[string]hRun)}
+func New(w *wrapper.Wrapper) *Action {
+	act := Action{w, make(map[string]hRun)}
 
 	// Register all action here
 	act.register("mysql", action_mysql)
@@ -54,17 +48,17 @@ func New(w *http.ResponseWriter, r *http.Request, vhost string, vhosthome string
 }
 
 func (e *Action) Call() bool {
-	if e.R.Method != "POST" {
+	if e.w.R.Method != "POST" {
 		return false
 	}
-	if err := e.R.ParseForm(); err == nil {
-		action := e.R.FormValue("action")
+	if err := e.w.R.ParseForm(); err == nil {
+		action := e.w.R.FormValue("action")
 		if action != "" {
-			fn, ok := e.list[action]
+			function, ok := e.list[action]
 			if ok {
-				(*e.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-				(*e.W).Header().Set("Content-Type", "text/html; charset=utf-8")
-				fn(e)
+				(*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)
 				return true
 			}
 		}

+ 0 - 12
engine/wrapper/wrapper.go

@@ -8,7 +8,6 @@ import (
 	"os"
 	"strings"
 
-	"golang-fave/engine/actions"
 	"golang-fave/engine/sessions"
 	Templates "golang-fave/engine/wrapper/resources/templates"
 )
@@ -45,7 +44,6 @@ type Wrapper struct {
 	RemoteIp     string
 	LoggerAcc    *log.Logger
 	LoggerErr    *log.Logger
-	Action       *actions.Action
 	Session      *sessions.Session
 	Debug        bool
 }
@@ -145,16 +143,6 @@ func (e *Wrapper) Run(hRun handleRun) {
 		e.Session.SetBool("IsLogged", false)
 	}
 
-	// Create action
-	e.Action = actions.New(e.W, e.R, e.VHost, e.DirVhostHome, e.RemoteIp, e.Session)
-
-	// Call action
-	if e.Action.Call() {
-		e.Log("200")
-		e.Session.Save()
-		return
-	}
-
 	// Logic
 	if hRun != nil {
 		if hRun(e) {

+ 1 - 1
frontend.go

@@ -1,9 +1,9 @@
 package main
 
 import (
-	"golang-fave/engine/wrapper"
 	"net/http"
 
+	"golang-fave/engine/wrapper"
 	utils "golang-fave/engine/wrapper/utils"
 )
 

+ 13 - 3
main.go

@@ -12,6 +12,7 @@ import (
 	"strings"
 	"time"
 
+	"golang-fave/engine/actions"
 	"golang-fave/engine/wrapper"
 )
 
@@ -117,10 +118,19 @@ func handler(w http.ResponseWriter, r *http.Request) {
 	// Create and start engine
 	wrapper.New(&w, r, host, port, FParamWwwDir, FVhostHomeDir, C_Debug).
 		Run(func(e *wrapper.Wrapper) bool {
-			if e.R.URL.Path == "/cp" || strings.HasPrefix(e.R.URL.Path, "/cp/") {
-				return handleBackEnd(e)
-			} else {
+			// Actions
+			action := actions.New(e)
+			if action.Call() {
+				e.Log("200")
+				e.Session.Save()
+				return true
+			}
+
+			// Pages
+			if !(e.R.URL.Path == "/cp" || strings.HasPrefix(e.R.URL.Path, "/cp/")) {
 				return handleFrontEnd(e)
+			} else {
+				return handleBackEnd(e)
 			}
 		})
 }