actions.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 (e *Action) write(data string) {
  18. (*e.W).Write([]byte(data))
  19. }
  20. func New(w *http.ResponseWriter, r *http.Request, vhost string, vhosthome string, remoteip string) *Action {
  21. act := Action{w, r, vhost, vhosthome, remoteip, make(map[string]hRun)}
  22. // Register all action here
  23. act.register("mysql", action_mysql)
  24. act.register("signin", action_signin)
  25. return &act
  26. }
  27. func (e *Action) Call() bool {
  28. if e.R.Method != "POST" {
  29. return false
  30. }
  31. if err := e.R.ParseForm(); err == nil {
  32. action := e.R.FormValue("action")
  33. if action != "" {
  34. fn, ok := e.list[action]
  35. if ok {
  36. (*e.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  37. (*e.W).Header().Set("Content-Type", "text/html; charset=utf-8")
  38. fn(e)
  39. return true
  40. }
  41. }
  42. }
  43. return false
  44. }