modules.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package modules
  2. import (
  3. "net/http"
  4. "reflect"
  5. "strings"
  6. "golang-fave/engine/wrapper"
  7. )
  8. type Module struct {
  9. Id string
  10. WantDB bool
  11. Name string
  12. FrontEnd func(wrap *wrapper.Wrapper)
  13. BackEnd func(wrap *wrapper.Wrapper)
  14. }
  15. type Action struct {
  16. Id string
  17. WantDB bool
  18. ActFunc func(wrap *wrapper.Wrapper)
  19. }
  20. type Modules struct {
  21. mods map[string]*Module
  22. acts map[string]*Action
  23. }
  24. func (this *Modules) load() {
  25. t := reflect.TypeOf(this)
  26. for i := 0; i < t.NumMethod(); i++ {
  27. m := t.Method(i)
  28. if strings.HasPrefix(m.Name, "XXX") {
  29. continue
  30. }
  31. if strings.HasPrefix(m.Name, "RegisterModule_") {
  32. id := m.Name[15:]
  33. if _, ok := reflect.TypeOf(this).MethodByName("RegisterModule_" + id); ok {
  34. result := reflect.ValueOf(this).MethodByName("RegisterModule_" + id).Call([]reflect.Value{})
  35. if len(result) >= 1 {
  36. mod := result[0].Interface().(*Module)
  37. mod.Id = id
  38. this.mods[mod.Id] = mod
  39. }
  40. }
  41. }
  42. if strings.HasPrefix(m.Name, "RegisterAction_") {
  43. id := m.Name[15:]
  44. if _, ok := reflect.TypeOf(this).MethodByName("RegisterAction_" + id); ok {
  45. result := reflect.ValueOf(this).MethodByName("RegisterAction_" + id).Call([]reflect.Value{})
  46. if len(result) >= 1 {
  47. act := result[0].Interface().(*Action)
  48. act.Id = id
  49. this.acts[act.Id] = act
  50. }
  51. }
  52. }
  53. }
  54. }
  55. func (this *Modules) newModule(WantDB bool, Name string, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper)) *Module {
  56. return &Module{
  57. WantDB: WantDB,
  58. Name: Name,
  59. FrontEnd: ff,
  60. BackEnd: bf,
  61. }
  62. }
  63. func (this *Modules) newAction(WantDB bool, af func(wrap *wrapper.Wrapper)) *Action {
  64. return &Action{
  65. WantDB: WantDB,
  66. ActFunc: af,
  67. }
  68. }
  69. func New() *Modules {
  70. m := Modules{
  71. mods: map[string]*Module{},
  72. acts: map[string]*Action{},
  73. }
  74. m.load()
  75. return &m
  76. }
  77. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  78. if wrap.R.Method == "POST" {
  79. if err := wrap.R.ParseForm(); err == nil {
  80. name := wrap.R.FormValue("action")
  81. if name != "" {
  82. wrap.W.WriteHeader(http.StatusOK)
  83. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  84. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  85. if act, ok := this.acts[name]; ok {
  86. if act.WantDB {
  87. err := wrap.UseDatabase()
  88. if err != nil {
  89. wrap.MsgError(err.Error())
  90. return true
  91. }
  92. }
  93. act.ActFunc(wrap)
  94. return true
  95. } else {
  96. wrap.MsgError(`This action is not implemented`)
  97. return true
  98. }
  99. }
  100. }
  101. }
  102. return false
  103. }
  104. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  105. //
  106. return false
  107. }
  108. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  109. //
  110. return false
  111. }