Browse Source

Run module function by url

Vova Tkach 6 years ago
parent
commit
a6d6a6c582
3 changed files with 58 additions and 15 deletions
  1. 0 3
      engine/engine.go
  2. 15 6
      modules/module_index.go
  3. 43 6
      modules/modules.go

+ 0 - 3
engine/engine.go

@@ -18,8 +18,6 @@ import (
 type Engine struct {
 	Wrap *wrapper.Wrapper
 	Mods *modules.Modules
-	// Actions
-	// Front-end or Back-end
 }
 
 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 {
@@ -28,7 +26,6 @@ func Response(l *logger.Logger, m *modules.Modules, w http.ResponseWriter, r *ht
 		Wrap: wrap,
 		Mods: m,
 	}
-
 	return eng.Process()
 }
 

+ 15 - 6
modules/module_index.go

@@ -2,20 +2,29 @@ package modules
 
 import (
 	"fmt"
+	"net/http"
 
 	"golang-fave/engine/wrapper"
 )
 
-func (this *Modules) RegisterModule_index() *Module {
-	return this.newModule(false, "Pages", func(wrap *wrapper.Wrapper) {
-		fmt.Printf("FrontEnd func call\n")
+func (this *Modules) RegisterModule_Index() *Module {
+	return this.newModule(false, "index", "Pages", func(wrap *wrapper.Wrapper) {
+		//fmt.Printf("FrontEnd func call\n")
+		wrap.W.WriteHeader(http.StatusOK)
+		wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+		wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
+		wrap.W.Write([]byte(`FrontEnd func call`))
 	}, func(wrap *wrapper.Wrapper) {
-		fmt.Printf("BackEnd func call\n")
+		//fmt.Printf("BackEnd func call\n")
+		wrap.W.WriteHeader(http.StatusOK)
+		wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+		wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
+		wrap.W.Write([]byte(`BackEnd func call`))
 	})
 }
 
-func (this *Modules) RegisterAction_mysql() *Action {
-	return this.newAction(false, func(wrap *wrapper.Wrapper) {
+func (this *Modules) RegisterAction_MysqlSetup() *Action {
+	return this.newAction(false, "mysql", func(wrap *wrapper.Wrapper) {
 		fmt.Printf("ActFunc func call\n")
 	})
 }

+ 43 - 6
modules/modules.go

@@ -1,16 +1,19 @@
 package modules
 
 import (
+	//"fmt"
 	"net/http"
 	"reflect"
 	"strings"
 
 	"golang-fave/engine/wrapper"
+	"golang-fave/utils"
 )
 
 type Module struct {
 	Id       string
 	WantDB   bool
+	Mount    string
 	Name     string
 	FrontEnd func(wrap *wrapper.Wrapper)
 	BackEnd  func(wrap *wrapper.Wrapper)
@@ -19,6 +22,7 @@ type Module struct {
 type Action struct {
 	Id      string
 	WantDB  bool
+	Mount   string
 	ActFunc func(wrap *wrapper.Wrapper)
 }
 
@@ -41,7 +45,7 @@ func (this *Modules) load() {
 				if len(result) >= 1 {
 					mod := result[0].Interface().(*Module)
 					mod.Id = id
-					this.mods[mod.Id] = mod
+					this.mods[mod.Mount] = mod
 				}
 			}
 		}
@@ -52,25 +56,27 @@ func (this *Modules) load() {
 				if len(result) >= 1 {
 					act := result[0].Interface().(*Action)
 					act.Id = id
-					this.acts[act.Id] = act
+					this.acts[act.Mount] = act
 				}
 			}
 		}
 	}
 }
 
-func (this *Modules) newModule(WantDB bool, Name string, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper)) *Module {
+func (this *Modules) newModule(WantDB bool, Mount string, Name string, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper)) *Module {
 	return &Module{
 		WantDB:   WantDB,
+		Mount:    Mount,
 		Name:     Name,
 		FrontEnd: ff,
 		BackEnd:  bf,
 	}
 }
 
-func (this *Modules) newAction(WantDB bool, af func(wrap *wrapper.Wrapper)) *Action {
+func (this *Modules) newAction(WantDB bool, Mount string, af func(wrap *wrapper.Wrapper)) *Action {
 	return &Action{
 		WantDB:  WantDB,
+		Mount:   Mount,
 		ActFunc: af,
 	}
 }
@@ -114,11 +120,42 @@ func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
 }
 
 func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
-	//
+	var mod *Module = nil
+
+	// Some module
+	if len(wrap.UrlArgs) > 0 {
+		if m, ok := this.mods[wrap.UrlArgs[0]]; ok {
+			mod = m
+		}
+	}
+
+	// Default module
+	if mod == nil {
+		if m, ok := this.mods["index"]; ok {
+			mod = m
+		}
+	}
+
+	// Check and run
+	if mod != nil {
+		if mod.FrontEnd != nil {
+			if mod.WantDB {
+				err := wrap.UseDatabase()
+				if err != nil {
+					utils.SystemErrorPageEngine(wrap.W, err)
+					return true
+				}
+				defer wrap.DB.Close()
+			}
+			mod.FrontEnd(wrap)
+			return true
+		}
+	}
+
 	return false
 }
 
 func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
-	//
+	//fmt.Printf("Back: %v\n", wrap.UrlArgs)
 	return false
 }