engine.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package engine
  2. import (
  3. "net/http"
  4. "os"
  5. "strings"
  6. "golang-fave/assets"
  7. "golang-fave/cblocks"
  8. "golang-fave/engine/mysqlpool"
  9. "golang-fave/engine/wrapper"
  10. "golang-fave/logger"
  11. "golang-fave/modules"
  12. "golang-fave/utils"
  13. "github.com/vladimirok5959/golang-server-sessions/session"
  14. )
  15. type Engine struct {
  16. Wrap *wrapper.Wrapper
  17. Mods *modules.Modules
  18. }
  19. func Response(mp *mysqlpool.MySqlPool, 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 {
  20. wrap := wrapper.New(l, w, r, s, c, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp, mp)
  21. eng := &Engine{
  22. Wrap: wrap,
  23. Mods: m,
  24. }
  25. return eng.Process()
  26. }
  27. func (this *Engine) Process() bool {
  28. this.Wrap.IsBackend = this.Wrap.R.URL.Path == "/cp" || strings.HasPrefix(this.Wrap.R.URL.Path, "/cp/")
  29. this.Wrap.ConfMysqlExists = utils.IsMySqlConfigExists(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
  30. this.Wrap.UrlArgs = append(this.Wrap.UrlArgs, utils.UrlToArray(this.Wrap.R.URL.Path)...)
  31. if this.Wrap.IsBackend && len(this.Wrap.UrlArgs) >= 1 && this.Wrap.UrlArgs[0] == "cp" {
  32. this.Wrap.UrlArgs = this.Wrap.UrlArgs[1:]
  33. }
  34. // Action
  35. if this.Mods.XXXActionFire(this.Wrap) {
  36. return true
  37. }
  38. // Redirect to CP for creating MySQL config file
  39. if !this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  40. this.Wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  41. http.Redirect(this.Wrap.W, this.Wrap.R, this.Wrap.R.URL.Scheme+"://"+this.Wrap.R.Host+"/cp/", 302)
  42. return true
  43. }
  44. // Display MySQL install page on backend
  45. if this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  46. // Redirect
  47. if this.redirectFixCpUrl() {
  48. return true
  49. }
  50. // Show mysql settings form
  51. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpMySql, nil, "", "")
  52. return true
  53. }
  54. // Separated logic
  55. if !this.Wrap.IsBackend {
  56. // Render frontend
  57. return this.Mods.XXXFrontEnd(this.Wrap)
  58. }
  59. // Backend must use MySQL anyway, so, check and connect
  60. err := this.Wrap.UseDatabase()
  61. if err != nil {
  62. utils.SystemErrorPageEngine(this.Wrap.W, err)
  63. return true
  64. }
  65. // Show login page if need
  66. if this.Wrap.S.GetInt("UserId", 0) <= 0 {
  67. // Redirect
  68. if this.redirectFixCpUrl() {
  69. return true
  70. }
  71. // Show login form
  72. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil, "", "")
  73. return true
  74. }
  75. // Try load current user data
  76. if !this.Wrap.LoadSessionUser() {
  77. http.Redirect(this.Wrap.W, this.Wrap.R, "/", 302)
  78. return true
  79. }
  80. // Only active admins can use backend
  81. if !(this.Wrap.User.A_admin == 1 && this.Wrap.User.A_active == 1) {
  82. // Redirect
  83. if this.redirectFixCpUrl() {
  84. return true
  85. }
  86. // Show login form
  87. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpLogin, nil, "", "")
  88. return true
  89. }
  90. // Redirect
  91. if this.redirectFixCpUrl() {
  92. return true
  93. }
  94. // Render backend
  95. return this.Mods.XXXBackEnd(this.Wrap)
  96. }
  97. func (this *Engine) redirectFixCpUrl() bool {
  98. if len(this.Wrap.R.URL.Path) > 0 && this.Wrap.R.URL.Path[len(this.Wrap.R.URL.Path)-1] != '/' {
  99. http.Redirect(this.Wrap.W, this.Wrap.R, this.Wrap.R.URL.Path+"/"+utils.ExtractGetParams(this.Wrap.R.RequestURI), 302)
  100. return true
  101. }
  102. return false
  103. }