engine.go 3.2 KB

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