actions.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package actions
  2. import (
  3. "net/http"
  4. )
  5. type hRun func(e *Action)
  6. type Action struct {
  7. W *http.ResponseWriter
  8. R *http.Request
  9. VHost string
  10. vhosthome string
  11. RemoteIp string
  12. list map[string]hRun
  13. }
  14. func (e *Action) register(name string, handle hRun) {
  15. e.list[name] = handle
  16. }
  17. func New(w *http.ResponseWriter, r *http.Request, vhost string, vhosthome string, remoteip string) *Action {
  18. act := Action{w, r, vhost, vhosthome, remoteip, make(map[string]hRun)}
  19. // Register all action here
  20. act.register("mysql", action_mysql)
  21. act.register("signin", action_signin)
  22. return &act
  23. }
  24. func (e *Action) Call() bool {
  25. if e.R.Method != "POST" {
  26. return false
  27. }
  28. if err := e.R.ParseForm(); err == nil {
  29. action := e.R.FormValue("action")
  30. if action != "" {
  31. fn, ok := e.list[action]
  32. if ok {
  33. (*e.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  34. (*e.W).Header().Set("Content-Type", "text/html; charset=utf-8")
  35. fn(e)
  36. return true
  37. }
  38. }
  39. }
  40. return false
  41. }