Serve Swagger docs via middleware

This commit is contained in:
domaindrivendev 2016-02-03 23:41:31 -08:00
parent 58691903fb
commit df4b9c80c9
12 changed files with 54 additions and 89 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ spec/dummy/db/*.sqlite3-journal
spec/dummy/log/*.log
spec/dummy/tmp/
spec/dummy/.sass-cache
spec/generators/swagger_rails/tmp
bower_components/*
!bower_components/swagger-ui
bower_components/swagger-ui/*

View File

@ -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

View File

@ -2,7 +2,6 @@ module SwaggerRails
class SwaggerUiController < ApplicationController
def index
@discovery_path = swagger_path(SwaggerRails.target_api_version)
render :index, layout: false
end
end

View File

@ -33,7 +33,7 @@
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "<%= @discovery_path %>";
url = "<%= SwaggerRails.swagger_docs.values.first %>";
}
// Pre load translate...
@ -106,9 +106,35 @@
<div class="swagger-ui-wrap">
<a id="logo" href="http://swagger.io">swagger</a>
<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'><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>
</div>
</div>

View File

@ -1,4 +1,3 @@
SwaggerRails::Engine.routes.draw do
get '/index.html', to: 'swagger_ui#index', as: :swagger_ui
get '/:api_version/swagger.json', to: 'swagger_docs#show', as: :swagger
end

View File

@ -1,6 +1,8 @@
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'
# List the names and paths of Swagger
# documents you'd like to expose in your swagger-ui
c.swagger_docs = {
'API V1' => '/swagger/v1/swagger.json'
}
end

View File

@ -7,9 +7,11 @@ module SwaggerRails
end
class << self
attr_accessor :target_api_version
attr_accessor :swagger_docs
#Defaults
@@target_api_version = 'v1'
@@swagger_docs = {
'V1' => '/swagger/v1/swagger.json'
}
end
end

View File

@ -1,9 +1,13 @@
require 'swagger_rails/middleware/swagger_docs'
require 'swagger_rails/middleware/swagger_ui'
module SwaggerRails
class Engine < ::Rails::Engine
isolate_namespace SwaggerRails
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

View File

@ -0,0 +1,4 @@
module SwaggerRails
class SwaggerDocs < ActionDispatch::Static
end
end

View File

@ -1,6 +1,9 @@
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'
# List the names and paths of Swagger
# documents you'd like to expose in your swagger-ui
c.swagger_docs = {
'API V1' => '/swagger/v1/swagger.json',
'API V2' => '/swagger/v2/swagger.json'
}
end

View File

@ -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

View File

@ -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"
}
}
}
}
}