wrapper.go 3.5 KB

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