backend.go 4.0 KB

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