actions.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package actions
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "errors"
  6. "fmt"
  7. "reflect"
  8. "strings"
  9. "golang-fave/engine/wrapper"
  10. utils "golang-fave/engine/wrapper/utils"
  11. )
  12. type Action struct {
  13. wrapper *wrapper.Wrapper
  14. db *sql.DB
  15. user *utils.MySql_user
  16. }
  17. func (this *Action) write(data string) {
  18. (*this.wrapper.W).Write([]byte(data))
  19. }
  20. func (this *Action) msg_success(msg string) {
  21. this.write(fmt.Sprintf(
  22. `ShowSystemMsgSuccess('Success!', '%s', false);`,
  23. strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
  24. }
  25. func (this *Action) msg_error(msg string) {
  26. this.write(fmt.Sprintf(
  27. `ShowSystemMsgError('Error!', '%s', true);`,
  28. strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
  29. }
  30. func (this *Action) use_database() error {
  31. if this.db != nil {
  32. return errors.New("already connected to database")
  33. }
  34. if !utils.IsMySqlConfigExists(this.wrapper.DirVHostHome) {
  35. return errors.New("can't read database configuration file")
  36. }
  37. mc, err := utils.MySqlConfigRead(this.wrapper.DirVHostHome)
  38. if err != nil {
  39. return err
  40. }
  41. this.db, err = sql.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
  42. if err != nil {
  43. return err
  44. }
  45. err = this.db.Ping()
  46. if err != nil {
  47. this.db.Close()
  48. return err
  49. }
  50. return nil
  51. }
  52. func (this *Action) load_session_user() error {
  53. if this.db == nil {
  54. return errors.New("not connected to database")
  55. }
  56. if this.user != nil {
  57. return errors.New("user already loaded")
  58. }
  59. if this.wrapper.Session.GetIntDef("UserId", 0) <= 0 {
  60. return errors.New("session user id is not defined")
  61. }
  62. this.user = &utils.MySql_user{}
  63. err := this.db.QueryRow("SELECT `id`, `first_name`, `last_name`, `email`, `password` FROM `users` WHERE `id` = ? LIMIT 1;", this.wrapper.Session.GetIntDef("UserId", 0)).Scan(
  64. &this.user.A_id, &this.user.A_first_name, &this.user.A_last_name, &this.user.A_email, &this.user.A_password)
  65. if err != nil {
  66. return err
  67. }
  68. if this.user.A_id != this.wrapper.Session.GetIntDef("UserId", 0) {
  69. return errors.New("can't load user from session user id")
  70. }
  71. return nil
  72. }
  73. func New(wrapper *wrapper.Wrapper) *Action {
  74. return &Action{wrapper, nil, nil}
  75. }
  76. func (this *Action) Run() bool {
  77. if this.wrapper.R.Method != "POST" {
  78. return false
  79. }
  80. if err := this.wrapper.R.ParseForm(); err == nil {
  81. action := this.wrapper.R.FormValue("action")
  82. if action != "" {
  83. if _, ok := reflect.TypeOf(this).MethodByName("Action_" + action); ok {
  84. (*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  85. (*this.wrapper.W).Header().Set("Content-Type", "text/html; charset=utf-8")
  86. reflect.ValueOf(this).MethodByName("Action_" + action).Call([]reflect.Value{})
  87. return true
  88. }
  89. }
  90. }
  91. return false
  92. }