actions.go 911 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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("Content-Type", "text/html; charset=utf-8")
  33. fn(e)
  34. return true
  35. }
  36. }
  37. }
  38. return false
  39. }