mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 15:22:56 +00:00
Renames gems to open_api-rswag-*
This commit is contained in:
@@ -3,7 +3,7 @@ require 'rails/generators'
|
||||
module Rswag
|
||||
module Ui
|
||||
class CustomGenerator < Rails::Generators::Base
|
||||
source_root File.expand_path('../../../../../../lib/rswag/ui', __FILE__)
|
||||
source_root File.expand_path('../../../../../../lib/open_api/rswag/ui', __FILE__)
|
||||
|
||||
def add_custom_index
|
||||
copy_file('index.erb', 'app/views/rswag/ui/home/index.html.erb')
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Rswag::Ui.configure do |c|
|
||||
OpenApi::Rswag::Ui.configure do |c|
|
||||
|
||||
# List the Swagger endpoints that you want to be documented through the swagger-ui
|
||||
# The first parameter is the path (absolute or relative to the UI host) to the corresponding
|
||||
|
||||
16
rswag-ui/lib/open_api/rswag/ui.rb
Normal file
16
rswag-ui/lib/open_api/rswag/ui.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require 'open_api/rswag/ui/configuration'
|
||||
require 'open_api/rswag/ui/engine'
|
||||
|
||||
module OpenApi
|
||||
module Rswag
|
||||
module Ui
|
||||
def self.configure
|
||||
yield(config)
|
||||
end
|
||||
|
||||
def self.config
|
||||
@config ||= Configuration.new
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
37
rswag-ui/lib/open_api/rswag/ui/configuration.rb
Normal file
37
rswag-ui/lib/open_api/rswag/ui/configuration.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
require 'ostruct'
|
||||
|
||||
module OpenApi
|
||||
module Rswag
|
||||
module Ui
|
||||
class Configuration
|
||||
attr_reader :template_locations
|
||||
attr_accessor :config_object
|
||||
attr_accessor :oauth_config_object
|
||||
attr_reader :assets_root
|
||||
|
||||
def initialize
|
||||
@template_locations = [
|
||||
# preffered override location
|
||||
"#{Rack::Directory.new('').root}/swagger/index.erb",
|
||||
# backwards compatible override location
|
||||
"#{Rack::Directory.new('').root}/app/views/rswag/ui/home/index.html.erb",
|
||||
# default location
|
||||
File.expand_path('../index.erb', __FILE__)
|
||||
]
|
||||
@assets_root = File.expand_path('../../../../../node_modules/swagger-ui-dist', __FILE__)
|
||||
@config_object = {}
|
||||
@oauth_config_object = {}
|
||||
end
|
||||
|
||||
def swagger_endpoint(url, name)
|
||||
@config_object[:urls] ||= []
|
||||
@config_object[:urls] << { url: url, name: name }
|
||||
end
|
||||
|
||||
def get_binding
|
||||
binding
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
19
rswag-ui/lib/open_api/rswag/ui/engine.rb
Normal file
19
rswag-ui/lib/open_api/rswag/ui/engine.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
require 'open_api/rswag/ui/middleware'
|
||||
|
||||
module OpenApi
|
||||
module Rswag
|
||||
module Ui
|
||||
class Engine < ::Rails::Engine
|
||||
isolate_namespace OpenApi::Rswag::Ui
|
||||
|
||||
initializer 'rswag-ui.initialize' do |app|
|
||||
middleware.use OpenApi::Rswag::Ui::Middleware, OpenApi::Rswag::Ui.config
|
||||
end
|
||||
|
||||
rake_tasks do
|
||||
load File.expand_path('../../../tasks/rswag-ui_tasks.rake', __FILE__)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
46
rswag-ui/lib/open_api/rswag/ui/middleware.rb
Normal file
46
rswag-ui/lib/open_api/rswag/ui/middleware.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
module OpenApi
|
||||
module Rswag
|
||||
module Ui
|
||||
class Middleware < Rack::Static
|
||||
|
||||
def initialize(app, config)
|
||||
@config = config
|
||||
super(app, urls: [ '' ], root: config.assets_root )
|
||||
end
|
||||
|
||||
def call(env)
|
||||
if base_path?(env)
|
||||
redirect_uri = env['SCRIPT_NAME'].chomp('/') + '/index.html'
|
||||
return [ 301, { 'Location' => redirect_uri }, [ ] ]
|
||||
end
|
||||
|
||||
if index_path?(env)
|
||||
return [ 200, { 'Content-Type' => 'text/html' }, [ render_template ] ]
|
||||
end
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def base_path?(env)
|
||||
env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/"
|
||||
end
|
||||
|
||||
def index_path?(env)
|
||||
env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/index.html"
|
||||
end
|
||||
|
||||
def render_template
|
||||
file = File.new(template_filename)
|
||||
template = ERB.new(file.read)
|
||||
template.result(@config.get_binding)
|
||||
end
|
||||
|
||||
def template_filename
|
||||
@config.template_locations.find { |filename| File.exists?(filename) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,14 +0,0 @@
|
||||
require 'rswag/ui/configuration'
|
||||
require 'rswag/ui/engine'
|
||||
|
||||
module Rswag
|
||||
module Ui
|
||||
def self.configure
|
||||
yield(config)
|
||||
end
|
||||
|
||||
def self.config
|
||||
@config ||= Configuration.new
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,35 +0,0 @@
|
||||
require 'ostruct'
|
||||
|
||||
module Rswag
|
||||
module Ui
|
||||
class Configuration
|
||||
attr_reader :template_locations
|
||||
attr_accessor :config_object
|
||||
attr_accessor :oauth_config_object
|
||||
attr_reader :assets_root
|
||||
|
||||
def initialize
|
||||
@template_locations = [
|
||||
# preffered override location
|
||||
"#{Rack::Directory.new('').root}/swagger/index.erb",
|
||||
# backwards compatible override location
|
||||
"#{Rack::Directory.new('').root}/app/views/rswag/ui/home/index.html.erb",
|
||||
# default location
|
||||
File.expand_path('../index.erb', __FILE__)
|
||||
]
|
||||
@assets_root = File.expand_path('../../../../node_modules/swagger-ui-dist', __FILE__)
|
||||
@config_object = {}
|
||||
@oauth_config_object = {}
|
||||
end
|
||||
|
||||
def swagger_endpoint(url, name)
|
||||
@config_object[:urls] ||= []
|
||||
@config_object[:urls] << { url: url, name: name }
|
||||
end
|
||||
|
||||
def get_binding
|
||||
binding
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,17 +0,0 @@
|
||||
require 'rswag/ui/middleware'
|
||||
|
||||
module Rswag
|
||||
module Ui
|
||||
class Engine < ::Rails::Engine
|
||||
isolate_namespace Rswag::Ui
|
||||
|
||||
initializer 'rswag-ui.initialize' do |app|
|
||||
middleware.use Rswag::Ui::Middleware, Rswag::Ui.config
|
||||
end
|
||||
|
||||
rake_tasks do
|
||||
load File.expand_path('../../../tasks/rswag-ui_tasks.rake', __FILE__)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,44 +0,0 @@
|
||||
module Rswag
|
||||
module Ui
|
||||
class Middleware < Rack::Static
|
||||
|
||||
def initialize(app, config)
|
||||
@config = config
|
||||
super(app, urls: [ '' ], root: config.assets_root )
|
||||
end
|
||||
|
||||
def call(env)
|
||||
if base_path?(env)
|
||||
redirect_uri = env['SCRIPT_NAME'].chomp('/') + '/index.html'
|
||||
return [ 301, { 'Location' => redirect_uri }, [ ] ]
|
||||
end
|
||||
|
||||
if index_path?(env)
|
||||
return [ 200, { 'Content-Type' => 'text/html' }, [ render_template ] ]
|
||||
end
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def base_path?(env)
|
||||
env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/"
|
||||
end
|
||||
|
||||
def index_path?(env)
|
||||
env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/index.html"
|
||||
end
|
||||
|
||||
def render_template
|
||||
file = File.new(template_filename)
|
||||
template = ERB.new(file.read)
|
||||
template.result(@config.get_binding)
|
||||
end
|
||||
|
||||
def template_filename
|
||||
@config.template_locations.find { |filename| File.exists?(filename) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -6,7 +6,7 @@ namespace :rswag do
|
||||
dest = args[:dest]
|
||||
FileUtils.rm_r(dest, force: true)
|
||||
FileUtils.mkdir_p(dest)
|
||||
FileUtils.cp_r(Dir.glob("#{Rswag::Ui.config.assets_root}/{*.js,*.png,*.css}"), dest)
|
||||
FileUtils.cp_r(Dir.glob("#{OpenApi::Rswag::Ui.config.assets_root}/{*.js,*.png,*.css}"), dest)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user