backend.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. ModuleCurrentAlias string
  29. SidebarLeft template.HTML
  30. Content template.HTML
  31. SidebarRight template.HTML
  32. }
  33. func New(wrapper *wrapper.Wrapper, db *sql.DB, url_args *[]string) *Backend {
  34. return &Backend{wrapper, db, nil, url_args}
  35. }
  36. func (this *Backend) Run() bool {
  37. // Show add user form if no any user in db
  38. var count int
  39. err := this.db.QueryRow("SELECT COUNT(*) FROM `users`;").Scan(&count)
  40. if this.wrapper.EngineErrMsgOnError(err) {
  41. return true
  42. }
  43. if count <= 0 {
  44. return this.wrapper.TmplBackEnd(templates.CpFirstUser, nil)
  45. }
  46. // Login page
  47. if this.wrapper.Session.GetIntDef("UserId", 0) <= 0 {
  48. return this.wrapper.TmplBackEnd(templates.CpLogin, nil)
  49. }
  50. // Load current user, if not, show login page
  51. this.user = &utils.MySql_user{}
  52. 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(
  53. &this.user.A_id, &this.user.A_first_name, &this.user.A_last_name, &this.user.A_email, &this.user.A_password)
  54. if this.wrapper.EngineErrMsgOnError(err) {
  55. return true
  56. }
  57. if this.user.A_id != this.wrapper.Session.GetIntDef("UserId", 0) {
  58. return this.wrapper.TmplBackEnd(templates.CpLogin, nil)
  59. }
  60. // Display cp page
  61. body_class := "cp"
  62. // Get module content here
  63. page_content := ""
  64. page_sb_right := ""
  65. mdl := modules.New(this.wrapper, this.db, this.user, this.urls)
  66. page_sb_left := mdl.GetSidebarLeft()
  67. nav_bar_modules := mdl.GetNavMenuModules()
  68. if mdl.Run() {
  69. page_content = mdl.GetContent()
  70. page_sb_right = mdl.GetSidebarRight()
  71. }
  72. // If right sidebar and content need to show
  73. if page_sb_left != "" {
  74. body_class = body_class + " cp-sidebar-left"
  75. }
  76. if page_content == "" {
  77. body_class = body_class + " cp-404"
  78. page_content = "Panel 404"
  79. }
  80. if page_sb_right != "" {
  81. body_class = body_class + " cp-sidebar-right"
  82. }
  83. // Current module alias
  84. malias := "index"
  85. if len(*this.urls) >= 2 {
  86. malias = (*this.urls)[1]
  87. }
  88. // Render page
  89. page := this.wrapper.TmplParseToString(templates.CpBase, wrapper.TmplDataAll{
  90. System: this.wrapper.TmplGetSystemData(),
  91. Data: TmplData{
  92. Title: "Fave " + constants.ServerVersion,
  93. BodyClasses: body_class,
  94. UserId: this.user.A_id,
  95. UserFirstName: this.user.A_first_name,
  96. UserLastName: this.user.A_last_name,
  97. UserEmail: this.user.A_email,
  98. UserPassword: "",
  99. UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(this.user.A_email) + "?s=80&r=g",
  100. NavBarModules: template.HTML(nav_bar_modules),
  101. ModuleCurrentAlias: malias,
  102. SidebarLeft: template.HTML(page_sb_left),
  103. Content: template.HTML(page_content),
  104. SidebarRight: template.HTML(page_sb_right),
  105. },
  106. })
  107. (*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  108. (*this.wrapper.W).Write([]byte(page))
  109. return true
  110. return false
  111. }