modules.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package modules
  2. import (
  3. "net/http"
  4. "reflect"
  5. "strings"
  6. "golang-fave/engine/wrapper"
  7. "golang-fave/utils"
  8. )
  9. type MInfo struct {
  10. Id string
  11. WantDB bool
  12. Mount string
  13. Name string
  14. }
  15. type Module struct {
  16. Info MInfo
  17. Front func(wrap *wrapper.Wrapper)
  18. Back func(wrap *wrapper.Wrapper)
  19. }
  20. type AInfo struct {
  21. Id string
  22. WantDB bool
  23. Mount string
  24. }
  25. type Action struct {
  26. Info AInfo
  27. Act func(wrap *wrapper.Wrapper)
  28. }
  29. type Modules struct {
  30. mods map[string]*Module
  31. acts map[string]*Action
  32. }
  33. func (this *Modules) load() {
  34. t := reflect.TypeOf(this)
  35. for i := 0; i < t.NumMethod(); i++ {
  36. m := t.Method(i)
  37. if strings.HasPrefix(m.Name, "XXX") {
  38. continue
  39. }
  40. if strings.HasPrefix(m.Name, "RegisterModule_") {
  41. id := m.Name[15:]
  42. if _, ok := reflect.TypeOf(this).MethodByName("RegisterModule_" + id); ok {
  43. result := reflect.ValueOf(this).MethodByName("RegisterModule_" + id).Call([]reflect.Value{})
  44. if len(result) >= 1 {
  45. mod := result[0].Interface().(*Module)
  46. mod.Info.Id = id
  47. this.mods[mod.Info.Mount] = mod
  48. }
  49. }
  50. }
  51. if strings.HasPrefix(m.Name, "RegisterAction_") {
  52. id := m.Name[15:]
  53. if _, ok := reflect.TypeOf(this).MethodByName("RegisterAction_" + id); ok {
  54. result := reflect.ValueOf(this).MethodByName("RegisterAction_" + id).Call([]reflect.Value{})
  55. if len(result) >= 1 {
  56. act := result[0].Interface().(*Action)
  57. act.Info.Id = id
  58. this.acts[act.Info.Mount] = act
  59. }
  60. }
  61. }
  62. }
  63. }
  64. func (this *Modules) newModule(info MInfo, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper)) *Module {
  65. return &Module{Info: info, Front: ff, Back: bf}
  66. }
  67. func (this *Modules) newAction(info AInfo, af func(wrap *wrapper.Wrapper)) *Action {
  68. return &Action{Info: info, Act: af}
  69. }
  70. func (this *Modules) getCurrentModule(wrap *wrapper.Wrapper) (*Module, string) {
  71. var mod *Module = nil
  72. var modCurr string = ""
  73. // Some module
  74. if len(wrap.UrlArgs) >= 1 {
  75. if m, ok := this.mods[wrap.UrlArgs[0]]; ok {
  76. mod = m
  77. modCurr = wrap.UrlArgs[0]
  78. }
  79. }
  80. // Default module
  81. if mod == nil {
  82. if m, ok := this.mods["index"]; ok {
  83. mod = m
  84. modCurr = "index"
  85. }
  86. }
  87. return mod, modCurr
  88. }
  89. func New() *Modules {
  90. m := Modules{
  91. mods: map[string]*Module{},
  92. acts: map[string]*Action{},
  93. }
  94. m.load()
  95. return &m
  96. }
  97. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  98. if wrap.R.Method == "POST" {
  99. if err := wrap.R.ParseForm(); err == nil {
  100. name := wrap.R.FormValue("action")
  101. if name != "" {
  102. wrap.W.WriteHeader(http.StatusOK)
  103. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  104. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  105. if act, ok := this.acts[name]; ok {
  106. if act.Info.WantDB {
  107. err := wrap.UseDatabase()
  108. if err != nil {
  109. wrap.MsgError(err.Error())
  110. return true
  111. }
  112. defer wrap.DB.Close()
  113. }
  114. act.Act(wrap)
  115. return true
  116. } else {
  117. wrap.MsgError(`This action is not implemented`)
  118. return true
  119. }
  120. }
  121. }
  122. }
  123. return false
  124. }
  125. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  126. mod, cm := this.getCurrentModule(wrap)
  127. if mod != nil {
  128. wrap.CurrModule = cm
  129. if mod.Front != nil {
  130. if mod.Info.WantDB {
  131. err := wrap.UseDatabase()
  132. if err != nil {
  133. utils.SystemErrorPageEngine(wrap.W, err)
  134. return true
  135. }
  136. defer wrap.DB.Close()
  137. }
  138. mod.Front(wrap)
  139. return true
  140. }
  141. }
  142. return false
  143. }
  144. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  145. mod, cm := this.getCurrentModule(wrap)
  146. if mod != nil {
  147. wrap.CurrModule = cm
  148. if mod.Back != nil {
  149. if mod.Info.WantDB {
  150. err := wrap.UseDatabase()
  151. if err != nil {
  152. utils.SystemErrorPageEngine(wrap.W, err)
  153. return true
  154. }
  155. defer wrap.DB.Close()
  156. }
  157. mod.Back(wrap)
  158. return true
  159. }
  160. }
  161. return false
  162. }