modules.go 3.8 KB

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