wrapper.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package wrapper
  2. import (
  3. "bytes"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "html/template"
  8. "net/http"
  9. "os"
  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. CurrHost 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, chost, 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. CurrHost: chost,
  46. DConfig: dirConfig,
  47. DHtdocs: dirHtdocs,
  48. DLogs: dirLogs,
  49. DTemplate: dirTemplate,
  50. DTmp: dirTmp,
  51. UrlArgs: []string{},
  52. CurrModule: "",
  53. CurrSubModule: "",
  54. }
  55. }
  56. func (this *Wrapper) LogAccess(msg string) {
  57. this.l.Log(msg, this.R, false)
  58. }
  59. func (this *Wrapper) LogError(msg string) {
  60. this.l.Log(msg, this.R, true)
  61. }
  62. func (this *Wrapper) UseDatabase() error {
  63. if this.DB != nil {
  64. return errors.New("already connected to database")
  65. }
  66. if !utils.IsMySqlConfigExists(this.DConfig + string(os.PathSeparator) + "mysql.json") {
  67. return errors.New("can't read database configuration file")
  68. }
  69. mc, err := utils.MySqlConfigRead(this.DConfig + string(os.PathSeparator) + "mysql.json")
  70. if err != nil {
  71. return err
  72. }
  73. this.DB, err = sql.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
  74. if err != nil {
  75. return err
  76. }
  77. err = this.DB.Ping()
  78. if err != nil {
  79. this.DB.Close()
  80. return err
  81. }
  82. return nil
  83. }
  84. func (this *Wrapper) LoadSessionUser() bool {
  85. if this.S.GetInt("UserId", 0) <= 0 {
  86. return false
  87. }
  88. if this.DB == nil {
  89. return false
  90. }
  91. user := &utils.MySql_user{}
  92. err := this.DB.QueryRow(`
  93. SELECT
  94. id,
  95. first_name,
  96. last_name,
  97. email,
  98. password,
  99. admin,
  100. active
  101. FROM
  102. users
  103. WHERE
  104. id = ?
  105. LIMIT 1;`,
  106. this.S.GetInt("UserId", 0),
  107. ).Scan(
  108. &user.A_id,
  109. &user.A_first_name,
  110. &user.A_last_name,
  111. &user.A_email,
  112. &user.A_password,
  113. &user.A_admin,
  114. &user.A_active,
  115. )
  116. if err != nil {
  117. return false
  118. }
  119. if user.A_id != this.S.GetInt("UserId", 0) {
  120. return false
  121. }
  122. this.User = user
  123. return true
  124. }
  125. func (this *Wrapper) Write(data string) {
  126. this.W.Write([]byte(data))
  127. }
  128. func (this *Wrapper) MsgSuccess(msg string) {
  129. this.Write(fmt.Sprintf(
  130. `fave.ShowMsgSuccess('Success!', '%s', false);`,
  131. utils.JavaScriptVarValue(msg)))
  132. }
  133. func (this *Wrapper) MsgError(msg string) {
  134. this.Write(fmt.Sprintf(
  135. `fave.ShowMsgError('Error!', '%s', true);`,
  136. utils.JavaScriptVarValue(msg)))
  137. }
  138. func (this *Wrapper) RenderToString(tcont []byte, data interface{}) string {
  139. tmpl, err := template.New("template").Parse(string(tcont))
  140. if err != nil {
  141. return err.Error()
  142. }
  143. var tpl bytes.Buffer
  144. if err := tmpl.Execute(&tpl, data); err != nil {
  145. return err.Error()
  146. }
  147. return tpl.String()
  148. }
  149. func (this *Wrapper) RenderFrontEnd(tname string, data interface{}) {
  150. tmpl, err := template.ParseFiles(
  151. this.DTemplate+string(os.PathSeparator)+tname+".html",
  152. this.DTemplate+string(os.PathSeparator)+"header.html",
  153. this.DTemplate+string(os.PathSeparator)+"sidebar.html",
  154. this.DTemplate+string(os.PathSeparator)+"footer.html",
  155. )
  156. if err != nil {
  157. utils.SystemErrorPageTemplate(this.W, err)
  158. return
  159. }
  160. this.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  161. this.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  162. var tpl bytes.Buffer
  163. err = tmpl.Execute(&tpl, consts.TmplData{
  164. System: utils.GetTmplSystemData(),
  165. Data: data,
  166. })
  167. if err != nil {
  168. utils.SystemErrorPageTemplate(this.W, err)
  169. return
  170. }
  171. this.W.Write(tpl.Bytes())
  172. }
  173. func (this *Wrapper) RenderBackEnd(tcont []byte, data interface{}) {
  174. tmpl, err := template.New("template").Parse(string(tcont))
  175. if err != nil {
  176. utils.SystemErrorPageEngine(this.W, err)
  177. return
  178. }
  179. this.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  180. this.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  181. var tpl bytes.Buffer
  182. err = tmpl.Execute(this.W, consts.TmplData{
  183. System: utils.GetTmplSystemData(),
  184. Data: data,
  185. })
  186. if err != nil {
  187. utils.SystemErrorPageEngine(this.W, err)
  188. return
  189. }
  190. this.W.Write(tpl.Bytes())
  191. }