mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
Serve Swagger docs via middleware
This commit is contained in:
parent
58691903fb
commit
df4b9c80c9
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,6 +6,7 @@ spec/dummy/db/*.sqlite3-journal
|
|||||||
spec/dummy/log/*.log
|
spec/dummy/log/*.log
|
||||||
spec/dummy/tmp/
|
spec/dummy/tmp/
|
||||||
spec/dummy/.sass-cache
|
spec/dummy/.sass-cache
|
||||||
|
spec/generators/swagger_rails/tmp
|
||||||
bower_components/*
|
bower_components/*
|
||||||
!bower_components/swagger-ui
|
!bower_components/swagger-ui
|
||||||
bower_components/swagger-ui/*
|
bower_components/swagger-ui/*
|
||||||
|
|||||||
@ -1,15 +0,0 @@
|
|||||||
module SwaggerRails
|
|
||||||
class SwaggerDocsController < ApplicationController
|
|
||||||
|
|
||||||
def show
|
|
||||||
render json: swagger_json_for(params[:api_version]), layout: false
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def swagger_json_for(api_version)
|
|
||||||
path = File.join(Rails.root, 'config', 'swagger', api_version, 'swagger.json')
|
|
||||||
File.read(path)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -2,7 +2,6 @@ module SwaggerRails
|
|||||||
class SwaggerUiController < ApplicationController
|
class SwaggerUiController < ApplicationController
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@discovery_path = swagger_path(SwaggerRails.target_api_version)
|
|
||||||
render :index, layout: false
|
render :index, layout: false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -33,7 +33,7 @@
|
|||||||
if (url && url.length > 1) {
|
if (url && url.length > 1) {
|
||||||
url = decodeURIComponent(url[1]);
|
url = decodeURIComponent(url[1]);
|
||||||
} else {
|
} else {
|
||||||
url = "<%= @discovery_path %>";
|
url = "<%= SwaggerRails.swagger_docs.values.first %>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pre load translate...
|
// Pre load translate...
|
||||||
@ -106,9 +106,35 @@
|
|||||||
<div class="swagger-ui-wrap">
|
<div class="swagger-ui-wrap">
|
||||||
<a id="logo" href="http://swagger.io">swagger</a>
|
<a id="logo" href="http://swagger.io">swagger</a>
|
||||||
<form id='api_selector'>
|
<form id='api_selector'>
|
||||||
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div>
|
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text" disabled="disabled"/></div>
|
||||||
<div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div>
|
<div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div>
|
||||||
<div class='input'><a id="explore" href="#" data-sw-translate>Explore</a></div>
|
<div class='input'>
|
||||||
|
<select id="select_version">
|
||||||
|
<% SwaggerRails.swagger_docs.each do |name, path| %>
|
||||||
|
<option value="<%= path %>"><%= name %></option>
|
||||||
|
<% end %>
|
||||||
|
</select>
|
||||||
|
<script type="text/javascript">
|
||||||
|
$('#select_version').change(function () {
|
||||||
|
$('#input_baseUrl').val($(this).val());
|
||||||
|
window.swaggerUi.headerView.showCustom();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<style>
|
||||||
|
#select_version {
|
||||||
|
border: none;
|
||||||
|
-moz-border-radius: 4px;
|
||||||
|
-webkit-border-radius: 4px;
|
||||||
|
-o-border-radius: 4px;
|
||||||
|
-ms-border-radius: 4px;
|
||||||
|
-khtml-border-radius: 4px;
|
||||||
|
border-radius: 4px;
|
||||||
|
color: white;
|
||||||
|
background-color: #547f00;
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
SwaggerRails::Engine.routes.draw do
|
SwaggerRails::Engine.routes.draw do
|
||||||
get '/index.html', to: 'swagger_ui#index', as: :swagger_ui
|
get '/index.html', to: 'swagger_ui#index', as: :swagger_ui
|
||||||
get '/:api_version/swagger.json', to: 'swagger_docs#show', as: :swagger
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
SwaggerRails.configure do |c|
|
SwaggerRails.configure do |c|
|
||||||
|
|
||||||
# Specify the API version and hence discovery_url (e.g. swagger/v1/swagger.json)
|
# List the names and paths of Swagger
|
||||||
# that will be used to power the embedded swagger-ui
|
# documents you'd like to expose in your swagger-ui
|
||||||
c.target_api_version = 'v1'
|
c.swagger_docs = {
|
||||||
|
'API V1' => '/swagger/v1/swagger.json'
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|||||||
@ -7,9 +7,11 @@ module SwaggerRails
|
|||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :target_api_version
|
attr_accessor :swagger_docs
|
||||||
|
|
||||||
#Defaults
|
#Defaults
|
||||||
@@target_api_version = 'v1'
|
@@swagger_docs = {
|
||||||
|
'V1' => '/swagger/v1/swagger.json'
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,9 +1,13 @@
|
|||||||
|
require 'swagger_rails/middleware/swagger_docs'
|
||||||
require 'swagger_rails/middleware/swagger_ui'
|
require 'swagger_rails/middleware/swagger_ui'
|
||||||
|
|
||||||
module SwaggerRails
|
module SwaggerRails
|
||||||
class Engine < ::Rails::Engine
|
class Engine < ::Rails::Engine
|
||||||
isolate_namespace SwaggerRails
|
isolate_namespace SwaggerRails
|
||||||
|
|
||||||
middleware.use SwaggerUi, "#{root}/bower_components/swagger-ui/dist"
|
initializer 'swagger_rails.initialize' do |app|
|
||||||
|
middleware.use SwaggerDocs, File.join(app.root, 'config', 'swagger')
|
||||||
|
middleware.use SwaggerUi, "#{root}/bower_components/swagger-ui/dist"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
4
lib/swagger_rails/middleware/swagger_docs.rb
Normal file
4
lib/swagger_rails/middleware/swagger_docs.rb
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
module SwaggerRails
|
||||||
|
class SwaggerDocs < ActionDispatch::Static
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,6 +1,9 @@
|
|||||||
SwaggerRails.configure do |c|
|
SwaggerRails.configure do |c|
|
||||||
|
|
||||||
# Specify the API version and hence discovery_url (e.g. swagger/v1/swagger.json)
|
# List the names and paths of Swagger
|
||||||
# that will be used to power the embedded swagger-ui
|
# documents you'd like to expose in your swagger-ui
|
||||||
c.target_api_version = 'v1'
|
c.swagger_docs = {
|
||||||
|
'API V1' => '/swagger/v1/swagger.json',
|
||||||
|
'API V2' => '/swagger/v2/swagger.json'
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,6 +0,0 @@
|
|||||||
SwaggerRails.configure do |c|
|
|
||||||
|
|
||||||
# Specify the API version and hence discovery_url (e.g. swagger/v1/swagger.json)
|
|
||||||
# that will be used to power the embedded swagger-ui
|
|
||||||
c.target_api_version = 'v1'
|
|
||||||
end
|
|
||||||
@ -1,54 +0,0 @@
|
|||||||
{
|
|
||||||
"swagger": "2.0",
|
|
||||||
"info": {
|
|
||||||
"version": "0.0.0",
|
|
||||||
"title": "[Enter a description for your API here]",
|
|
||||||
"description": "The docs below are powered by the default swagger.json that was installed with swagger_rails. You can update it to describe your API. See here for the complete swagger spec - https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md"
|
|
||||||
},
|
|
||||||
"paths": {
|
|
||||||
"/a/sample/resource": {
|
|
||||||
"post": {
|
|
||||||
"tags": [
|
|
||||||
"a/sample/resource"
|
|
||||||
],
|
|
||||||
"description": "Create a new sample resource",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"name": "body",
|
|
||||||
"in": "body",
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/CreateSampleResource"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Ok"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"definitions": {
|
|
||||||
"ClinicPatientLink": {
|
|
||||||
"properties": {
|
|
||||||
"clinic_patient_link": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/definitions/ClinicPatientLink"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"ClinicPatientLink": {
|
|
||||||
"properties": {
|
|
||||||
"name": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"date_time": {
|
|
||||||
"type": "string",
|
|
||||||
"format": "date-time"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user