123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package wrapper
- import (
- "bytes"
- "database/sql"
- "errors"
- "fmt"
- "html/template"
- "net/http"
- "os"
- "golang-fave/consts"
- "golang-fave/logger"
- "golang-fave/utils"
- _ "github.com/go-sql-driver/mysql"
- "github.com/vladimirok5959/golang-server-sessions/session"
- )
- type Wrapper struct {
- l *logger.Logger
- W http.ResponseWriter
- R *http.Request
- S *session.Session
- Host string
- Port string
- CurrHost string
- DConfig string
- DHtdocs string
- DLogs string
- DTemplate string
- DTmp string
- IsBackend bool
- ConfMysqlExists bool
- UrlArgs []string
- CurrModule string
- CurrSubModule string
- DB *sql.DB
- User *utils.MySql_user
- }
- func New(l *logger.Logger, w http.ResponseWriter, r *http.Request, s *session.Session, host, port, chost, dirConfig, dirHtdocs, dirLogs, dirTemplate, dirTmp string) *Wrapper {
- return &Wrapper{
- l: l,
- W: w,
- R: r,
- S: s,
- Host: host,
- Port: port,
- CurrHost: chost,
- DConfig: dirConfig,
- DHtdocs: dirHtdocs,
- DLogs: dirLogs,
- DTemplate: dirTemplate,
- DTmp: dirTmp,
- UrlArgs: []string{},
- CurrModule: "",
- CurrSubModule: "",
- }
- }
- func (this *Wrapper) LogAccess(msg string) {
- this.l.Log(msg, this.R, false)
- }
- func (this *Wrapper) LogError(msg string) {
- this.l.Log(msg, this.R, true)
- }
- func (this *Wrapper) UseDatabase() error {
- if this.DB != nil {
- return errors.New("already connected to database")
- }
- if !utils.IsMySqlConfigExists(this.DConfig + string(os.PathSeparator) + "mysql.json") {
- return errors.New("can't read database configuration file")
- }
- mc, err := utils.MySqlConfigRead(this.DConfig + string(os.PathSeparator) + "mysql.json")
- if err != nil {
- return err
- }
- this.DB, err = sql.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
- if err != nil {
- return err
- }
- err = this.DB.Ping()
- if err != nil {
- this.DB.Close()
- return err
- }
- return nil
- }
- func (this *Wrapper) LoadSessionUser() bool {
- if this.S.GetInt("UserId", 0) <= 0 {
- return false
- }
- if this.DB == nil {
- return false
- }
- user := &utils.MySql_user{}
- err := this.DB.QueryRow(`
- SELECT
- id,
- first_name,
- last_name,
- email,
- password,
- admin,
- active
- FROM
- users
- WHERE
- id = ?
- LIMIT 1;`,
- this.S.GetInt("UserId", 0),
- ).Scan(
- &user.A_id,
- &user.A_first_name,
- &user.A_last_name,
- &user.A_email,
- &user.A_password,
- &user.A_admin,
- &user.A_active,
- )
- if err != nil {
- return false
- }
- if user.A_id != this.S.GetInt("UserId", 0) {
- return false
- }
- this.User = user
- return true
- }
- func (this *Wrapper) Write(data string) {
- this.W.Write([]byte(data))
- }
- func (this *Wrapper) MsgSuccess(msg string) {
- this.Write(fmt.Sprintf(
- `fave.ShowMsgSuccess('Success!', '%s', false);`,
- utils.JavaScriptVarValue(msg)))
- }
- func (this *Wrapper) MsgError(msg string) {
- this.Write(fmt.Sprintf(
- `fave.ShowMsgError('Error!', '%s', true);`,
- utils.JavaScriptVarValue(msg)))
- }
- func (this *Wrapper) RenderToString(tcont []byte, data interface{}) string {
- tmpl, err := template.New("template").Parse(string(tcont))
- if err != nil {
- return err.Error()
- }
- var tpl bytes.Buffer
- if err := tmpl.Execute(&tpl, data); err != nil {
- return err.Error()
- }
- return tpl.String()
- }
- func (this *Wrapper) RenderFrontEnd(tname string, data interface{}) {
- tmpl, err := template.ParseFiles(
- this.DTemplate+string(os.PathSeparator)+tname+".html",
- this.DTemplate+string(os.PathSeparator)+"header.html",
- this.DTemplate+string(os.PathSeparator)+"sidebar.html",
- this.DTemplate+string(os.PathSeparator)+"footer.html",
- )
- if err != nil {
- utils.SystemErrorPageTemplate(this.W, err)
- return
- }
- this.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
- this.W.Header().Set("Content-Type", "text/html; charset=utf-8")
- var tpl bytes.Buffer
- err = tmpl.Execute(&tpl, consts.TmplData{
- System: utils.GetTmplSystemData(),
- Data: data,
- })
- if err != nil {
- utils.SystemErrorPageTemplate(this.W, err)
- return
- }
- this.W.Write(tpl.Bytes())
- }
- func (this *Wrapper) RenderBackEnd(tcont []byte, data interface{}) {
- tmpl, err := template.New("template").Parse(string(tcont))
- if err != nil {
- utils.SystemErrorPageEngine(this.W, err)
- return
- }
- this.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
- this.W.Header().Set("Content-Type", "text/html; charset=utf-8")
- var tpl bytes.Buffer
- err = tmpl.Execute(this.W, consts.TmplData{
- System: utils.GetTmplSystemData(),
- Data: data,
- })
- if err != nil {
- utils.SystemErrorPageEngine(this.W, err)
- return
- }
- this.W.Write(tpl.Bytes())
- }
|