wrapper.go 4.3 KB

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