modules.go 3.8 KB

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