engine.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package engine
  2. import (
  3. "net/http"
  4. "os"
  5. "strings"
  6. "golang-fave/engine/assets"
  7. "golang-fave/engine/basket"
  8. "golang-fave/engine/cblocks"
  9. "golang-fave/engine/logger"
  10. "golang-fave/engine/modules"
  11. "golang-fave/engine/mysqlpool"
  12. "golang-fave/engine/utils"
  13. "golang-fave/engine/wrapper"
  14. "github.com/vladimirok5959/golang-server-sessions/session"
  15. )
  16. type Engine struct {
  17. Wrap *wrapper.Wrapper
  18. Mods *modules.Modules
  19. }
  20. func Response(mp *mysqlpool.MySqlPool, sb *basket.Basket, l *logger.Logger, m *modules.Modules, w http.ResponseWriter, r *http.Request, s *session.Session, c *cblocks.CacheBlocks, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) bool {
  21. wrap := wrapper.New(l, w, r, s, c, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp, mp, sb)
  22. eng := &Engine{
  23. Wrap: wrap,
  24. Mods: m,
  25. }
  26. return eng.Process()
  27. }
  28. func (this *Engine) Process() bool {
  29. // Request was canceled
  30. if this.contextDone() {
  31. return false
  32. }
  33. this.Wrap.IsBackend = this.Wrap.R.URL.Path == "/cp" || strings.HasPrefix(this.Wrap.R.URL.Path, "/cp/")
  34. this.Wrap.ConfMysqlExists = utils.IsMySqlConfigExists(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
  35. this.Wrap.UrlArgs = append(this.Wrap.UrlArgs, utils.UrlToArray(this.Wrap.R.URL.Path)...)
  36. if this.Wrap.IsBackend && len(this.Wrap.UrlArgs) >= 1 && this.Wrap.UrlArgs[0] == "cp" {
  37. this.Wrap.UrlArgs = this.Wrap.UrlArgs[1:]
  38. }
  39. // Action
  40. if this.Mods.XXXActionFire(this.Wrap) {
  41. return true
  42. }
  43. // Request was canceled
  44. if this.contextDone() {
  45. return false
  46. }
  47. // Redirect to CP for creating MySQL config file
  48. if !this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  49. this.Wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  50. http.Redirect(this.Wrap.W, this.Wrap.R, this.Wrap.R.URL.Scheme+"://"+this.Wrap.R.Host+"/cp/", 302)
  51. return true
  52. }
  53. // Display MySQL install page on backend
  54. if this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  55. // Redirect
  56. if this.redirectFixCpUrl() {
  57. return true
  58. }
  59. // Show mysql settings form
  60. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpMySql, nil, "", "")
  61. return true
  62. }
  63. // Request was canceled
  64. if this.contextDone() {
  65. return false
  66. }
  67. // Check for MySQL connection
  68. err := this.Wrap.UseDatabase()
  69. if err != nil {
  70. utils.SystemErrorPageEngine(this.Wrap.W, err)
  71. return true
  72. }
  73. // Request was canceled
  74. if this.contextDone() {
  75. return false
  76. }
  77. // Separated logic
  78. if !this.Wrap.IsBackend {
  79. // Maintenance mode
  80. if this.Wrap.Config.Engine.Maintenance != 0 {
  81. if this.Wrap.User == nil {
  82. // this.Wrap.UseDatabase()
  83. this.Wrap.LoadSessionUser()
  84. }
  85. if this.Wrap.User == nil {
  86. this.Wrap.RenderFrontEnd("maintenance", nil, http.StatusServiceUnavailable)
  87. return true
  88. }
  89. if this.Wrap.User.A_id <= 0 {
  90. this.Wrap.RenderFrontEnd("maintenance", nil, http.StatusServiceUnavailable)
  91. return true
  92. }
  93. if this.Wrap.User.A_admin <= 0 {
  94. this.Wrap.RenderFrontEnd("maintenance", nil, http.StatusServiceUnavailable)
  95. return true
  96. }
  97. }
  98. // Render frontend
  99. return this.Mods.XXXFrontEnd(this.Wrap)
  100. }
  101. // Request was canceled
  102. if this.contextDone() {
  103. return false
  104. }
  105. // Show login page if need
  106. if this.Wrap.S.GetInt("UserId", 0) <= 0 {
  107. // Redirect
  108. if this.redirectFixCpUrl() {
  109. return true
  110. }
  111. // Show login form
  112. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil, "", "")
  113. return true
  114. }
  115. // Request was canceled
  116. if this.contextDone() {
  117. return false
  118. }
  119. // Try load current user data
  120. if !this.Wrap.LoadSessionUser() {
  121. http.Redirect(this.Wrap.W, this.Wrap.R, "/", 302)
  122. return true
  123. }
  124. // Request was canceled
  125. if this.contextDone() {
  126. return false
  127. }
  128. // Only active admins can use backend
  129. if !(this.Wrap.User.A_admin == 1 && this.Wrap.User.A_active == 1) {
  130. // Redirect
  131. if this.redirectFixCpUrl() {
  132. return true
  133. }
  134. // Show login form
  135. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil, "", "")
  136. return true
  137. }
  138. // Redirect
  139. if this.redirectFixCpUrl() {
  140. return true
  141. }
  142. // Request was canceled
  143. if this.contextDone() {
  144. return false
  145. }
  146. // Render backend
  147. return this.Mods.XXXBackEnd(this.Wrap)
  148. }
  149. func (this *Engine) redirectFixCpUrl() bool {
  150. if len(this.Wrap.R.URL.Path) > 0 && this.Wrap.R.URL.Path[len(this.Wrap.R.URL.Path)-1] != '/' {
  151. http.Redirect(this.Wrap.W, this.Wrap.R, this.Wrap.R.URL.Path+"/"+utils.ExtractGetParams(this.Wrap.R.RequestURI), 302)
  152. return true
  153. }
  154. return false
  155. }
  156. func (this *Engine) contextDone() bool {
  157. select {
  158. case <-this.Wrap.R.Context().Done():
  159. return true
  160. default:
  161. }
  162. return false
  163. }