backend.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package backend
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "html/template"
  6. "golang-fave/constants"
  7. "golang-fave/engine/backend/modules"
  8. "golang-fave/engine/wrapper"
  9. templates "golang-fave/engine/wrapper/resources/templates"
  10. utils "golang-fave/engine/wrapper/utils"
  11. )
  12. type Backend struct {
  13. wrapper *wrapper.Wrapper
  14. db *sql.DB
  15. user *utils.MySql_user
  16. urls *[]string
  17. }
  18. type TmplData struct {
  19. Title string
  20. BodyClasses string
  21. UserId int
  22. UserFirstName string
  23. UserLastName string
  24. UserEmail string
  25. UserPassword string
  26. UserAvatarLink string
  27. NavBarModules template.HTML
  28. NavBarModulesSys template.HTML
  29. ModuleCurrentAlias string
  30. SidebarLeft template.HTML
  31. Content template.HTML
  32. SidebarRight template.HTML
  33. }
  34. func New(wrapper *wrapper.Wrapper, db *sql.DB, url_args *[]string) *Backend {
  35. return &Backend{wrapper, db, nil, url_args}
  36. }
  37. func (this *Backend) Run() bool {
  38. // Show add user form if no any user in db
  39. var count int
  40. err := this.db.QueryRow("SELECT COUNT(*) FROM `users`;").Scan(&count)
  41. if this.wrapper.EngineErrMsgOnError(err) {
  42. return true
  43. }
  44. if count <= 0 {
  45. return this.wrapper.TmplBackEnd(templates.CpFirstUser, nil)
  46. }
  47. // Login page
  48. if this.wrapper.Session.GetIntDef("UserId", 0) <= 0 {
  49. return this.wrapper.TmplBackEnd(templates.CpLogin, nil)
  50. }
  51. // Load current user, if not, show login page
  52. this.user = &utils.MySql_user{}
  53. err = this.db.QueryRow("SELECT `id`, `first_name`, `last_name`, `email`, `password` FROM `users` WHERE `id` = ? LIMIT 1;", this.wrapper.Session.GetIntDef("UserId", 0)).Scan(
  54. &this.user.A_id, &this.user.A_first_name, &this.user.A_last_name, &this.user.A_email, &this.user.A_password)
  55. if this.wrapper.EngineErrMsgOnError(err) {
  56. return true
  57. }
  58. if this.user.A_id != this.wrapper.Session.GetIntDef("UserId", 0) {
  59. return this.wrapper.TmplBackEnd(templates.CpLogin, nil)
  60. }
  61. // Display cp page
  62. body_class := "cp"
  63. // Get module content here
  64. page_content := ""
  65. page_sb_right := ""
  66. mdl := modules.New(this.wrapper, this.db, this.user, this.urls)
  67. page_sb_left := mdl.GetSidebarLeft()
  68. nav_bar_modules_all := mdl.GetNavMenuModules()
  69. nav_bar_modules_sys := mdl.GetNavMenuModulesSys()
  70. if mdl.Run() {
  71. page_content = mdl.GetContent()
  72. page_sb_right = mdl.GetSidebarRight()
  73. }
  74. // If right sidebar and content need to show
  75. if page_sb_left != "" {
  76. body_class = body_class + " cp-sidebar-left"
  77. }
  78. if page_content == "" {
  79. body_class = body_class + " cp-404"
  80. page_content = "Panel 404"
  81. }
  82. if page_sb_right != "" {
  83. body_class = body_class + " cp-sidebar-right"
  84. }
  85. // Current module alias
  86. malias := "index"
  87. if len(*this.urls) >= 2 {
  88. malias = (*this.urls)[1]
  89. }
  90. // Render page
  91. page := this.wrapper.TmplParseToString(templates.CpBase, wrapper.TmplDataAll{
  92. System: this.wrapper.TmplGetSystemData(),
  93. Data: TmplData{
  94. Title: "Fave " + constants.ServerVersion,
  95. BodyClasses: body_class,
  96. UserId: this.user.A_id,
  97. UserFirstName: this.user.A_first_name,
  98. UserLastName: this.user.A_last_name,
  99. UserEmail: this.user.A_email,
  100. UserPassword: "",
  101. UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(this.user.A_email) + "?s=80&r=g",
  102. NavBarModules: template.HTML(nav_bar_modules_all),
  103. NavBarModulesSys: template.HTML(nav_bar_modules_sys),
  104. ModuleCurrentAlias: malias,
  105. SidebarLeft: template.HTML(page_sb_left),
  106. Content: template.HTML(page_content),
  107. SidebarRight: template.HTML(page_sb_right),
  108. },
  109. })
  110. (*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  111. (*this.wrapper.W).Write([]byte(page))
  112. return true
  113. return false
  114. }