modules.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package modules
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "golang-fave/engine/wrapper"
  7. )
  8. type FuncFrontEnd func(mod *Modules, wrap *wrapper.Wrapper)
  9. type FuncBackEnd func(mod *Modules, wrap *wrapper.Wrapper)
  10. type FuncAction func(mod *Modules, wrap *wrapper.Wrapper)
  11. type Module struct {
  12. Id string
  13. Name string
  14. FrontEnd FuncFrontEnd
  15. BackEnd FuncBackEnd
  16. }
  17. type Action struct {
  18. Id string
  19. ActFunc FuncAction
  20. }
  21. type Modules struct {
  22. mods map[string]Module
  23. acts map[string]Action
  24. }
  25. func New() *Modules {
  26. m := Modules{
  27. mods: map[string]Module{},
  28. acts: map[string]Action{},
  29. }
  30. m.load()
  31. return &m
  32. }
  33. func (this *Modules) load() {
  34. // Called before server starts
  35. fmt.Println("Load modules")
  36. fmt.Println("---")
  37. t := reflect.TypeOf(this)
  38. for i := 0; i < t.NumMethod(); i++ {
  39. m := t.Method(i)
  40. if strings.HasPrefix(m.Name, "XXX") {
  41. continue
  42. }
  43. if strings.HasPrefix(m.Name, "RegisterModule_") {
  44. //fmt.Printf("%s\n", m.Name)
  45. id := m.Name[15:]
  46. fmt.Printf("Module (%s)\n", id)
  47. if _, ok := reflect.TypeOf(this).MethodByName("RegisterModule_" + id); ok {
  48. result := reflect.ValueOf(this).MethodByName("RegisterModule_" + id).Call([]reflect.Value{})
  49. if len(result) >= 1 {
  50. //fmt.Printf("--- result -> (%d)\n", len(result))
  51. //fmt.Printf("%v\n", result[0])
  52. //fmt.Printf("%T\n", result)
  53. //mod := result[0]
  54. //mod := result[0]
  55. //fmt.Printf("%v\n", mod)
  56. //fmt.Printf("%s\n", *Module(mod).Id)
  57. mod := result[0].Interface().(*Module)
  58. fmt.Printf("%s\n", mod.Id)
  59. }
  60. } else {
  61. fmt.Printf("Error\n")
  62. }
  63. // Add to array
  64. //mod :=
  65. /*
  66. if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
  67. result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
  68. return result[0].String()
  69. }
  70. */
  71. }
  72. if strings.HasPrefix(m.Name, "RegisterAction_") {
  73. //fmt.Printf("%s\n", m.Name)
  74. id := m.Name[15:]
  75. fmt.Printf("Action (%s)\n", id)
  76. // Add to array
  77. }
  78. }
  79. }
  80. // All actions here...
  81. // MySQL install
  82. // MySQL first user
  83. // User login
  84. // User logout
  85. // Called inside goroutine
  86. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  87. //
  88. return false
  89. }
  90. // Called inside goroutine
  91. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  92. //
  93. return false
  94. }
  95. // Called inside goroutine
  96. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  97. //
  98. return false
  99. }