engine.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package engine
  2. import (
  3. //"database/sql"
  4. "net/http"
  5. "os"
  6. "strings"
  7. "golang-fave/assets"
  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(l *logger.Logger, m *modules.Modules, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) bool {
  19. wrap := wrapper.New(l, w, r, s, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp)
  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. //
  38. // TODO: make a redirect or display error page?
  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. //
  47. // TODO: show or not show configure MySQL form?
  48. //
  49. // Display MySQL install page on backend
  50. if this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  51. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpMySql, nil)
  52. return true
  53. }
  54. /*
  55. // Read MySQL settings file
  56. mc, err := utils.MySqlConfigRead(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
  57. if err != nil {
  58. utils.SystemErrorPageEngine(this.Wrap.W, err)
  59. return true
  60. }
  61. // Connect to MySQL server
  62. db, err := sql.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
  63. if err != nil {
  64. utils.SystemErrorPageEngine(this.Wrap.W, err)
  65. return true
  66. }
  67. this.Wrap.DB = db
  68. defer db.Close()
  69. err = db.Ping()
  70. // Check if MySQL server alive
  71. if err != nil {
  72. utils.SystemErrorPageEngine(this.Wrap.W, err)
  73. return true
  74. }
  75. */
  76. // Separated logic
  77. if this.Wrap.IsBackend {
  78. return this.Mods.XXXBackEnd(this.Wrap)
  79. }
  80. return this.Mods.XXXFrontEnd(this.Wrap)
  81. }