wrapper.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package wrapper
  2. import (
  3. "bytes"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "html/template"
  8. "net/http"
  9. "os"
  10. "strings"
  11. "golang-fave/consts"
  12. "golang-fave/logger"
  13. "golang-fave/utils"
  14. _ "github.com/go-sql-driver/mysql"
  15. "github.com/vladimirok5959/golang-server-sessions/session"
  16. )
  17. type Wrapper struct {
  18. l *logger.Logger
  19. W http.ResponseWriter
  20. R *http.Request
  21. S *session.Session
  22. Host string
  23. Port string
  24. DConfig string
  25. DHtdocs string
  26. DLogs string
  27. DTemplate string
  28. DTmp string
  29. IsBackend bool
  30. ConfMysqlExists bool
  31. UrlArgs []string
  32. CurrModule string
  33. DB *sql.DB
  34. User *utils.MySql_user
  35. }
  36. func New(l *logger.Logger, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) *Wrapper {
  37. return &Wrapper{
  38. l: l,
  39. W: w,
  40. R: r,
  41. S: s,
  42. Host: host,
  43. Port: port,
  44. DConfig: dirConfig,
  45. DHtdocs: dirHtdocs,
  46. DLogs: dirLogs,
  47. DTemplate: dirTemplate,
  48. DTmp: dirTmp,
  49. UrlArgs: []string{},
  50. CurrModule: "",
  51. }
  52. }
  53. func (this *Wrapper) LogAccess(msg string) {
  54. this.l.Log(msg, this.R, false)
  55. }
  56. func (this *Wrapper) LogError(msg string) {
  57. this.l.Log(msg, this.R, true)
  58. }
  59. func (this *Wrapper) UseDatabase() error {
  60. if this.DB != nil {
  61. return errors.New("already connected to database")
  62. }
  63. if !utils.IsMySqlConfigExists(this.DConfig + string(os.PathSeparator) + "mysql.json") {
  64. return errors.New("can't read database configuration file")
  65. }
  66. mc, err := utils.MySqlConfigRead(this.DConfig + string(os.PathSeparator) + "mysql.json")
  67. if err != nil {
  68. return err
  69. }
  70. this.DB, err = sql.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
  71. if err != nil {
  72. return err
  73. }
  74. err = this.DB.Ping()
  75. if err != nil {
  76. this.DB.Close()
  77. return err
  78. }
  79. return nil
  80. }
  81. func (this *Wrapper) LoadSessionUser() bool {
  82. if this.S.GetInt("UserId", 0) <= 0 {
  83. return false
  84. }
  85. if this.DB == nil {
  86. return false
  87. }
  88. user := &utils.MySql_user{}
  89. err := this.DB.QueryRow("SELECT `id`, `first_name`, `last_name`, `email`, `password` FROM `users` WHERE `id` = ? LIMIT 1;", this.S.GetInt("UserId", 0)).Scan(
  90. &user.A_id, &user.A_first_name, &user.A_last_name, &user.A_email, &user.A_password)
  91. if err != nil {
  92. return false
  93. }
  94. if user.A_id != this.S.GetInt("UserId", 0) {
  95. return false
  96. }
  97. this.User = user
  98. return true
  99. }
  100. func (this *Wrapper) Write(data string) {
  101. this.W.Write([]byte(data))
  102. }
  103. func (this *Wrapper) MsgSuccess(msg string) {
  104. this.Write(fmt.Sprintf(
  105. `ShowSystemMsgSuccess('Success!', '%s', false);`,
  106. strings.Replace(strings.Replace(msg, `'`, `&rsquo;`, -1), `"`, `&rdquo;`, -1)))
  107. }
  108. func (this *Wrapper) MsgError(msg string) {
  109. this.Write(fmt.Sprintf(
  110. `ShowSystemMsgError('Error!', '%s', true);`,
  111. strings.Replace(strings.Replace(msg, `'`, `&rsquo;`, -1), `"`, `&rdquo;`, -1)))
  112. }
  113. func (this *Wrapper) RenderToString(tcont []byte, data interface{}) string {
  114. tmpl, err := template.New("template").Parse(string(tcont))
  115. if err != nil {
  116. return err.Error()
  117. }
  118. var tpl bytes.Buffer
  119. if err := tmpl.Execute(&tpl, data); err != nil {
  120. return err.Error()
  121. }
  122. return tpl.String()
  123. }
  124. func (this *Wrapper) RenderFrontEnd(tname string, data interface{}) {
  125. tmpl, err := template.ParseFiles(
  126. this.DTemplate+string(os.PathSeparator)+tname+".html",
  127. this.DTemplate+string(os.PathSeparator)+"header.html",
  128. this.DTemplate+string(os.PathSeparator)+"sidebar.html",
  129. this.DTemplate+string(os.PathSeparator)+"footer.html",
  130. )
  131. if err != nil {
  132. utils.SystemErrorPageEngine(this.W, err)
  133. return
  134. }
  135. this.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  136. this.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  137. tmpl.Execute(this.W, consts.TmplData{
  138. System: utils.GetTmplSystemData(),
  139. Data: data,
  140. })
  141. }
  142. func (this *Wrapper) RenderBackEnd(tcont []byte, data interface{}) {
  143. tmpl, err := template.New("template").Parse(string(tcont))
  144. if err != nil {
  145. utils.SystemErrorPageEngine(this.W, err)
  146. return
  147. }
  148. this.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  149. this.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  150. tmpl.Execute(this.W, consts.TmplData{
  151. System: utils.GetTmplSystemData(),
  152. Data: data,
  153. })
  154. }