backend.go 3.5 KB

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