1
0

utils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package utils
  2. import (
  3. "errors"
  4. "log"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. /*
  12. Common
  13. Some commonly used functions in ArozOS
  14. */
  15. // Response related
  16. func SendTextResponse(w http.ResponseWriter, msg string) {
  17. w.Write([]byte(msg))
  18. }
  19. // Send JSON response, with an extra json header
  20. func SendJSONResponse(w http.ResponseWriter, json string) {
  21. w.Header().Set("Content-Type", "application/json")
  22. w.Write([]byte(json))
  23. }
  24. func SendErrorResponse(w http.ResponseWriter, errMsg string) {
  25. w.Header().Set("Content-Type", "application/json")
  26. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  27. }
  28. func SendOK(w http.ResponseWriter) {
  29. w.Header().Set("Content-Type", "application/json")
  30. w.Write([]byte("\"OK\""))
  31. }
  32. // Get GET parameter
  33. func GetPara(r *http.Request, key string) (string, error) {
  34. keys, ok := r.URL.Query()[key]
  35. if !ok || len(keys[0]) < 1 {
  36. return "", errors.New("invalid " + key + " given")
  37. } else {
  38. return keys[0], nil
  39. }
  40. }
  41. // Get POST paramter
  42. func PostPara(r *http.Request, key string) (string, error) {
  43. r.ParseForm()
  44. x := r.Form.Get(key)
  45. if x == "" {
  46. return "", errors.New("invalid " + key + " given")
  47. } else {
  48. return x, nil
  49. }
  50. }
  51. // Get POST paramter as boolean, accept 1 or true
  52. func PostBool(r *http.Request, key string) (bool, error) {
  53. x, err := PostPara(r, key)
  54. if err != nil {
  55. return false, err
  56. }
  57. x = strings.TrimSpace(x)
  58. if x == "1" || strings.ToLower(x) == "true" {
  59. return true, nil
  60. } else if x == "0" || strings.ToLower(x) == "false" {
  61. return false, nil
  62. }
  63. return false, errors.New("invalid boolean given")
  64. }
  65. // Get POST paramter as int
  66. func PostInt(r *http.Request, key string) (int, error) {
  67. x, err := PostPara(r, key)
  68. if err != nil {
  69. return 0, err
  70. }
  71. x = strings.TrimSpace(x)
  72. rx, err := strconv.Atoi(x)
  73. if err != nil {
  74. return 0, err
  75. }
  76. return rx, nil
  77. }
  78. func FileExists(filename string) bool {
  79. _, err := os.Stat(filename)
  80. if os.IsNotExist(err) {
  81. return false
  82. }
  83. return true
  84. }
  85. func IsDir(path string) bool {
  86. if FileExists(path) == false {
  87. return false
  88. }
  89. fi, err := os.Stat(path)
  90. if err != nil {
  91. log.Fatal(err)
  92. return false
  93. }
  94. switch mode := fi.Mode(); {
  95. case mode.IsDir():
  96. return true
  97. case mode.IsRegular():
  98. return false
  99. }
  100. return false
  101. }
  102. func TimeToString(targetTime time.Time) string {
  103. return targetTime.Format("2006-01-02 15:04:05")
  104. }
  105. // Check if given string in a given slice
  106. func StringInArray(arr []string, str string) bool {
  107. for _, a := range arr {
  108. if a == str {
  109. return true
  110. }
  111. }
  112. return false
  113. }
  114. func StringInArrayIgnoreCase(arr []string, str string) bool {
  115. smallArray := []string{}
  116. for _, item := range arr {
  117. smallArray = append(smallArray, strings.ToLower(item))
  118. }
  119. return StringInArray(smallArray, strings.ToLower(str))
  120. }