Browse Source

POST actions

Vova Tkach 6 years ago
parent
commit
2821835032
4 changed files with 82 additions and 19 deletions
  1. 12 0
      engine/actions/action_signin.go
  2. 47 0
      engine/actions/actions.go
  3. 10 10
      engine/sessions/sessions.go
  4. 13 9
      engine/wrapper/wrapper.go

+ 12 - 0
engine/actions/action_signin.go

@@ -0,0 +1,12 @@
+package actions
+
+import (
+	"fmt"
+)
+
+func action_signin(e *Action) {
+	action := e.R.FormValue("action")
+	(*e.W).Write([]byte(fmt.Sprintf(`
+		alert('Action: %s');
+	`, action)))
+}

+ 47 - 0
engine/actions/actions.go

@@ -0,0 +1,47 @@
+package actions
+
+import (
+	"net/http"
+)
+
+type hRun func(e *Action)
+
+type Action struct {
+	W         *http.ResponseWriter
+	R         *http.Request
+	VHost     string
+	vhosthome string
+	RemoteIp  string
+	list      map[string]hRun
+}
+
+func (e *Action) register(name string, handle hRun) {
+	e.list[name] = handle
+}
+
+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)}
+
+	// Register all action here
+	act.register("signin", action_signin)
+
+	return &act
+}
+
+func (e *Action) Call() bool {
+	if e.R.Method != "POST" {
+		return false
+	}
+	if err := e.R.ParseForm(); err == nil {
+		action := e.R.FormValue("action")
+		if action != "" {
+			fn, ok := e.list[action]
+			if ok {
+				(*e.W).Header().Set("Content-Type", "text/html; charset=utf-8")
+				fn(e)
+				return true
+			}
+		}
+	}
+	return false
+}

+ 10 - 10
engine/sessions/sessions.go

@@ -19,11 +19,11 @@ type Vars struct {
 }
 
 type Session struct {
-	w         *http.ResponseWriter
-	r         *http.Request
-	vhost     string
-	vhosthome string
-	remoteip  string
+	W         *http.ResponseWriter
+	R         *http.Request
+	VHost     string
+	VHostHome string
+	RemoteIp  string
 	vars      *Vars
 	Ident     string
 	changed   bool
@@ -34,12 +34,12 @@ func New(w *http.ResponseWriter, r *http.Request, vhost string, vhosthome string
 }
 
 func (s *Session) Load() {
-	var session, err = s.r.Cookie("fsession")
+	var session, err = s.R.Cookie("fsession")
 	if err == nil && len(session.Value) == 40 {
 		// Load session
 		s.Ident = session.Value
 		StartNewSession := true
-		fsessfile := s.vhosthome + "/tmp/" + s.Ident
+		fsessfile := s.VHostHome + "/tmp/" + s.Ident
 		file, ferr := os.Open(fsessfile)
 		if ferr == nil {
 			defer file.Close()
@@ -62,7 +62,7 @@ func (s *Session) Load() {
 		// Generate unique hash
 		rand.Seed(time.Now().Unix())
 		rnd := rand.Intn(9999999-99) + 99
-		userstr := s.vhost + s.remoteip + s.r.Header.Get("User-Agent") +
+		userstr := s.VHost + s.RemoteIp + s.R.Header.Get("User-Agent") +
 			strconv.FormatInt((int64(time.Now().Unix())), 10) +
 			strconv.FormatInt(int64(rnd), 10)
 		userhashstr := fmt.Sprintf("%x", sha1.Sum([]byte(userstr)))
@@ -79,7 +79,7 @@ func (s *Session) Load() {
 		// Set session cookie
 		expiration := time.Now().Add(365 * 24 * time.Hour)
 		cookie := http.Cookie{Name: "fsession", Value: userhashstr, Expires: expiration}
-		http.SetCookie(*s.w, &cookie)
+		http.SetCookie(*s.W, &cookie)
 	}
 }
 
@@ -87,7 +87,7 @@ func (s *Session) Save() bool {
 	if !s.changed {
 		return false
 	}
-	fsessfile := s.vhosthome + "/tmp/" + s.Ident
+	fsessfile := s.VHostHome + "/tmp/" + s.Ident
 	r, err := json.Marshal(s.vars)
 	if err == nil {
 		file, ferr := os.Create(fsessfile)

+ 13 - 9
engine/wrapper/wrapper.go

@@ -8,6 +8,7 @@ import (
 	"os"
 	"strings"
 
+	"golang-fave/engine/actions"
 	"golang-fave/engine/sessions"
 )
 
@@ -42,6 +43,7 @@ type Wrapper struct {
 	RemoteIp     string
 	LoggerAcc    *log.Logger
 	LoggerErr    *log.Logger
+	Action       *actions.Action
 	Session      *sessions.Session
 	Debug        bool
 }
@@ -140,21 +142,23 @@ 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)
+
+	// Call action
+	if e.Action.Call() {
+		e.Session.Save()
+		return
+	}
+
 	// Logic
-	ret := false
 	if hRun != nil {
 		if hRun(e) {
-			ret = true
+			e.Session.Save()
+			return
 		}
 	}
 
-	// Save session
-	e.Session.Save()
-
-	if ret {
-		return
-	}
-
 	// Show default page
 	if e.R.URL.Path == "/" {
 		e.Log("200")