swagger.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "encoding/json"
  13. "net/http"
  14. )
  15. /*
  16. SwaggerDefs is used to describe the endpoint in swagger.
  17. */
  18. func (a *aboutEndpoint) SwaggerDefs(s map[string]interface{}) {
  19. // Add query paths
  20. s["paths"].(map[string]interface{})["/about"] = map[string]interface{}{
  21. "get": map[string]interface{}{
  22. "summary": "Return information about the REST API provider.",
  23. "description": "Returns available API versions, product name and product version.",
  24. "produces": []string{
  25. "application/json",
  26. },
  27. "responses": map[string]interface{}{
  28. "200": map[string]interface{}{
  29. "description": "About info object",
  30. "schema": map[string]interface{}{
  31. "type": "object",
  32. "properties": map[string]interface{}{
  33. "api_versions": map[string]interface{}{
  34. "description": "List of available API versions.",
  35. "type": "array",
  36. "items": map[string]interface{}{
  37. "description": "Available API version.",
  38. "type": "string",
  39. },
  40. },
  41. "product": map[string]interface{}{
  42. "description": "Product name of the REST API provider.",
  43. "type": "string",
  44. },
  45. "version": map[string]interface{}{
  46. "description": "Version of the REST API provider.",
  47. "type": "string",
  48. },
  49. },
  50. },
  51. },
  52. "default": map[string]interface{}{
  53. "description": "Error response",
  54. "schema": map[string]interface{}{
  55. "$ref": "#/definitions/Error",
  56. },
  57. },
  58. },
  59. },
  60. }
  61. // Add generic error object to definition
  62. s["definitions"].(map[string]interface{})["Error"] = map[string]interface{}{
  63. "description": "A human readable error mesage.",
  64. "type": "string",
  65. }
  66. }
  67. /*
  68. EndpointSwagger is the swagger endpoint URL (rooted). Handles swagger.json/
  69. */
  70. const EndpointSwagger = APIRoot + "/swagger.json/"
  71. /*
  72. SwaggerEndpointInst creates a new endpoint handler.
  73. */
  74. func SwaggerEndpointInst() RestEndpointHandler {
  75. return &swaggerEndpoint{}
  76. }
  77. /*
  78. Handler object for swagger operations.
  79. */
  80. type swaggerEndpoint struct {
  81. *DefaultEndpointHandler
  82. }
  83. /*
  84. HandleGET returns the swagger definition of the REST API.
  85. */
  86. func (a *swaggerEndpoint) HandleGET(w http.ResponseWriter, r *http.Request, resources []string) {
  87. // Add general sections
  88. data := map[string]interface{}{
  89. "swagger": "2.0",
  90. "host": APIHost,
  91. "schemes": APISchemes,
  92. "basePath": APIRoot,
  93. "produces": []string{"application/json"},
  94. "paths": map[string]interface{}{},
  95. "definitions": map[string]interface{}{},
  96. }
  97. // Go through all registered components and let them add their definitions
  98. a.SwaggerDefs(data)
  99. for _, inst := range registered {
  100. inst().SwaggerDefs(data)
  101. }
  102. // Write data
  103. w.Header().Set("content-type", "application/json; charset=utf-8")
  104. ret := json.NewEncoder(w)
  105. ret.Encode(data)
  106. }
  107. /*
  108. SwaggerDefs is used to describe the endpoint in swagger.
  109. */
  110. func (a *swaggerEndpoint) SwaggerDefs(s map[string]interface{}) {
  111. // Add general application information
  112. s["info"] = map[string]interface{}{
  113. "title": "RUFS API",
  114. "description": "Query and control the Remote Union File System.",
  115. "version": APIVersion,
  116. }
  117. }