engine.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) bool {
  18. wrap := wrapper.New(l, w, r, s, host, port, 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. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpMySql, nil)
  45. return true
  46. }
  47. // Separated logic
  48. if !this.Wrap.IsBackend {
  49. // Render frontend
  50. return this.Mods.XXXFrontEnd(this.Wrap)
  51. }
  52. // Backend must use MySQL anyway, so, check and connect
  53. err := this.Wrap.UseDatabase()
  54. if err != nil {
  55. utils.SystemErrorPageEngine(this.Wrap.W, err)
  56. return true
  57. }
  58. defer this.Wrap.DB.Close()
  59. // Render backend
  60. return this.Mods.XXXBackEnd(this.Wrap)
  61. }