engine.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. this.Wrap.IsBackend = this.Wrap.R.URL.Path == "/cp" || strings.HasPrefix(this.Wrap.R.URL.Path, "/cp/")
  27. this.Wrap.ConfMysqlExists = utils.IsMySqlConfigExists(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
  28. this.Wrap.UrlArgs = append(this.Wrap.UrlArgs, utils.UrlToArray(this.Wrap.R.URL.Path)...)
  29. if this.Wrap.IsBackend && len(this.Wrap.UrlArgs) >= 1 && this.Wrap.UrlArgs[0] == "cp" {
  30. this.Wrap.UrlArgs = this.Wrap.UrlArgs[1:]
  31. }
  32. // Action
  33. if this.Mods.XXXActionFire(this.Wrap) {
  34. return true
  35. }
  36. // Redirect to CP for creating MySQL config file
  37. if !this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  38. this.Wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  39. http.Redirect(this.Wrap.W, this.Wrap.R, this.Wrap.R.URL.Scheme+"://"+this.Wrap.R.Host+"/cp/", 302)
  40. return true
  41. }
  42. // Display MySQL install page on backend
  43. if this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  44. // Redirect
  45. if this.redirectFixCpUrl() {
  46. return true
  47. }
  48. // Show mysql settings form
  49. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpMySql, nil)
  50. return true
  51. }
  52. // Separated logic
  53. if !this.Wrap.IsBackend {
  54. // Render frontend
  55. return this.Mods.XXXFrontEnd(this.Wrap)
  56. }
  57. // Backend must use MySQL anyway, so, check and connect
  58. err := this.Wrap.UseDatabase()
  59. if err != nil {
  60. utils.SystemErrorPageEngine(this.Wrap.W, err)
  61. return true
  62. }
  63. defer this.Wrap.DB.Close()
  64. // Show add first user form if no any user in database
  65. if !utils.IsFileExists(this.Wrap.DConfig + string(os.PathSeparator) + ".installed") {
  66. var count int
  67. err = this.Wrap.DB.QueryRow(`
  68. SELECT
  69. COUNT(*)
  70. FROM
  71. users
  72. ;`,
  73. ).Scan(
  74. &count,
  75. )
  76. if err != nil {
  77. utils.SystemErrorPageEngine(this.Wrap.W, err)
  78. return true
  79. }
  80. if count <= 0 {
  81. // Redirect
  82. if this.redirectFixCpUrl() {
  83. return true
  84. }
  85. // Show first user form
  86. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpFirstUser, nil)
  87. return true
  88. } else {
  89. // Mark what one or more users already exists
  90. if _, err := os.OpenFile(this.Wrap.DConfig+string(os.PathSeparator)+".installed", os.O_RDONLY|os.O_CREATE, 0666); err != nil {
  91. this.Wrap.LogError(err.Error())
  92. }
  93. }
  94. }
  95. // Show login page if need
  96. if this.Wrap.S.GetInt("UserId", 0) <= 0 {
  97. // Redirect
  98. if this.redirectFixCpUrl() {
  99. return true
  100. }
  101. // Show login form
  102. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil)
  103. return true
  104. }
  105. // Try load current user data
  106. if !this.Wrap.LoadSessionUser() {
  107. http.Redirect(this.Wrap.W, this.Wrap.R, "/", 302)
  108. return true
  109. }
  110. // Only active admins can use backend
  111. if !(this.Wrap.User.A_admin == 1 && this.Wrap.User.A_active == 1) {
  112. // Redirect
  113. if this.redirectFixCpUrl() {
  114. return true
  115. }
  116. // Show login form
  117. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil)
  118. return true
  119. }
  120. // Redirect
  121. if this.redirectFixCpUrl() {
  122. return true
  123. }
  124. // Render backend
  125. return this.Mods.XXXBackEnd(this.Wrap)
  126. }
  127. func (this *Engine) redirectFixCpUrl() bool {
  128. if len(this.Wrap.R.URL.Path) > 0 && this.Wrap.R.URL.Path[len(this.Wrap.R.URL.Path)-1] != '/' {
  129. http.Redirect(this.Wrap.W, this.Wrap.R, this.Wrap.R.URL.Path+"/"+utils.ExtractGetParams(this.Wrap.R.RequestURI), 302)
  130. return true
  131. }
  132. return false
  133. }