rest.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package v1
  11. import (
  12. "net/http"
  13. "strconv"
  14. "strings"
  15. "devt.de/krotik/eliasdb/api"
  16. )
  17. /*
  18. APIv1 is the directory for version 1 of the API
  19. */
  20. const APIv1 = "/v1"
  21. /*
  22. HTTPHeaderTotalCount is a special header value containing the total count of objects.
  23. */
  24. const HTTPHeaderTotalCount = "X-Total-Count"
  25. /*
  26. HTTPHeaderCacheID is a special header value containing a cache ID for a quick follow up query.
  27. */
  28. const HTTPHeaderCacheID = "X-Cache-Id"
  29. /*
  30. V1EndpointMap is a map of urls to endpoints for version 1 of the API
  31. */
  32. var V1EndpointMap = map[string]api.RestEndpointInst{
  33. EndpointBlob: BlobEndpointInst,
  34. EndpointClusterQuery: ClusterEndpointInst,
  35. EndpointEql: EqlEndpointInst,
  36. EndpointGraph: GraphEndpointInst,
  37. EndpointGraphQL: GraphQLEndpointInst,
  38. EndpointGraphQLQuery: GraphQLQueryEndpointInst,
  39. EndpointGraphQLSubscriptions: GraphQLSubscriptionsEndpointInst,
  40. EndpointIndexQuery: IndexEndpointInst,
  41. EndpointFindQuery: FindEndpointInst,
  42. EndpointInfoQuery: InfoEndpointInst,
  43. EndpointQuery: QueryEndpointInst,
  44. EndpointQueryResult: QueryResultEndpointInst,
  45. EndpointECALInternal: EndpointECALInst,
  46. }
  47. /*
  48. V1PublicEndpointMap is a map of urls to public endpoints for version 1 of the API
  49. */
  50. var V1PublicEndpointMap = map[string]api.RestEndpointInst{
  51. EndpointECALPublic: EndpointECALInst,
  52. }
  53. // Helper functions
  54. // ================
  55. /*
  56. checkResources check given resources for a GET request.
  57. */
  58. func checkResources(w http.ResponseWriter, resources []string, requiredMin int, requiredMax int, errorMsg string) bool {
  59. if len(resources) < requiredMin {
  60. http.Error(w, errorMsg, http.StatusBadRequest)
  61. return false
  62. } else if len(resources) > requiredMax {
  63. http.Error(w, "Invalid resource specification: "+strings.Join(resources[1:], "/"), http.StatusBadRequest)
  64. return false
  65. }
  66. return true
  67. }
  68. /*
  69. Extract a positive number from a query parameter. Returns -1 and true
  70. if the parameter was not given.
  71. */
  72. func queryParamPosNum(w http.ResponseWriter, r *http.Request, param string) (int, bool) {
  73. val := r.URL.Query().Get(param)
  74. if val == "" {
  75. return -1, true
  76. }
  77. num, err := strconv.Atoi(val)
  78. if err != nil || num < 0 {
  79. http.Error(w, "Invalid parameter value: "+param+" should be a positive integer number", http.StatusBadRequest)
  80. return -1, false
  81. }
  82. return num, true
  83. }