engine.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package engine
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "golang-fave/assets"
  9. "golang-fave/engine/wrapper"
  10. "golang-fave/logger"
  11. "golang-fave/utils"
  12. "github.com/vladimirok5959/golang-server-sessions/session"
  13. )
  14. type Engine struct {
  15. Wrap *wrapper.Wrapper
  16. // Actions
  17. // Front-end or Back-end
  18. }
  19. func Response(l *logger.Logger, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) bool {
  20. wrap := wrapper.New(l, w, r, s, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp)
  21. eng := &Engine{Wrap: wrap}
  22. return eng.Process()
  23. }
  24. func (this *Engine) Process() bool {
  25. this.Wrap.IsBackend = this.Wrap.R.URL.Path == "/cp" || strings.HasPrefix(this.Wrap.R.URL.Path, "/cp/")
  26. this.Wrap.ConfMysqlExists = utils.IsMySqlConfigExists(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
  27. // Some system actions here...
  28. // MySQL install
  29. // MySQL first user
  30. // User login
  31. // User logout
  32. // Redirect to CP for creating MySQL config file
  33. if !this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  34. this.Wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  35. http.Redirect(this.Wrap.W, this.Wrap.R, this.Wrap.R.URL.Scheme+"://"+this.Wrap.R.Host+"/cp/", 302)
  36. return true
  37. }
  38. // Display MySQL install page on backend
  39. if this.Wrap.IsBackend && !this.Wrap.ConfMysqlExists {
  40. utils.SystemRenderTemplate(this.Wrap.W, assets.TmplCpMySql, nil)
  41. return true
  42. }
  43. // Read MySQL settings file
  44. mc, err := utils.MySqlConfigRead(this.Wrap.DConfig + string(os.PathSeparator) + "mysql.json")
  45. if err != nil {
  46. utils.SystemErrorPageEngine(this.Wrap.W, err)
  47. return true
  48. }
  49. // Connect to MySQL server
  50. db, err := sql.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
  51. if err != nil {
  52. utils.SystemErrorPageEngine(this.Wrap.W, err)
  53. return true
  54. }
  55. this.Wrap.DB = db
  56. defer db.Close()
  57. err = db.Ping()
  58. // Check if MySQL server alive
  59. if err != nil {
  60. utils.SystemErrorPageEngine(this.Wrap.W, err)
  61. return true
  62. }
  63. // Front-end or back-end here...
  64. if this.Wrap.R.URL.Path == "/" {
  65. this.Wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  66. this.Wrap.W.Header().Set("Content-Type", "text/html")
  67. counter := this.Wrap.S.GetInt("counter", 0)
  68. // this.Wrap.LogAccess(fmt.Sprintf("Counter value was: %d", counter))
  69. this.Wrap.W.Write([]byte(`Logic -> (` + fmt.Sprintf("%d", counter) + `)`))
  70. counter++
  71. this.Wrap.S.SetInt("counter", counter)
  72. // this.Wrap.LogAccess(fmt.Sprintf("Counter value now: %d", counter))
  73. return true
  74. }
  75. return false
  76. }