backend.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/wrapper"
  8. templates "golang-fave/engine/wrapper/resources/templates"
  9. utils "golang-fave/engine/wrapper/utils"
  10. )
  11. type Backend struct {
  12. wrapper *wrapper.Wrapper
  13. db *sql.DB
  14. user *utils.MySql_user
  15. }
  16. type TmplData struct {
  17. Title string
  18. UserId int
  19. UserFirstName string
  20. UserLastName string
  21. UserEmail string
  22. UserPassword string
  23. UserAvatarLink string
  24. SidebarLeft template.HTML
  25. Content template.HTML
  26. SidebarRight template.HTML
  27. }
  28. func New(wrapper *wrapper.Wrapper, db *sql.DB) *Backend {
  29. return &Backend{wrapper, db, nil}
  30. }
  31. func (this *Backend) Run() bool {
  32. // Show add user form if no any user in db
  33. var count int
  34. err := this.db.QueryRow("SELECT COUNT(*) FROM `users`;").Scan(&count)
  35. if this.wrapper.EngineErrMsgOnError(err) {
  36. return true
  37. }
  38. if count <= 0 {
  39. return this.wrapper.TmplBackEnd(templates.CpFirstUser, nil)
  40. }
  41. // Login page
  42. if this.wrapper.Session.GetIntDef("UserId", 0) <= 0 {
  43. return this.wrapper.TmplBackEnd(templates.CpLogin, nil)
  44. }
  45. // Load current user, if not, show login page
  46. this.user = &utils.MySql_user{}
  47. 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(
  48. &this.user.A_id, &this.user.A_first_name, &this.user.A_last_name, &this.user.A_email, &this.user.A_password)
  49. if this.wrapper.EngineErrMsgOnError(err) {
  50. return true
  51. }
  52. if this.user.A_id != this.wrapper.Session.GetIntDef("UserId", 0) {
  53. return this.wrapper.TmplBackEnd(templates.CpLogin, nil)
  54. }
  55. // Display cp page
  56. /*
  57. (*this.wrapper.W).Write([]byte(`Admin panel here...`))
  58. return true
  59. */
  60. // return this.wrapper.TmplBackEnd(templates.CpBase, nil)
  61. /*
  62. tmpl, err := template.New("template").Parse(string(templates.CpBase))
  63. if err == nil {
  64. (*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  65. tmpl.Execute(*this.wrapper.W, wrapper.TmplDataAll{
  66. System: this.wrapper.TmplGetSystemData(),
  67. Data: TmplData{
  68. Title: "Fave " + constants.ServerVersion,
  69. UserEmail: this.user.A_email,
  70. SidebarLeft: "Sidebar left",
  71. Content: "Content",
  72. SidebarRight: "Sidebar right",
  73. },
  74. })
  75. return true
  76. }
  77. */
  78. // Get parsed template as string
  79. // https://coderwall.com/p/ns60fq/simply-output-go-html-template-execution-to-strings
  80. // http://localhost:8080/admin/
  81. sidebar_left := string(`<ul class="nav flex-column">
  82. <li class="nav-item active">
  83. <a class="nav-link" href="#">Pages</a>
  84. <ul class="nav flex-column">
  85. <li class="nav-item active">
  86. <a class="nav-link" href="#">List of pages</a>
  87. </li>
  88. <li class="nav-item">
  89. <a class="nav-link" href="#">Add new page</a>
  90. </li>
  91. </ul>
  92. </li>
  93. <li class="nav-item">
  94. <a class="nav-link" href="#">Link 2</a>
  95. </li>
  96. <li class="nav-item">
  97. <a class="nav-link" href="#">Link 3</a>
  98. </li>
  99. <li class="nav-item">
  100. <a class="nav-link" href="#">Link 4</a>
  101. </li>
  102. </ul>`)
  103. page := this.wrapper.TmplParseToString(templates.CpBase, wrapper.TmplDataAll{
  104. System: this.wrapper.TmplGetSystemData(),
  105. Data: TmplData{
  106. Title: "Fave " + constants.ServerVersion,
  107. UserId: this.user.A_id,
  108. UserFirstName: this.user.A_first_name,
  109. UserLastName: this.user.A_last_name,
  110. UserEmail: this.user.A_email,
  111. UserPassword: "",
  112. UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(this.user.A_email) + "?s=80&r=g",
  113. SidebarLeft: template.HTML(sidebar_left),
  114. Content: template.HTML("Content"),
  115. SidebarRight: template.HTML("Sidebar right"),
  116. },
  117. })
  118. (*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  119. (*this.wrapper.W).Write([]byte(page))
  120. return true
  121. return false
  122. }