actions.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package actions
  2. import (
  3. "fmt"
  4. "strings"
  5. "golang-fave/engine/wrapper"
  6. )
  7. type hRun func(e *Action)
  8. type Action struct {
  9. w *wrapper.Wrapper
  10. list map[string]hRun
  11. }
  12. func (e *Action) register(name string, handle hRun) {
  13. e.list[name] = handle
  14. }
  15. func (e *Action) write(data string) {
  16. (*e.w.W).Write([]byte(data))
  17. }
  18. func (e *Action) msg_show(title string, msg string) {
  19. e.write(fmt.Sprintf(
  20. `ModalShowMsg('%s', '%s');`,
  21. strings.Replace(strings.Replace(title, `'`, `’`, -1), `"`, `”`, -1),
  22. strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
  23. }
  24. func (e *Action) msg_success(msg string) {
  25. e.msg_show("Success", msg)
  26. }
  27. func (e *Action) msg_error(msg string) {
  28. e.msg_show("Error", msg)
  29. }
  30. func New(w *wrapper.Wrapper) *Action {
  31. act := Action{w, make(map[string]hRun)}
  32. // Register all action here
  33. act.register("mysql", action_mysql)
  34. act.register("signin", action_signin)
  35. return &act
  36. }
  37. func (e *Action) Call() bool {
  38. if e.w.R.Method != "POST" {
  39. return false
  40. }
  41. if err := e.w.R.ParseForm(); err == nil {
  42. action := e.w.R.FormValue("action")
  43. if action != "" {
  44. function, ok := e.list[action]
  45. if ok {
  46. (*e.w.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  47. (*e.w.W).Header().Set("Content-Type", "text/html; charset=utf-8")
  48. function(e)
  49. return true
  50. }
  51. }
  52. }
  53. return false
  54. }