utils.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package utils
  2. import (
  3. "errors"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. )
  8. // Response related
  9. func SendTextResponse(w http.ResponseWriter, msg string) {
  10. w.Write([]byte(msg))
  11. }
  12. // Send JSON response, with an extra json header
  13. func SendJSONResponse(w http.ResponseWriter, json string) {
  14. w.Header().Set("Content-Type", "application/json")
  15. w.Write([]byte(json))
  16. }
  17. func SendErrorResponse(w http.ResponseWriter, errMsg string) {
  18. w.Header().Set("Content-Type", "application/json")
  19. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  20. }
  21. func SendOK(w http.ResponseWriter) {
  22. w.Header().Set("Content-Type", "application/json")
  23. w.Write([]byte("\"OK\""))
  24. }
  25. // Get GET parameter
  26. func GetPara(r *http.Request, key string) (string, error) {
  27. // Get first value from the URL query
  28. value := r.URL.Query().Get(key)
  29. if len(value) == 0 {
  30. return "", errors.New("invalid " + key + " given")
  31. }
  32. return value, nil
  33. }
  34. // Get GET paramter as boolean, accept 1 or true
  35. func GetBool(r *http.Request, key string) (bool, error) {
  36. x, err := GetPara(r, key)
  37. if err != nil {
  38. return false, err
  39. }
  40. // Convert to lowercase and trim spaces just once to compare
  41. switch strings.ToLower(strings.TrimSpace(x)) {
  42. case "1", "true", "on":
  43. return true, nil
  44. case "0", "false", "off":
  45. return false, nil
  46. }
  47. return false, errors.New("invalid boolean given")
  48. }
  49. // Get POST parameter
  50. func PostPara(r *http.Request, key string) (string, error) {
  51. // Try to parse the form
  52. if err := r.ParseForm(); err != nil {
  53. return "", err
  54. }
  55. // Get first value from the form
  56. x := r.Form.Get(key)
  57. if len(x) == 0 {
  58. return "", errors.New("invalid " + key + " given")
  59. }
  60. return x, nil
  61. }
  62. // Get POST paramter as boolean, accept 1 or true
  63. func PostBool(r *http.Request, key string) (bool, error) {
  64. x, err := PostPara(r, key)
  65. if err != nil {
  66. return false, err
  67. }
  68. // Convert to lowercase and trim spaces just once to compare
  69. switch strings.ToLower(strings.TrimSpace(x)) {
  70. case "1", "true", "on":
  71. return true, nil
  72. case "0", "false", "off":
  73. return false, nil
  74. }
  75. return false, errors.New("invalid boolean given")
  76. }
  77. // Get POST paramter as int
  78. func PostInt(r *http.Request, key string) (int, error) {
  79. x, err := PostPara(r, key)
  80. if err != nil {
  81. return 0, err
  82. }
  83. x = strings.TrimSpace(x)
  84. rx, err := strconv.Atoi(x)
  85. if err != nil {
  86. return 0, err
  87. }
  88. return rx, nil
  89. }