modules.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package modules
  2. import (
  3. "html/template"
  4. "net/http"
  5. "reflect"
  6. "sort"
  7. "strings"
  8. "golang-fave/assets"
  9. "golang-fave/consts"
  10. "golang-fave/engine/wrapper"
  11. "golang-fave/utils"
  12. )
  13. type MInfo struct {
  14. Id string
  15. WantDB bool
  16. Mount string
  17. Name string
  18. Order int
  19. System bool
  20. }
  21. type Module struct {
  22. Info MInfo
  23. Front func(wrap *wrapper.Wrapper)
  24. Back func(wrap *wrapper.Wrapper) (string, string, string)
  25. }
  26. type AInfo struct {
  27. Id string
  28. WantDB bool
  29. Mount string
  30. WantUser bool
  31. }
  32. type Action struct {
  33. Info AInfo
  34. Act func(wrap *wrapper.Wrapper)
  35. }
  36. type Modules struct {
  37. mods map[string]*Module
  38. acts map[string]*Action
  39. }
  40. func (this *Modules) load() {
  41. t := reflect.TypeOf(this)
  42. for i := 0; i < t.NumMethod(); i++ {
  43. m := t.Method(i)
  44. if strings.HasPrefix(m.Name, "XXX") {
  45. continue
  46. }
  47. if strings.HasPrefix(m.Name, "RegisterModule_") {
  48. id := m.Name[15:]
  49. if _, ok := reflect.TypeOf(this).MethodByName("RegisterModule_" + id); ok {
  50. result := reflect.ValueOf(this).MethodByName("RegisterModule_" + id).Call([]reflect.Value{})
  51. if len(result) >= 1 {
  52. mod := result[0].Interface().(*Module)
  53. mod.Info.Id = id
  54. this.mods[mod.Info.Mount] = mod
  55. }
  56. }
  57. }
  58. if strings.HasPrefix(m.Name, "RegisterAction_") {
  59. id := m.Name[15:]
  60. if _, ok := reflect.TypeOf(this).MethodByName("RegisterAction_" + id); ok {
  61. result := reflect.ValueOf(this).MethodByName("RegisterAction_" + id).Call([]reflect.Value{})
  62. if len(result) >= 1 {
  63. act := result[0].Interface().(*Action)
  64. act.Info.Id = id
  65. this.acts[act.Info.Mount] = act
  66. }
  67. }
  68. }
  69. }
  70. }
  71. func (this *Modules) newModule(info MInfo, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper) (string, string, string)) *Module {
  72. return &Module{Info: info, Front: ff, Back: bf}
  73. }
  74. func (this *Modules) newAction(info AInfo, af func(wrap *wrapper.Wrapper)) *Action {
  75. return &Action{Info: info, Act: af}
  76. }
  77. func (this *Modules) getCurrentModule(wrap *wrapper.Wrapper, backend bool) (*Module, string) {
  78. var mod *Module = nil
  79. var modCurr string = ""
  80. // Some module
  81. if len(wrap.UrlArgs) >= 1 {
  82. if m, ok := this.mods[wrap.UrlArgs[0]]; ok {
  83. if (!backend && m.Front != nil) || (backend && m.Back != nil) {
  84. mod = m
  85. modCurr = wrap.UrlArgs[0]
  86. }
  87. }
  88. }
  89. // Default module
  90. if mod == nil {
  91. if m, ok := this.mods["index"]; ok {
  92. mod = m
  93. modCurr = "index"
  94. }
  95. }
  96. return mod, modCurr
  97. }
  98. func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper, sys bool) string {
  99. list := make([]*MInfo, 0)
  100. for _, mod := range this.mods {
  101. if mod.Back != nil {
  102. if mod.Info.System == sys {
  103. list = append(list, &mod.Info)
  104. }
  105. }
  106. }
  107. sort.Slice(list, func(i, j int) bool {
  108. return list[i].Order < list[j].Order
  109. })
  110. html := ""
  111. for _, mod := range list {
  112. class := ""
  113. if mod.Mount == wrap.CurrModule {
  114. class = " active"
  115. }
  116. html += `<a class="dropdown-item` + class + `" href="/cp/` + mod.Mount + `/">` + mod.Name + `</a>`
  117. }
  118. return html
  119. }
  120. func New() *Modules {
  121. m := Modules{
  122. mods: map[string]*Module{},
  123. acts: map[string]*Action{},
  124. }
  125. m.load()
  126. return &m
  127. }
  128. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  129. if wrap.R.Method == "POST" {
  130. if err := wrap.R.ParseForm(); err == nil {
  131. name := wrap.R.FormValue("action")
  132. if name != "" {
  133. wrap.W.WriteHeader(http.StatusOK)
  134. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  135. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  136. if act, ok := this.acts[name]; ok {
  137. if act.Info.WantDB {
  138. err := wrap.UseDatabase()
  139. if err != nil {
  140. wrap.MsgError(err.Error())
  141. return true
  142. }
  143. defer wrap.DB.Close()
  144. }
  145. if act.Info.WantUser {
  146. if !wrap.LoadSessionUser() {
  147. wrap.MsgError(`You must be loginned to run this action`)
  148. return true
  149. }
  150. }
  151. act.Act(wrap)
  152. return true
  153. } else {
  154. wrap.MsgError(`This action is not implemented`)
  155. return true
  156. }
  157. }
  158. }
  159. }
  160. return false
  161. }
  162. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  163. mod, cm := this.getCurrentModule(wrap, false)
  164. if mod != nil {
  165. wrap.CurrModule = cm
  166. if mod.Front != nil {
  167. if mod.Info.WantDB {
  168. err := wrap.UseDatabase()
  169. if err != nil {
  170. utils.SystemErrorPageEngine(wrap.W, err)
  171. return true
  172. }
  173. defer wrap.DB.Close()
  174. }
  175. mod.Front(wrap)
  176. return true
  177. }
  178. }
  179. return false
  180. }
  181. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  182. mod, cm := this.getCurrentModule(wrap, true)
  183. if mod != nil {
  184. wrap.CurrModule = cm
  185. if mod.Back != nil {
  186. sidebar_left, content, sidebar_right := mod.Back(wrap)
  187. body_class := "cp"
  188. if sidebar_left != "" {
  189. body_class = body_class + " cp-sidebar-left"
  190. }
  191. if content == "" {
  192. body_class = body_class + " cp-404"
  193. content = "Panel 404"
  194. }
  195. if sidebar_right != "" {
  196. body_class = body_class + " cp-sidebar-right"
  197. }
  198. wrap.RenderBackEnd(assets.TmplCpBase, consts.TmplDataCpBase{
  199. Title: "Fave " + consts.ServerVersion,
  200. BodyClasses: body_class,
  201. UserId: wrap.User.A_id,
  202. UserFirstName: wrap.User.A_first_name,
  203. UserLastName: wrap.User.A_last_name,
  204. UserEmail: wrap.User.A_email,
  205. UserPassword: "",
  206. UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(wrap.User.A_email) + "?s=80&r=g",
  207. NavBarModules: template.HTML(this.getNavMenuModules(wrap, false)),
  208. NavBarModulesSys: template.HTML(this.getNavMenuModules(wrap, true)),
  209. ModuleCurrentAlias: wrap.CurrModule,
  210. SidebarLeft: template.HTML(sidebar_left),
  211. Content: template.HTML(content),
  212. SidebarRight: template.HTML(sidebar_right),
  213. })
  214. return true
  215. }
  216. }
  217. return false
  218. }