Vova Tkach 6 years ago
parent
commit
94a1271fef
5 changed files with 77 additions and 21 deletions
  1. 1 1
      engine/backend.go
  2. 11 7
      engine/engine.go
  3. 17 12
      engine/frontend.go
  4. 6 1
      main.go
  5. 42 0
      modules/modules.go

+ 1 - 1
engine/backend.go

@@ -1,5 +1,5 @@
 package engine
 
 func (this *Engine) BackEnd() bool {
-	return false
+	return this.Mods.BackEnd(this.Wrap)
 }

+ 11 - 7
engine/engine.go

@@ -9,6 +9,7 @@ import (
 	"golang-fave/assets"
 	"golang-fave/engine/wrapper"
 	"golang-fave/logger"
+	"golang-fave/modules"
 	"golang-fave/utils"
 
 	"github.com/vladimirok5959/golang-server-sessions/session"
@@ -16,13 +17,17 @@ import (
 
 type Engine struct {
 	Wrap *wrapper.Wrapper
+	Mods *modules.Modules
 	// Actions
 	// Front-end or Back-end
 }
 
-func Response(l *logger.Logger, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) bool {
+func Response(l *logger.Logger, m *modules.Modules, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) bool {
 	wrap := wrapper.New(l, w, r, s, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp)
-	eng := &Engine{Wrap: wrap}
+	eng := &Engine{
+		Wrap: wrap,
+		Mods: m,
+	}
 
 	return eng.Process()
 }
@@ -31,11 +36,10 @@ func (this *Engine) Process() bool {
 	this.Wrap.IsBackend = this.Wrap.R.URL.Path == "/cp" || strings.HasPrefix(this.Wrap.R.URL.Path, "/cp/")
 	this.Wrap.ConfMysqlExists = utils.IsMySqlConfigExists(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
 
-	// All actions here...
-	// MySQL install
-	// MySQL first user
-	// User login
-	// User logout
+	// Action
+	if this.Mods.Action(this.Wrap) {
+		return true
+	}
 
 	// Redirect to CP for creating MySQL config file
 	if !this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {

+ 17 - 12
engine/frontend.go

@@ -1,24 +1,29 @@
 package engine
 
+/*
 import (
 	"fmt"
 )
+*/
 
 func (this *Engine) FrontEnd() bool {
-	if this.Wrap.R.URL.Path == "/" {
-		this.Wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-		this.Wrap.W.Header().Set("Content-Type", "text/html")
+	/*
+		if this.Wrap.R.URL.Path == "/" {
+			this.Wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+			this.Wrap.W.Header().Set("Content-Type", "text/html")
 
-		counter := this.Wrap.S.GetInt("counter", 0)
-		// this.Wrap.LogAccess(fmt.Sprintf("Counter value was: %d", counter))
+			counter := this.Wrap.S.GetInt("counter", 0)
+			// this.Wrap.LogAccess(fmt.Sprintf("Counter value was: %d", counter))
 
-		this.Wrap.W.Write([]byte(`Logic -> (` + fmt.Sprintf("%d", counter) + `)`))
+			this.Wrap.W.Write([]byte(`Logic -> (` + fmt.Sprintf("%d", counter) + `)`))
 
-		counter++
-		this.Wrap.S.SetInt("counter", counter)
-		// this.Wrap.LogAccess(fmt.Sprintf("Counter value now: %d", counter))
+			counter++
+			this.Wrap.S.SetInt("counter", counter)
+			// this.Wrap.LogAccess(fmt.Sprintf("Counter value now: %d", counter))
 
-		return true
-	}
-	return false
+			return true
+		}
+	*/
+	// return false
+	return this.Mods.FrontEnd(this.Wrap)
 }

+ 6 - 1
main.go

@@ -11,6 +11,7 @@ import (
 	"golang-fave/consts"
 	"golang-fave/engine"
 	"golang-fave/logger"
+	"golang-fave/modules"
 	"golang-fave/utils"
 
 	"github.com/vladimirok5959/golang-server-bootstrap/bootstrap"
@@ -63,6 +64,10 @@ func main() {
 	// Init static files helper
 	stat := static.New(consts.DirIndexFile)
 
+	// Init modules
+	mods := modules.New()
+	mods.Load()
+
 	// Init and start web server
 	bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
 		w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
@@ -121,7 +126,7 @@ func main() {
 		defer sess.Close()
 
 		// Logic
-		if engine.Response(lg, w, r, sess, host, port, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp) {
+		if engine.Response(lg, mods, w, r, sess, host, port, vhost_dir_config, vhost_dir_htdocs, vhost_dir_logs, vhost_dir_template, vhost_dir_tmp) {
 			return
 		}
 

+ 42 - 0
modules/modules.go

@@ -0,0 +1,42 @@
+package modules
+
+import (
+	"golang-fave/engine/wrapper"
+)
+
+type Modules struct {
+	//
+}
+
+func New() *Modules {
+	m := Modules{}
+	return &m
+}
+
+func (this *Modules) Load() {
+	// Called before server starts
+}
+
+// All actions here...
+// MySQL install
+// MySQL first user
+// User login
+// User logout
+
+// Called inside goroutine
+func (this *Modules) Action(wrap *wrapper.Wrapper) bool {
+	//
+	return false
+}
+
+// Called inside goroutine
+func (this *Modules) FrontEnd(wrap *wrapper.Wrapper) bool {
+	//
+	return false
+}
+
+// Called inside goroutine
+func (this *Modules) BackEnd(wrap *wrapper.Wrapper) bool {
+	//
+	return false
+}