|
@@ -1,48 +1,33 @@
|
|
package modules
|
|
package modules
|
|
|
|
|
|
import (
|
|
import (
|
|
- "fmt"
|
|
|
|
|
|
+ "net/http"
|
|
"reflect"
|
|
"reflect"
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
"golang-fave/engine/wrapper"
|
|
"golang-fave/engine/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
|
|
|
|
+ WantDB bool
|
|
Name string
|
|
Name string
|
|
- FrontEnd FuncFrontEnd
|
|
|
|
- BackEnd FuncBackEnd
|
|
|
|
|
|
+ FrontEnd func(wrap *wrapper.Wrapper)
|
|
|
|
+ BackEnd func(wrap *wrapper.Wrapper)
|
|
}
|
|
}
|
|
|
|
|
|
type Action struct {
|
|
type Action struct {
|
|
Id string
|
|
Id string
|
|
- ActFunc FuncAction
|
|
|
|
|
|
+ WantDB bool
|
|
|
|
+ ActFunc func(wrap *wrapper.Wrapper)
|
|
}
|
|
}
|
|
|
|
|
|
type Modules struct {
|
|
type Modules struct {
|
|
- mods map[string]Module
|
|
|
|
- acts map[string]Action
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func New() *Modules {
|
|
|
|
- m := Modules{
|
|
|
|
- mods: map[string]Module{},
|
|
|
|
- acts: map[string]Action{},
|
|
|
|
- }
|
|
|
|
- m.load()
|
|
|
|
- return &m
|
|
|
|
|
|
+ mods map[string]*Module
|
|
|
|
+ acts map[string]*Action
|
|
}
|
|
}
|
|
|
|
|
|
func (this *Modules) load() {
|
|
func (this *Modules) load() {
|
|
- // Called before server starts
|
|
|
|
- fmt.Println("Load modules")
|
|
|
|
- fmt.Println("---")
|
|
|
|
-
|
|
|
|
t := reflect.TypeOf(this)
|
|
t := reflect.TypeOf(this)
|
|
for i := 0; i < t.NumMethod(); i++ {
|
|
for i := 0; i < t.NumMethod(); i++ {
|
|
m := t.Method(i)
|
|
m := t.Method(i)
|
|
@@ -50,63 +35,88 @@ func (this *Modules) load() {
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
if strings.HasPrefix(m.Name, "RegisterModule_") {
|
|
if strings.HasPrefix(m.Name, "RegisterModule_") {
|
|
- //fmt.Printf("%s\n", m.Name)
|
|
|
|
id := m.Name[15:]
|
|
id := m.Name[15:]
|
|
- fmt.Printf("Module (%s)\n", id)
|
|
|
|
-
|
|
|
|
if _, ok := reflect.TypeOf(this).MethodByName("RegisterModule_" + id); ok {
|
|
if _, ok := reflect.TypeOf(this).MethodByName("RegisterModule_" + id); ok {
|
|
result := reflect.ValueOf(this).MethodByName("RegisterModule_" + id).Call([]reflect.Value{})
|
|
result := reflect.ValueOf(this).MethodByName("RegisterModule_" + id).Call([]reflect.Value{})
|
|
if len(result) >= 1 {
|
|
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)
|
|
mod := result[0].Interface().(*Module)
|
|
- fmt.Printf("%s\n", mod.Id)
|
|
|
|
|
|
+ mod.Id = id
|
|
|
|
+ this.mods[mod.Id] = mod
|
|
}
|
|
}
|
|
- } 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_") {
|
|
if strings.HasPrefix(m.Name, "RegisterAction_") {
|
|
- //fmt.Printf("%s\n", m.Name)
|
|
|
|
id := m.Name[15:]
|
|
id := m.Name[15:]
|
|
- fmt.Printf("Action (%s)\n", id)
|
|
|
|
- // Add to array
|
|
|
|
|
|
+ if _, ok := reflect.TypeOf(this).MethodByName("RegisterAction_" + id); ok {
|
|
|
|
+ result := reflect.ValueOf(this).MethodByName("RegisterAction_" + id).Call([]reflect.Value{})
|
|
|
|
+ if len(result) >= 1 {
|
|
|
|
+ act := result[0].Interface().(*Action)
|
|
|
|
+ act.Id = id
|
|
|
|
+ this.acts[act.Id] = act
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// All actions here...
|
|
|
|
-// MySQL install
|
|
|
|
-// MySQL first user
|
|
|
|
-// User login
|
|
|
|
-// User logout
|
|
|
|
|
|
+func (this *Modules) newModule(WantDB bool, Name string, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper)) *Module {
|
|
|
|
+ return &Module{
|
|
|
|
+ WantDB: WantDB,
|
|
|
|
+ Name: Name,
|
|
|
|
+ FrontEnd: ff,
|
|
|
|
+ BackEnd: bf,
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (this *Modules) newAction(WantDB bool, af func(wrap *wrapper.Wrapper)) *Action {
|
|
|
|
+ return &Action{
|
|
|
|
+ WantDB: WantDB,
|
|
|
|
+ ActFunc: af,
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func New() *Modules {
|
|
|
|
+ m := Modules{
|
|
|
|
+ mods: map[string]*Module{},
|
|
|
|
+ acts: map[string]*Action{},
|
|
|
|
+ }
|
|
|
|
+ m.load()
|
|
|
|
+ return &m
|
|
|
|
+}
|
|
|
|
|
|
-// Called inside goroutine
|
|
|
|
func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
|
|
func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
|
|
- //
|
|
|
|
|
|
+ if wrap.R.Method == "POST" {
|
|
|
|
+ if err := wrap.R.ParseForm(); err == nil {
|
|
|
|
+ name := wrap.R.FormValue("action")
|
|
|
|
+ if name != "" {
|
|
|
|
+ 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")
|
|
|
|
+ if act, ok := this.acts[name]; ok {
|
|
|
|
+ if act.WantDB {
|
|
|
|
+ err := wrap.UseDatabase()
|
|
|
|
+ if err != nil {
|
|
|
|
+ wrap.MsgError(err.Error())
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ act.ActFunc(wrap)
|
|
|
|
+ return true
|
|
|
|
+ } else {
|
|
|
|
+ wrap.MsgError(`This action is not implemented`)
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
|
|
|
|
-// Called inside goroutine
|
|
|
|
func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
|
|
func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
|
|
//
|
|
//
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
|
|
|
|
-// Called inside goroutine
|
|
|
|
func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
|
|
func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
|
|
//
|
|
//
|
|
return false
|
|
return false
|