rest.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Rufs - Remote Union File System
  3. *
  4. * Copyright 2017 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package v1
  11. import (
  12. "fmt"
  13. "net/http"
  14. "strings"
  15. "devt.de/krotik/rufs/api"
  16. )
  17. /*
  18. APIv1 is the directory for version 1 of the API
  19. */
  20. const APIv1 = "/v1"
  21. /*
  22. V1EndpointMap is a map of urls to endpoints for version 1 of the API
  23. */
  24. var V1EndpointMap = map[string]api.RestEndpointInst{
  25. EndpointAdmin: AdminEndpointInst,
  26. EndpointDir: DirEndpointInst,
  27. EndpointFile: FileEndpointInst,
  28. EndpointProgress: ProgressEndpointInst,
  29. EndpointZip: ZipEndpointInst,
  30. }
  31. // Helper functions
  32. // ================
  33. /*
  34. checkResources check given resources for a GET request.
  35. */
  36. func checkResources(w http.ResponseWriter, resources []string, requiredMin int, requiredMax int, errorMsg string) bool {
  37. if len(resources) < requiredMin {
  38. http.Error(w, errorMsg, http.StatusBadRequest)
  39. return false
  40. } else if len(resources) > requiredMax {
  41. http.Error(w, "Invalid resource specification: "+strings.Join(resources[1:], "/"), http.StatusBadRequest)
  42. return false
  43. }
  44. return true
  45. }
  46. /*
  47. getMapValue extracts a value from a given map.
  48. */
  49. func getMapValue(w http.ResponseWriter, data map[string]interface{}, key string) (string, bool) {
  50. if val, ok := data[key]; ok && val != "" {
  51. return fmt.Sprint(val), true
  52. }
  53. http.Error(w, fmt.Sprintf("Value for %v is missing in posted data", key), http.StatusBadRequest)
  54. return "", false
  55. }