rest.go 4.8 KB

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