rest.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 api
  11. import (
  12. "crypto/tls"
  13. "fmt"
  14. "net/http"
  15. "strings"
  16. "sync"
  17. "devt.de/krotik/rufs"
  18. )
  19. /*
  20. APIVersion is the version of the REST API
  21. */
  22. const APIVersion = "1.0.0"
  23. /*
  24. APIRoot is the API root directory for the REST API
  25. */
  26. const APIRoot = "/fs"
  27. /*
  28. APISchemes defines the supported schemes by the REST API
  29. */
  30. var APISchemes = []string{"https"}
  31. /*
  32. APIHost is the host definition for the REST API
  33. */
  34. var APIHost = "localhost:9040"
  35. /*
  36. RestEndpointInst models a factory function for REST endpoint handlers.
  37. */
  38. type RestEndpointInst func() RestEndpointHandler
  39. /*
  40. GeneralEndpointMap is a map of urls to general REST endpoints
  41. */
  42. var GeneralEndpointMap = map[string]RestEndpointInst{
  43. EndpointAbout: AboutEndpointInst,
  44. EndpointSwagger: SwaggerEndpointInst,
  45. }
  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. trees is a map of all trees which can be used by the REST API
  73. */
  74. var trees = make(map[string]*rufs.Tree)
  75. var treesLock = sync.Mutex{}
  76. /*
  77. ResetTrees removes all registered trees.
  78. */
  79. var ResetTrees = func() {
  80. treesLock.Lock()
  81. defer treesLock.Unlock()
  82. trees = make(map[string]*rufs.Tree)
  83. }
  84. /*
  85. Trees is a getter function which returns a map of all registered trees.
  86. This function can be overwritten by client code to implement access
  87. control.
  88. */
  89. var Trees = func() (map[string]*rufs.Tree, error) {
  90. treesLock.Lock()
  91. defer treesLock.Unlock()
  92. ret := make(map[string]*rufs.Tree)
  93. for k, v := range trees {
  94. ret[k] = v
  95. }
  96. return ret, nil
  97. }
  98. /*
  99. GetTree returns a specific tree. This function can be overwritten by
  100. client code to implement access control.
  101. */
  102. var GetTree = func(id string) (*rufs.Tree, bool, error) {
  103. treesLock.Lock()
  104. defer treesLock.Unlock()
  105. tree, ok := trees[id]
  106. return tree, ok, nil
  107. }
  108. /*
  109. AddTree adds a new tree. This function can be overwritten by
  110. client code to implement access control.
  111. */
  112. var AddTree = func(id string, tree *rufs.Tree) error {
  113. treesLock.Lock()
  114. defer treesLock.Unlock()
  115. if _, ok := trees[id]; ok {
  116. return fmt.Errorf("Tree %v already exists", id)
  117. }
  118. trees[id] = tree
  119. return nil
  120. }
  121. /*
  122. RemoveTree removes a tree. This function can be overwritten by
  123. client code to implement access control.
  124. */
  125. var RemoveTree = func(id string) error {
  126. treesLock.Lock()
  127. defer treesLock.Unlock()
  128. if _, ok := trees[id]; !ok {
  129. return fmt.Errorf("Tree %v does not exist", id)
  130. }
  131. delete(trees, id)
  132. return nil
  133. }
  134. /*
  135. TreeConfigTemplate is the configuration which is used for newly created trees.
  136. */
  137. var TreeConfigTemplate map[string]interface{}
  138. /*
  139. TreeCertTemplate is the certificate which is used for newly created trees.
  140. */
  141. var TreeCertTemplate *tls.Certificate
  142. /*
  143. Map of all registered endpoint handlers.
  144. */
  145. var registered = map[string]RestEndpointInst{}
  146. /*
  147. HandleFunc to use for registering handlers
  148. */
  149. var HandleFunc = http.HandleFunc
  150. /*
  151. RegisterRestEndpoints registers all given REST endpoint handlers.
  152. */
  153. func RegisterRestEndpoints(endpointInsts map[string]RestEndpointInst) {
  154. for url, endpointInst := range endpointInsts {
  155. registered[url] = endpointInst
  156. HandleFunc(url, func() func(w http.ResponseWriter, r *http.Request) {
  157. var handlerURL = url
  158. var handlerInst = endpointInst
  159. return func(w http.ResponseWriter, r *http.Request) {
  160. // Create a new handler instance
  161. handler := handlerInst()
  162. // Handle request in appropriate method
  163. res := strings.TrimSpace(r.URL.Path[len(handlerURL):])
  164. if len(res) > 0 && res[len(res)-1] == '/' {
  165. res = res[:len(res)-1]
  166. }
  167. var resources []string
  168. if res != "" {
  169. resources = strings.Split(res, "/")
  170. }
  171. switch r.Method {
  172. case "GET":
  173. handler.HandleGET(w, r, resources)
  174. case "POST":
  175. handler.HandlePOST(w, r, resources)
  176. case "PUT":
  177. handler.HandlePUT(w, r, resources)
  178. case "DELETE":
  179. handler.HandleDELETE(w, r, resources)
  180. default:
  181. http.Error(w, http.StatusText(http.StatusMethodNotAllowed),
  182. http.StatusMethodNotAllowed)
  183. }
  184. }
  185. }())
  186. }
  187. }
  188. /*
  189. DefaultEndpointHandler is the default endpoint handler implementation.
  190. */
  191. type DefaultEndpointHandler struct {
  192. }
  193. /*
  194. HandleGET handles a GET request.
  195. */
  196. func (de *DefaultEndpointHandler) HandleGET(w http.ResponseWriter, r *http.Request, resources []string) {
  197. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  198. }
  199. /*
  200. HandlePOST handles a POST request.
  201. */
  202. func (de *DefaultEndpointHandler) HandlePOST(w http.ResponseWriter, r *http.Request, resources []string) {
  203. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  204. }
  205. /*
  206. HandlePUT handles a PUT request.
  207. */
  208. func (de *DefaultEndpointHandler) HandlePUT(w http.ResponseWriter, r *http.Request, resources []string) {
  209. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  210. }
  211. /*
  212. HandleDELETE handles a DELETE request.
  213. */
  214. func (de *DefaultEndpointHandler) HandleDELETE(w http.ResponseWriter, r *http.Request, resources []string) {
  215. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  216. }