modules.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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, backend bool) (*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. if (!backend && m.Front != nil) || (backend && m.Back != nil) {
  78. mod = m
  79. modCurr = wrap.UrlArgs[0]
  80. }
  81. }
  82. }
  83. // Default module
  84. if mod == nil {
  85. if m, ok := this.mods["index"]; ok {
  86. mod = m
  87. modCurr = "index"
  88. }
  89. }
  90. return mod, modCurr
  91. }
  92. func New() *Modules {
  93. m := Modules{
  94. mods: map[string]*Module{},
  95. acts: map[string]*Action{},
  96. }
  97. m.load()
  98. return &m
  99. }
  100. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  101. if wrap.R.Method == "POST" {
  102. if err := wrap.R.ParseForm(); err == nil {
  103. name := wrap.R.FormValue("action")
  104. if name != "" {
  105. wrap.W.WriteHeader(http.StatusOK)
  106. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  107. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  108. if act, ok := this.acts[name]; ok {
  109. if act.Info.WantDB {
  110. err := wrap.UseDatabase()
  111. if err != nil {
  112. wrap.MsgError(err.Error())
  113. return true
  114. }
  115. defer wrap.DB.Close()
  116. }
  117. if act.Info.WantUser {
  118. if !wrap.LoadSessionUser() {
  119. wrap.MsgError(`You must be loginned to run this action`)
  120. return true
  121. }
  122. }
  123. act.Act(wrap)
  124. return true
  125. } else {
  126. wrap.MsgError(`This action is not implemented`)
  127. return true
  128. }
  129. }
  130. }
  131. }
  132. return false
  133. }
  134. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  135. mod, cm := this.getCurrentModule(wrap, false)
  136. if mod != nil {
  137. wrap.CurrModule = cm
  138. if mod.Front != nil {
  139. if mod.Info.WantDB {
  140. err := wrap.UseDatabase()
  141. if err != nil {
  142. utils.SystemErrorPageEngine(wrap.W, err)
  143. return true
  144. }
  145. defer wrap.DB.Close()
  146. }
  147. mod.Front(wrap)
  148. return true
  149. }
  150. }
  151. return false
  152. }
  153. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  154. mod, cm := this.getCurrentModule(wrap, true)
  155. if mod != nil {
  156. wrap.CurrModule = cm
  157. if mod.Back != nil {
  158. mod.Back(wrap)
  159. return true
  160. }
  161. }
  162. return false
  163. }