utils.go 2.6 KB

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