backend.go 3.2 KB

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