internal.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package auth
  2. import (
  3. "errors"
  4. "net/http"
  5. )
  6. //Common functions
  7. func sendTextResponse(w http.ResponseWriter, msg string) {
  8. w.Write([]byte(msg))
  9. }
  10. //Send JSON response, with an extra json header
  11. func sendJSONResponse(w http.ResponseWriter, json string) {
  12. w.Header().Set("Content-Type", "application/json")
  13. w.Write([]byte(json))
  14. }
  15. func sendErrorResponse(w http.ResponseWriter, errMsg string) {
  16. w.Header().Set("Content-Type", "application/json")
  17. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  18. }
  19. func sendOK(w http.ResponseWriter) {
  20. w.Header().Set("Content-Type", "application/json")
  21. w.Write([]byte("\"OK\""))
  22. }
  23. //TODO: Deprecate this Mv
  24. func Mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  25. if postMode == false {
  26. //Access the paramter via GET
  27. keys, ok := r.URL.Query()[getParamter]
  28. if !ok || len(keys[0]) < 1 {
  29. //log.Println("Url Param " + getParamter +" is missing")
  30. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  31. }
  32. // Query()["key"] will return an array of items,
  33. // we only want the single item.
  34. key := keys[0]
  35. return string(key), nil
  36. } else {
  37. //Access the parameter via POST
  38. r.ParseForm()
  39. x := r.Form.Get(getParamter)
  40. if len(x) == 0 || x == "" {
  41. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  42. }
  43. return string(x), nil
  44. }
  45. }
  46. func inSlice(list []string, a string) bool {
  47. for _, b := range list {
  48. if b == a {
  49. return true
  50. }
  51. }
  52. return false
  53. }