wrapper.go 4.3 KB

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