backend.go 3.7 KB

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