actions.go 991 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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("signin", action_signin)
  21. return &act
  22. }
  23. func (e *Action) Call() bool {
  24. if e.R.Method != "POST" {
  25. return false
  26. }
  27. if err := e.R.ParseForm(); err == nil {
  28. action := e.R.FormValue("action")
  29. if action != "" {
  30. fn, ok := e.list[action]
  31. if ok {
  32. (*e.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  33. (*e.W).Header().Set("Content-Type", "text/html; charset=utf-8")
  34. fn(e)
  35. return true
  36. }
  37. }
  38. }
  39. return false
  40. }