swagger.go 3.4 KB

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