modules.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // TODO: add module ordering
  93. func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper) string {
  94. html := ""
  95. for _, mod := range this.mods {
  96. class := ""
  97. if mod.Info.Mount == wrap.CurrModule {
  98. class = " active"
  99. }
  100. html += `<a class="dropdown-item` + class + `" href="/cp/` + mod.Info.Mount + `/">` + mod.Info.Name + `</a>`
  101. }
  102. return html
  103. }
  104. func New() *Modules {
  105. m := Modules{
  106. mods: map[string]*Module{},
  107. acts: map[string]*Action{},
  108. }
  109. m.load()
  110. return &m
  111. }
  112. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  113. if wrap.R.Method == "POST" {
  114. if err := wrap.R.ParseForm(); err == nil {
  115. name := wrap.R.FormValue("action")
  116. if name != "" {
  117. wrap.W.WriteHeader(http.StatusOK)
  118. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  119. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  120. if act, ok := this.acts[name]; ok {
  121. if act.Info.WantDB {
  122. err := wrap.UseDatabase()
  123. if err != nil {
  124. wrap.MsgError(err.Error())
  125. return true
  126. }
  127. defer wrap.DB.Close()
  128. }
  129. if act.Info.WantUser {
  130. if !wrap.LoadSessionUser() {
  131. wrap.MsgError(`You must be loginned to run this action`)
  132. return true
  133. }
  134. }
  135. act.Act(wrap)
  136. return true
  137. } else {
  138. wrap.MsgError(`This action is not implemented`)
  139. return true
  140. }
  141. }
  142. }
  143. }
  144. return false
  145. }
  146. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  147. mod, cm := this.getCurrentModule(wrap, false)
  148. if mod != nil {
  149. wrap.CurrModule = cm
  150. if mod.Front != nil {
  151. if mod.Info.WantDB {
  152. err := wrap.UseDatabase()
  153. if err != nil {
  154. utils.SystemErrorPageEngine(wrap.W, err)
  155. return true
  156. }
  157. defer wrap.DB.Close()
  158. }
  159. mod.Front(wrap)
  160. return true
  161. }
  162. }
  163. return false
  164. }
  165. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  166. mod, cm := this.getCurrentModule(wrap, true)
  167. if mod != nil {
  168. wrap.CurrModule = cm
  169. if mod.Back != nil {
  170. mod.Back(wrap)
  171. return true
  172. }
  173. }
  174. return false
  175. }