rest.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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: ECALEndpointInst,
  46. EndpointECALSock: ECALSockEndpointInst,
  47. }
  48. /*
  49. V1PublicEndpointMap is a map of urls to public endpoints for version 1 of the API
  50. */
  51. var V1PublicEndpointMap = map[string]api.RestEndpointInst{
  52. EndpointECALPublic: ECALEndpointInst,
  53. }
  54. // Helper functions
  55. // ================
  56. /*
  57. checkResources check given resources for a GET request.
  58. */
  59. func checkResources(w http.ResponseWriter, resources []string, requiredMin int, requiredMax int, errorMsg string) bool {
  60. if len(resources) < requiredMin {
  61. http.Error(w, errorMsg, http.StatusBadRequest)
  62. return false
  63. } else if len(resources) > requiredMax {
  64. http.Error(w, "Invalid resource specification: "+strings.Join(resources[1:], "/"), http.StatusBadRequest)
  65. return false
  66. }
  67. return true
  68. }
  69. /*
  70. Extract a positive number from a query parameter. Returns -1 and true
  71. if the parameter was not given.
  72. */
  73. func queryParamPosNum(w http.ResponseWriter, r *http.Request, param string) (int, bool) {
  74. val := r.URL.Query().Get(param)
  75. if val == "" {
  76. return -1, true
  77. }
  78. num, err := strconv.Atoi(val)
  79. if err != nil || num < 0 {
  80. http.Error(w, "Invalid parameter value: "+param+" should be a positive integer number", http.StatusBadRequest)
  81. return -1, false
  82. }
  83. return num, true
  84. }