modules.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 New() *Modules {
  76. m := Modules{
  77. mods: map[string]*Module{},
  78. acts: map[string]*Action{},
  79. }
  80. m.load()
  81. return &m
  82. }
  83. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  84. if wrap.R.Method == "POST" {
  85. if err := wrap.R.ParseForm(); err == nil {
  86. name := wrap.R.FormValue("action")
  87. if name != "" {
  88. wrap.W.WriteHeader(http.StatusOK)
  89. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  90. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  91. if act, ok := this.acts[name]; ok {
  92. if act.WantDB {
  93. err := wrap.UseDatabase()
  94. if err != nil {
  95. wrap.MsgError(err.Error())
  96. return true
  97. }
  98. defer wrap.DB.Close()
  99. }
  100. act.ActFunc(wrap)
  101. return true
  102. } else {
  103. wrap.MsgError(`This action is not implemented`)
  104. return true
  105. }
  106. }
  107. }
  108. }
  109. return false
  110. }
  111. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  112. var mod *Module = nil
  113. // Some module
  114. if len(wrap.UrlArgs) > 0 {
  115. if m, ok := this.mods[wrap.UrlArgs[0]]; ok {
  116. mod = m
  117. }
  118. }
  119. // Default module
  120. if mod == nil {
  121. if m, ok := this.mods["index"]; ok {
  122. mod = m
  123. }
  124. }
  125. // Check and run
  126. if mod != nil {
  127. if mod.FrontEnd != nil {
  128. if mod.WantDB {
  129. err := wrap.UseDatabase()
  130. if err != nil {
  131. utils.SystemErrorPageEngine(wrap.W, err)
  132. return true
  133. }
  134. defer wrap.DB.Close()
  135. }
  136. mod.FrontEnd(wrap)
  137. return true
  138. }
  139. }
  140. return false
  141. }
  142. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  143. //fmt.Printf("Back: %v\n", wrap.UrlArgs)
  144. return false
  145. }