wrapper.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package wrapper
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "bytes"
  6. "errors"
  7. "fmt"
  8. "html/template"
  9. "net/http"
  10. "os"
  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(`
  94. SELECT
  95. id,
  96. first_name,
  97. last_name,
  98. email,
  99. password,
  100. admin,
  101. active
  102. FROM
  103. users
  104. WHERE
  105. id = ?
  106. LIMIT 1;`,
  107. this.S.GetInt("UserId", 0),
  108. ).Scan(
  109. &user.A_id,
  110. &user.A_first_name,
  111. &user.A_last_name,
  112. &user.A_email,
  113. &user.A_password,
  114. &user.A_admin,
  115. &user.A_active,
  116. )
  117. if err != nil {
  118. return false
  119. }
  120. if user.A_id != this.S.GetInt("UserId", 0) {
  121. return false
  122. }
  123. this.User = user
  124. return true
  125. }
  126. func (this *Wrapper) Write(data string) {
  127. this.W.Write([]byte(data))
  128. }
  129. func (this *Wrapper) MsgSuccess(msg string) {
  130. this.Write(fmt.Sprintf(
  131. `fave.ShowMsgSuccess('Success!', '%s', false);`,
  132. utils.JavaScriptVarValue(msg)))
  133. }
  134. func (this *Wrapper) MsgError(msg string) {
  135. this.Write(fmt.Sprintf(
  136. `fave.ShowMsgError('Error!', '%s', true);`,
  137. utils.JavaScriptVarValue(msg)))
  138. }
  139. func (this *Wrapper) RenderToString(tcont []byte, data interface{}) string {
  140. tmpl, err := template.New("template").Parse(string(tcont))
  141. if err != nil {
  142. return err.Error()
  143. }
  144. var tpl bytes.Buffer
  145. if err := tmpl.Execute(&tpl, data); err != nil {
  146. return err.Error()
  147. }
  148. return tpl.String()
  149. }
  150. func (this *Wrapper) RenderFrontEnd(tname string, data interface{}) {
  151. tmpl, err := template.ParseFiles(
  152. this.DTemplate+string(os.PathSeparator)+tname+".html",
  153. this.DTemplate+string(os.PathSeparator)+"header.html",
  154. this.DTemplate+string(os.PathSeparator)+"sidebar-left.html",
  155. this.DTemplate+string(os.PathSeparator)+"sidebar-right.html",
  156. this.DTemplate+string(os.PathSeparator)+"footer.html",
  157. )
  158. if err != nil {
  159. utils.SystemErrorPageTemplate(this.W, err)
  160. return
  161. }
  162. this.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  163. this.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  164. var tpl bytes.Buffer
  165. err = tmpl.Execute(&tpl, consts.TmplData{
  166. System: utils.GetTmplSystemData(),
  167. Data: data,
  168. })
  169. if err != nil {
  170. utils.SystemErrorPageTemplate(this.W, err)
  171. return
  172. }
  173. this.W.Write(tpl.Bytes())
  174. }
  175. func (this *Wrapper) RenderBackEnd(tcont []byte, data interface{}) {
  176. tmpl, err := template.New("template").Parse(string(tcont))
  177. if err != nil {
  178. utils.SystemErrorPageEngine(this.W, err)
  179. return
  180. }
  181. this.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  182. this.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  183. var tpl bytes.Buffer
  184. err = tmpl.Execute(this.W, consts.TmplData{
  185. System: utils.GetTmplSystemData(),
  186. Data: data,
  187. })
  188. if err != nil {
  189. utils.SystemErrorPageEngine(this.W, err)
  190. return
  191. }
  192. this.W.Write(tpl.Bytes())
  193. }