rest.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 api
  11. import (
  12. "net/http"
  13. "strings"
  14. "devt.de/krotik/common/datautil"
  15. "devt.de/krotik/eliasdb/cluster"
  16. "devt.de/krotik/eliasdb/graph"
  17. "devt.de/krotik/eliasdb/graph/graphstorage"
  18. )
  19. /*
  20. APIVersion is the version of the REST API
  21. */
  22. const APIVersion = "1.0.0"
  23. /*
  24. APIRoot is the root directory for the REST API
  25. */
  26. const APIRoot = "/db"
  27. /*
  28. APISchemes is a list of supported protocol schemes
  29. */
  30. var APISchemes = []string{"https"}
  31. /*
  32. APIHost is the host definition for the REST API
  33. */
  34. var APIHost = "localhost:9090"
  35. /*
  36. GeneralEndpointMap contains general endpoints which should always be available
  37. */
  38. var GeneralEndpointMap = map[string]RestEndpointInst{
  39. EndpointAbout: AboutEndpointInst,
  40. EndpointSwagger: SwaggerEndpointInst,
  41. }
  42. /*
  43. RestEndpointInst models a factory function for REST endpoint handlers.
  44. */
  45. type RestEndpointInst func() RestEndpointHandler
  46. /*
  47. RestEndpointHandler models a REST endpoint handler.
  48. */
  49. type RestEndpointHandler interface {
  50. /*
  51. HandleGET handles a GET request.
  52. */
  53. HandleGET(w http.ResponseWriter, r *http.Request, resources []string)
  54. /*
  55. HandlePOST handles a POST request.
  56. */
  57. HandlePOST(w http.ResponseWriter, r *http.Request, resources []string)
  58. /*
  59. HandlePUT handles a PUT request.
  60. */
  61. HandlePUT(w http.ResponseWriter, r *http.Request, resources []string)
  62. /*
  63. HandleDELETE handles a DELETE request.
  64. */
  65. HandleDELETE(w http.ResponseWriter, r *http.Request, resources []string)
  66. /*
  67. SwaggerDefs is used to describe the endpoint in swagger.
  68. */
  69. SwaggerDefs(s map[string]interface{})
  70. }
  71. /*
  72. GM is the GraphManager instance which should be used by the REST API.
  73. */
  74. var GM *graph.Manager
  75. /*
  76. GS is the GraphStorage instance which should be used by the REST API.
  77. */
  78. var GS graphstorage.Storage
  79. /*
  80. DD is the DistributedStorage instance which should be used by the REST API.
  81. (Only available if clustering is enabled.)
  82. */
  83. var DD *cluster.DistributedStorage
  84. /*
  85. DDLog is a ringbuffer containing cluster related logs.
  86. (Only available if clustering is enabled.)
  87. */
  88. var DDLog *datautil.RingBuffer
  89. /*
  90. Map of all registered endpoint handlers.
  91. */
  92. var registered = map[string]RestEndpointInst{}
  93. /*
  94. HandleFunc to use for registering handlers
  95. Should be of type: func(pattern string, handler func(http.ResponseWriter, *http.Request))
  96. */
  97. var HandleFunc = http.HandleFunc
  98. /*
  99. RegisterRestEndpoints registers all given REST endpoint handlers.
  100. */
  101. func RegisterRestEndpoints(endpointInsts map[string]RestEndpointInst) {
  102. for url, endpointInst := range endpointInsts {
  103. registered[url] = endpointInst
  104. HandleFunc(url, func() func(w http.ResponseWriter, r *http.Request) {
  105. var handlerURL = url
  106. var handlerInst = endpointInst
  107. return func(w http.ResponseWriter, r *http.Request) {
  108. // Create a new handler instance
  109. handler := handlerInst()
  110. // Handle request in appropriate method
  111. res := strings.TrimSpace(r.URL.Path[len(handlerURL):])
  112. if len(res) > 0 && res[len(res)-1] == '/' {
  113. res = res[:len(res)-1]
  114. }
  115. var resources []string
  116. if res != "" {
  117. resources = strings.Split(res, "/")
  118. }
  119. switch r.Method {
  120. case "GET":
  121. handler.HandleGET(w, r, resources)
  122. case "POST":
  123. handler.HandlePOST(w, r, resources)
  124. case "PUT":
  125. handler.HandlePUT(w, r, resources)
  126. case "DELETE":
  127. handler.HandleDELETE(w, r, resources)
  128. default:
  129. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  130. }
  131. }
  132. }())
  133. }
  134. }
  135. /*
  136. DefaultEndpointHandler represents the default endpoint handler.
  137. */
  138. type DefaultEndpointHandler struct {
  139. }
  140. /*
  141. HandleGET is a method stub returning an error.
  142. */
  143. func (de *DefaultEndpointHandler) HandleGET(w http.ResponseWriter, r *http.Request, resources []string) {
  144. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  145. }
  146. /*
  147. HandlePOST is a method stub returning an error.
  148. */
  149. func (de *DefaultEndpointHandler) HandlePOST(w http.ResponseWriter, r *http.Request, resources []string) {
  150. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  151. }
  152. /*
  153. HandlePUT is a method stub returning an error.
  154. */
  155. func (de *DefaultEndpointHandler) HandlePUT(w http.ResponseWriter, r *http.Request, resources []string) {
  156. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  157. }
  158. /*
  159. HandleDELETE is a method stub returning an error.
  160. */
  161. func (de *DefaultEndpointHandler) HandleDELETE(w http.ResponseWriter, r *http.Request, resources []string) {
  162. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  163. }