Skip to content

Commit 7b94bec

Browse files
authored
Add SwaggerUi to replace Apiary (#517)
* Add SwaggerUi to replace Apiary, expose api-doc and add basic-auth * Update README.md
1 parent 7894787 commit 7b94bec

File tree

8 files changed

+59
-4
lines changed

8 files changed

+59
-4
lines changed

.env.sample

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ PASSWORD_RESET_URL=http://127.0.0.1:3000
88
SENDGRID_API_KEY=
99
SERVER_HOST=localhost
1010
S3_BUCKET_NAME=
11+
SWAGGER_USERNAME=username
12+
SWAGGER_PASSWORD=password

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ gem 'pg', '~> 1.5'
2626
gem 'puma', '~> 6.4'
2727
gem 'pundit', '~> 2.3'
2828
gem 'rack-cors', '~> 2.0'
29+
gem 'rswag-api', '~> 2.11.0'
30+
gem 'rswag-ui', '~> 2.11.0'
2931
gem 'sass-rails', '~> 6.0.0'
3032
gem 'sendgrid', '~> 1.2.4'
3133
gem 'sprockets', '~> 4.2.1'

Gemfile.lock

+7
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ GEM
415415
rspec-mocks (~> 3.12)
416416
rspec-support (~> 3.12)
417417
rspec-support (3.12.1)
418+
rswag-api (2.11.0)
419+
railties (>= 3.1, < 7.2)
420+
rswag-ui (2.11.0)
421+
actionpack (>= 3.1, < 7.2)
422+
railties (>= 3.1, < 7.2)
418423
rubocop (1.57.2)
419424
json (~> 2.3)
420425
language_server-protocol (>= 3.17.0)
@@ -557,6 +562,8 @@ DEPENDENCIES
557562
rspec-openapi (~> 0.9)
558563
rspec-rails (~> 6.0)
559564
rspec-retry!
565+
rswag-api (~> 2.11.0)
566+
rswag-ui (~> 2.11.0)
560567
rubocop (~> 1.57)
561568
rubocop-factory_bot (~> 2.24)
562569
rubocop-performance (~> 1.19)

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ To illustrate, `bin/rails console` will run the console in the docker container
9999
- [Rails Best Practices](https://github.com/flyerhzm/rails_best_practices) for rails linting
100100
- [Reek](https://github.com/troessner/reek) for ruby linting
101101
- [RSpec](https://github.com/rspec/rspec) for testing
102-
- [RSpec OpenAPI](https://github.com/exoego/rspec-openapi) for API documentation
102+
- [RSpec OpenAPI](https://github.com/exoego/rspec-openapi) for generating API documentation
103+
- [Rswag](https://github.com/rswag/rswag) for expose API documentation
103104
- [Rubocop](https://github.com/bbatsov/rubocop/) for ruby linting
104105
- [Sendgrid](https://github.com/stephenb/sendgrid) for sending mails
105106
- [Shoulda Matchers](https://github.com/thoughtbot/shoulda-matchers) adds other testing matchers
@@ -117,9 +118,8 @@ To illustrate, `bin/rails console` will run the console in the docker container
117118

118119
## Api Docs
119120

120-
https://railsapibasers.docs.apiary.io/
121-
122-
With [RSpec API Doc Generator](https://github.com/exoego/rspec-openapi) you can generate the docs after writing requests specs.
121+
- [RSpec API Doc Generator](https://github.com/exoego/rspec-openapi) you can generate the docs after writing requests specs.
122+
- [Rswag](https://github.com/rswag/rswag) you can expose the generated docs.
123123

124124
See [api_documentation](./docs/api_documentation.md) docs for more info.
125125

config/initializers/rswag_api.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
Rswag::Api.configure do |c|
4+
# Specify a root folder where Swagger JSON files are located
5+
# This is used by the Swagger middleware to serve requests for API descriptions
6+
# NOTE: If you're using rswag-specs to generate Swagger, you'll need to ensure
7+
# that it's configured to generate files in the same folder
8+
c.swagger_root = "#{Rails.root}/doc"
9+
10+
# Inject a lambda function to alter the returned Swagger prior to serialization
11+
# The function will have access to the rack env for the current request
12+
# For example, you could leverage this to dynamically assign the "host" property
13+
#
14+
# c.swagger_filter = lambda { |swagger, env| swagger['host'] = env['HTTP_HOST'] }
15+
end

config/initializers/rswag_ui.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
Rswag::Ui.configure do |c|
4+
# List the Swagger endpoints that you want to be documented through the
5+
# swagger-ui. The first parameter is the path (absolute or relative to the UI
6+
# host) to the corresponding endpoint and the second is a title that will be
7+
# displayed in the document selector.
8+
# NOTE: If you're using rspec-api to expose Swagger files
9+
# (under swagger_root) as JSON or YAML endpoints, then the list below should
10+
# correspond to the relative paths for those endpoints.
11+
12+
c.swagger_endpoint '/api-docs/openapi.yaml', 'API V1 Docs'
13+
14+
# Add Basic Auth in case your API is private
15+
c.basic_auth_enabled = true
16+
c.basic_auth_credentials ENV.fetch('SWAGGER_USERNAME', 'username'), ENV.fetch('SWAGGER_PASSWORD', 'password')
17+
end

config/routes.rb

+2
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@
2626
end
2727
end
2828
end
29+
mount Rswag::Ui::Engine => '/api-docs'
30+
mount Rswag::Api::Engine => '/api-docs'
2931
end

docs/api_documentation.md

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ For the action to update the docs make sure to set the `PUSH_KEY` secret so the
99
2. Add a "secret" to the repo with name `PUSH_KEY` and value the generated file `deploy_key`.
1010
3. Add a "deploy key" to the repo with title `GitHub Actions` and value the generated file `deploy_key.pub`. Make sure to tick the "Allow write access" checkbox.
1111

12+
### SwaggerUI
13+
The autogenerated documentation is exposed using Swagger UI, this documentation can be found in the `/api-docs` path.
14+
15+
The route is protected using basic auth by default, this can be disabled in the initializer file `rswag_ui.rb`
16+
```ruby
17+
c.basic_auth_enabled = true
18+
c.basic_auth_credentials ENV.fetch('SWAGGER_USERNAME', 'username'), ENV.fetch('SWAGGER_PASSWORD', 'password')
19+
```
20+
the access data can also be defined using the environment variables `SWAGGER_USERNAME` and `SWAGGER_PASSWORD`.
21+
1222
### Ignoring specs
1323
To ignore specs that we don't want in the final API documentation we can add the `openapi: false` option to the spec like so:
1424
```ruby

0 commit comments

Comments
 (0)