Browse Source

Modules by reflect

Vova Tkach 6 years ago
parent
commit
decaf5e97e
4 changed files with 106 additions and 12 deletions
  1. 3 3
      engine/engine.go
  2. 0 1
      main.go
  3. 39 0
      modules/module_index.go
  4. 64 8
      modules/modules.go

+ 3 - 3
engine/engine.go

@@ -37,7 +37,7 @@ func (this *Engine) Process() bool {
 	this.Wrap.ConfMysqlExists = utils.IsMySqlConfigExists(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
 	this.Wrap.ConfMysqlExists = utils.IsMySqlConfigExists(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
 
 
 	// Action
 	// Action
-	if this.Mods.Action(this.Wrap) {
+	if this.Mods.XXXActionFire(this.Wrap) {
 		return true
 		return true
 	}
 	}
 
 
@@ -79,7 +79,7 @@ func (this *Engine) Process() bool {
 
 
 	// Separated logic
 	// Separated logic
 	if this.Wrap.IsBackend {
 	if this.Wrap.IsBackend {
-		return this.Mods.BackEnd(this.Wrap)
+		return this.Mods.XXXBackEnd(this.Wrap)
 	}
 	}
-	return this.Mods.FrontEnd(this.Wrap)
+	return this.Mods.XXXFrontEnd(this.Wrap)
 }
 }

+ 0 - 1
main.go

@@ -66,7 +66,6 @@ func main() {
 
 
 	// Init modules
 	// Init modules
 	mods := modules.New()
 	mods := modules.New()
-	mods.Load()
 
 
 	// Init and start web server
 	// 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) {
 	bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {

+ 39 - 0
modules/module_index.go

@@ -0,0 +1,39 @@
+package modules
+
+import (
+	"fmt"
+
+	"golang-fave/engine/wrapper"
+)
+
+func (this *Modules) RegisterModule_index() *Module {
+	return &Module{
+		Id:   "index",
+		Name: "Pages",
+		FrontEnd: func(mod *Modules, wrap *wrapper.Wrapper) {
+			fmt.Printf("FrontEnd func call\n")
+		},
+		BackEnd: func(mod *Modules, wrap *wrapper.Wrapper) {
+			fmt.Printf("BackEnd func call\n")
+		},
+	}
+}
+
+func (this *Modules) RegisterAction_test() *Action {
+	return &Action{
+		Id: "test",
+		ActFunc: func(mod *Modules, wrap *wrapper.Wrapper) {
+			fmt.Printf("ActFunc func call\n")
+		},
+	}
+}
+
+/*
+func (this *Modules) module_index_frontend(wrap *wrapper.Wrapper) {
+	//
+}
+
+func (this *Modules) module_index_backend(wrap *wrapper.Wrapper) {
+	//
+}
+*/

+ 64 - 8
modules/modules.go

@@ -2,12 +2,15 @@ package modules
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"reflect"
+	"strings"
 
 
 	"golang-fave/engine/wrapper"
 	"golang-fave/engine/wrapper"
 )
 )
 
 
-type FuncFrontEnd func(wrap *wrapper.Wrapper)
-type FuncBackEnd func(wrap *wrapper.Wrapper)
+type FuncFrontEnd func(mod *Modules, wrap *wrapper.Wrapper)
+type FuncBackEnd func(mod *Modules, wrap *wrapper.Wrapper)
+type FuncAction func(mod *Modules, wrap *wrapper.Wrapper)
 
 
 type Module struct {
 type Module struct {
 	Id       string
 	Id       string
@@ -16,20 +19,73 @@ type Module struct {
 	BackEnd  FuncBackEnd
 	BackEnd  FuncBackEnd
 }
 }
 
 
+type Action struct {
+	Id      string
+	ActFunc FuncAction
+}
+
 type Modules struct {
 type Modules struct {
-	list map[string]Module
+	mods map[string]Module
+	acts map[string]Action
 }
 }
 
 
 func New() *Modules {
 func New() *Modules {
 	m := Modules{
 	m := Modules{
-		list: map[string]Module{},
+		mods: map[string]Module{},
+		acts: map[string]Action{},
 	}
 	}
+	m.load()
 	return &m
 	return &m
 }
 }
 
 
-func (this *Modules) Load() {
+func (this *Modules) load() {
 	// Called before server starts
 	// Called before server starts
 	fmt.Println("Load modules")
 	fmt.Println("Load modules")
+	fmt.Println("---")
+
+	t := reflect.TypeOf(this)
+	for i := 0; i < t.NumMethod(); i++ {
+		m := t.Method(i)
+		if strings.HasPrefix(m.Name, "XXX") {
+			continue
+		}
+		if strings.HasPrefix(m.Name, "RegisterModule_") {
+			//fmt.Printf("%s\n", m.Name)
+			id := m.Name[15:]
+			fmt.Printf("Module (%s)\n", id)
+
+			if _, ok := reflect.TypeOf(this).MethodByName("RegisterModule_" + id); ok {
+				result := reflect.ValueOf(this).MethodByName("RegisterModule_" + id).Call([]reflect.Value{})
+				if len(result) >= 1 {
+					//fmt.Printf("--- result -> (%d)\n", len(result))
+					//fmt.Printf("%v\n", result[0])
+					//fmt.Printf("%T\n", result)
+					//mod := result[0]
+					//mod := result[0]
+					//fmt.Printf("%v\n", mod)
+					//fmt.Printf("%s\n", *Module(mod).Id)
+					mod := result[0].Interface().(*Module)
+					fmt.Printf("%s\n", mod.Id)
+				}
+			} else {
+				fmt.Printf("Error\n")
+			}
+			// Add to array
+			//mod := 
+			/*
+			if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
+				result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
+				return result[0].String()
+			}
+			*/
+		}
+		if strings.HasPrefix(m.Name, "RegisterAction_") {
+			//fmt.Printf("%s\n", m.Name)
+			id := m.Name[15:]
+			fmt.Printf("Action (%s)\n", id)
+			// Add to array
+		}
+	}
 }
 }
 
 
 // All actions here...
 // All actions here...
@@ -39,19 +95,19 @@ func (this *Modules) Load() {
 // User logout
 // User logout
 
 
 // Called inside goroutine
 // Called inside goroutine
-func (this *Modules) Action(wrap *wrapper.Wrapper) bool {
+func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
 	//
 	//
 	return false
 	return false
 }
 }
 
 
 // Called inside goroutine
 // Called inside goroutine
-func (this *Modules) FrontEnd(wrap *wrapper.Wrapper) bool {
+func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
 	//
 	//
 	return false
 	return false
 }
 }
 
 
 // Called inside goroutine
 // Called inside goroutine
-func (this *Modules) BackEnd(wrap *wrapper.Wrapper) bool {
+func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
 	//
 	//
 	return false
 	return false
 }
 }