Browse Source

Move logic, clean main file

Vova Tkach 6 years ago
parent
commit
f382d14404
2 changed files with 45 additions and 28 deletions
  1. 42 0
      engine/engine.go
  2. 3 28
      main.go

+ 42 - 0
engine/engine.go

@@ -1 +1,43 @@
 package engine
 package engine
+
+import (
+	"fmt"
+	"net/http"
+
+	"github.com/vladimirok5959/golang-server-sessions/session"
+)
+
+type Engine struct {
+	w http.ResponseWriter
+	r *http.Request
+	s *session.Session
+
+	host string
+	port string
+
+	dConfig   string
+	dHtdocs   string
+	dLogs     string
+	dTemplate string
+	dTmp      string
+}
+
+func New(w http.ResponseWriter, r *http.Request, s *session.Session, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) *Engine {
+	return &Engine{w, r, s, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp}
+}
+
+func (this *Engine) Response() bool {
+	if this.r.URL.Path == "/" {
+		this.w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+		this.w.Header().Set("Content-Type", "text/html")
+
+		counter := this.s.GetInt("counter", 0)
+		this.w.Write([]byte(`Logic -> (` + fmt.Sprintf("%d", counter) + `)`))
+
+		counter++
+		this.s.SetInt("counter", counter)
+
+		return true
+	}
+	return false
+}

+ 3 - 28
main.go

@@ -9,6 +9,7 @@ import (
 
 
 	"golang-fave/assets"
 	"golang-fave/assets"
 	"golang-fave/consts"
 	"golang-fave/consts"
+	"golang-fave/engine"
 	"golang-fave/utils"
 	"golang-fave/utils"
 
 
 	"github.com/vladimirok5959/golang-server-bootstrap/bootstrap"
 	"github.com/vladimirok5959/golang-server-bootstrap/bootstrap"
@@ -49,9 +50,7 @@ func main() {
 	// Init static files helper
 	// Init static files helper
 	stat := static.New(consts.DirIndexFile)
 	stat := static.New(consts.DirIndexFile)
 
 
-	// TODO: Logic as object here
-	// Init logic
-
+	// Init and start web server
 	bootstrap.Start(fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
 	bootstrap.Start(fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
 		w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
 		w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
 	}, func(w http.ResponseWriter, r *http.Request) {
 	}, func(w http.ResponseWriter, r *http.Request) {
@@ -103,17 +102,11 @@ func main() {
 		}
 		}
 
 
 		// Session
 		// Session
-		//sess := session.New(w, r, vhost_dir_tmp)
-		//defer sess.Close()
-
-		// Session struct need to make public!
 		sess := session.New(w, r, vhost_dir_tmp)
 		sess := session.New(w, r, vhost_dir_tmp)
 		defer sess.Close()
 		defer sess.Close()
 
 
 		// Logic
 		// Logic
-		// TODO: call from `logic.Response()`
-		// TODO: create logic object here???
-		if logic(host, port, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp, w, r) {
+		if engine.New(w, r, sess, host, port, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp).Response() {
 			return
 			return
 		}
 		}
 
 
@@ -125,21 +118,3 @@ func main() {
 	// Delete expired session files
 	// Delete expired session files
 	// session.Clean("./tmp")
 	// session.Clean("./tmp")
 }
 }
-
-func logic(host, port, dir_config, dir_htdocs, dir_logs, dir_template, dir_tmp string, w http.ResponseWriter, r *http.Request) bool {
-	if r.URL.Path == "/" {
-		w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-		w.Header().Set("Content-Type", "text/html")
-
-		//counter := sess.GetInt("counter", 0)
-		//w.Write([]byte(`Logic -> (` + fmt.Sprintf("%d", counter) + `)`))
-
-		w.Write([]byte(`Logic`))
-
-		//counter++
-		//sess.SetInt("counter", counter)
-
-		return true
-	}
-	return false
-}