engine.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package engine
  2. import (
  3. "net/http"
  4. "os"
  5. "strings"
  6. "golang-fave/assets"
  7. "golang-fave/engine/wrapper"
  8. "golang-fave/logger"
  9. "golang-fave/modules"
  10. "golang-fave/utils"
  11. "github.com/vladimirok5959/golang-server-sessions/session"
  12. )
  13. type Engine struct {
  14. Wrap *wrapper.Wrapper
  15. Mods *modules.Modules
  16. }
  17. func Response(l *logger.Logger, m *modules.Modules, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) bool {
  18. wrap := wrapper.New(l, w, r, s, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp)
  19. eng := &Engine{
  20. Wrap: wrap,
  21. Mods: m,
  22. }
  23. return eng.Process()
  24. }
  25. func (this *Engine) Process() bool {
  26. // Check and set session user
  27. if !this.Wrap.S.IsSetInt("UserId") {
  28. this.Wrap.S.SetInt("UserId", 0)
  29. }
  30. this.Wrap.IsBackend = this.Wrap.R.URL.Path == "/cp" || strings.HasPrefix(this.Wrap.R.URL.Path, "/cp/")
  31. this.Wrap.ConfMysqlExists = utils.IsMySqlConfigExists(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
  32. this.Wrap.UrlArgs = append(this.Wrap.UrlArgs, utils.UrlToArray(this.Wrap.R.URL.Path)...)
  33. if this.Wrap.IsBackend && len(this.Wrap.UrlArgs) >= 1 && this.Wrap.UrlArgs[0] == "cp" {
  34. this.Wrap.UrlArgs = this.Wrap.UrlArgs[1:]
  35. }
  36. // Action
  37. if this.Mods.XXXActionFire(this.Wrap) {
  38. return true
  39. }
  40. // Redirect to CP for creating MySQL config file
  41. if !this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  42. this.Wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  43. http.Redirect(this.Wrap.W, this.Wrap.R, this.Wrap.R.URL.Scheme+"://"+this.Wrap.R.Host+"/cp/", 302)
  44. return true
  45. }
  46. // Display MySQL install page on backend
  47. if this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  48. // Redirect
  49. if this.redirectFixCpUrl() {
  50. return true
  51. }
  52. // Show mysql settings form
  53. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpMySql, nil)
  54. return true
  55. }
  56. // Separated logic
  57. if !this.Wrap.IsBackend {
  58. // Render frontend
  59. return this.Mods.XXXFrontEnd(this.Wrap)
  60. }
  61. // Backend must use MySQL anyway, so, check and connect
  62. err := this.Wrap.UseDatabase()
  63. if err != nil {
  64. utils.SystemErrorPageEngine(this.Wrap.W, err)
  65. return true
  66. }
  67. defer this.Wrap.DB.Close()
  68. // Show add first user form if no any user in database
  69. if !utils.IsFileExists(this.Wrap.DConfig + string(os.PathSeparator) + ".installed") {
  70. var count int
  71. err = this.Wrap.DB.QueryRow(`
  72. SELECT
  73. COUNT(*)
  74. FROM
  75. users
  76. ;`,
  77. ).Scan(
  78. &count,
  79. )
  80. if err != nil {
  81. utils.SystemErrorPageEngine(this.Wrap.W, err)
  82. return true
  83. }
  84. if count <= 0 {
  85. // Redirect
  86. if this.redirectFixCpUrl() {
  87. return true
  88. }
  89. // Show first user form
  90. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpFirstUser, nil)
  91. return true
  92. } else {
  93. // Mark what one or more users already exists
  94. if _, err := os.OpenFile(this.Wrap.DConfig+string(os.PathSeparator)+".installed", os.O_RDONLY|os.O_CREATE, 0666); err != nil {
  95. this.Wrap.LogError(err.Error())
  96. }
  97. }
  98. }
  99. // Show login page if need
  100. if this.Wrap.S.GetInt("UserId", 0) <= 0 {
  101. // Redirect
  102. if this.redirectFixCpUrl() {
  103. return true
  104. }
  105. // Show login form
  106. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil)
  107. return true
  108. }
  109. // Try load current user data
  110. if !this.Wrap.LoadSessionUser() {
  111. http.Redirect(this.Wrap.W, this.Wrap.R, "/", 302)
  112. return true
  113. }
  114. // Only active admins can use backend
  115. if !(this.Wrap.User.A_admin == 1 && this.Wrap.User.A_active == 1) {
  116. // Redirect
  117. if this.redirectFixCpUrl() {
  118. return true
  119. }
  120. // Show login form
  121. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil)
  122. return true
  123. }
  124. // Redirect
  125. if this.redirectFixCpUrl() {
  126. return true
  127. }
  128. // Render backend
  129. return this.Mods.XXXBackEnd(this.Wrap)
  130. }
  131. func (this *Engine) redirectFixCpUrl() bool {
  132. if len(this.Wrap.R.URL.Path) > 0 && this.Wrap.R.URL.Path[len(this.Wrap.R.URL.Path)-1] != '/' {
  133. http.Redirect(this.Wrap.W, this.Wrap.R, this.Wrap.R.URL.Path+"/"+utils.ExtractGetParams(this.Wrap.R.RequestURI), 302)
  134. return true
  135. }
  136. return false
  137. }