modules.go 3.8 KB

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