actions.go 1.5 KB

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