common.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package main
  2. import (
  3. "errors"
  4. "log"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "time"
  9. )
  10. /*
  11. SYSTEM COMMON FUNCTIONS
  12. This is a parsed copy of arozos core common functions
  13. Make your life of writing module much easier
  14. */
  15. /*
  16. Basic Response Functions
  17. Send response with ease
  18. */
  19. //Send text response with given w and message as string
  20. func sendTextResponse(w http.ResponseWriter, msg string) {
  21. w.Write([]byte(msg))
  22. }
  23. // Send JSON response, with an extra json header
  24. func sendJSONResponse(w http.ResponseWriter, json string) {
  25. w.Header().Set("Content-Type", "application/json")
  26. w.Write([]byte(json))
  27. }
  28. func sendErrorResponse(w http.ResponseWriter, errMsg string) {
  29. w.Header().Set("Content-Type", "application/json")
  30. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  31. }
  32. func sendOK(w http.ResponseWriter) {
  33. w.Header().Set("Content-Type", "application/json")
  34. w.Write([]byte("\"OK\""))
  35. }
  36. /*
  37. The paramter move function (mv)
  38. You can find similar things in the PHP version of ArOZ Online Beta. You need to pass in
  39. r (HTTP Request Object)
  40. getParamter (string, aka $_GET['This string])
  41. Will return
  42. Paramter string (if any)
  43. Error (if error)
  44. */
  45. func mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  46. if postMode == false {
  47. //Access the paramter via GET
  48. keys, ok := r.URL.Query()[getParamter]
  49. if !ok || len(keys[0]) < 1 {
  50. //systemWideLogger.PrintAndLog("Url Param " + getParamter +" is missing",nil)
  51. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  52. }
  53. // Query()["key"] will return an array of items,
  54. // we only want the single item.
  55. key := keys[0]
  56. return string(key), nil
  57. } else {
  58. //Access the parameter via POST
  59. r.ParseForm()
  60. x := r.Form.Get(getParamter)
  61. if len(x) == 0 || x == "" {
  62. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  63. }
  64. return string(x), nil
  65. }
  66. }
  67. func stringInSlice(a string, list []string) bool {
  68. for _, b := range list {
  69. if b == a {
  70. return true
  71. }
  72. }
  73. return false
  74. }
  75. func fileExists(filename string) bool {
  76. _, err := os.Stat(filename)
  77. if os.IsNotExist(err) {
  78. return false
  79. }
  80. return true
  81. }
  82. func isDir(path string) bool {
  83. if fileExists(path) == false {
  84. return false
  85. }
  86. fi, err := os.Stat(path)
  87. if err != nil {
  88. log.Fatal(err)
  89. return false
  90. }
  91. switch mode := fi.Mode(); {
  92. case mode.IsDir():
  93. return true
  94. case mode.IsRegular():
  95. return false
  96. }
  97. return false
  98. }
  99. func inArray(arr []string, str string) bool {
  100. for _, a := range arr {
  101. if a == str {
  102. return true
  103. }
  104. }
  105. return false
  106. }
  107. func timeToString(targetTime time.Time) string {
  108. return targetTime.Format("2006-01-02 15:04:05")
  109. }
  110. func stringToInt64(number string) (int64, error) {
  111. i, err := strconv.ParseInt(number, 10, 64)
  112. if err != nil {
  113. return -1, err
  114. }
  115. return i, nil
  116. }
  117. func pushToSliceIfNotExist(slice []string, newItem string) []string {
  118. itemExists := false
  119. for _, item := range slice {
  120. if item == newItem {
  121. itemExists = true
  122. }
  123. }
  124. if !itemExists {
  125. slice = append(slice, newItem)
  126. }
  127. return slice
  128. }
  129. func removeFromSliceIfExists(slice []string, target string) []string {
  130. newSlice := []string{}
  131. for _, item := range slice {
  132. if item != target {
  133. newSlice = append(newSlice, item)
  134. }
  135. }
  136. return newSlice
  137. }