The purpose of this template is to accomodate monolith API project including its dashboard.
The boilerplate contains :
- ActiveAdmin
- Grape
- Swagger
- Rspec
- Rails 6.0.0.rc1 or newer
- Postgresql
Assume we want to create a project named Hello
- Create a new rails project
rails new hello -m https://raw.githubusercontent.com/extrainteger/exi-api/master/template.rb -d postgresql - While installing in progress you will be asked some question
Do you want to use Doorkeeper & WineBouncer?
Answer y if you wish to use Doorkeeper
Do you want to use Capistrano?
Answer y if you wish to use Capistrano
- Go to project
cd hello - Edit credential
rails credentials:edit --environment development. Modify the content from credentials/example.yml - Edit credential
rails credentials:edit --environment test. Modify the content from credentials/example.yml - Prepare database
rails db:create && rails db:migrate && rails seed:migrate
Create default admin user from your rails c :
AdminUser.create email: "helmy@extrainteger.com", password: "yunan123", password_confirmation: "yunan123"Execute :
rspec app/controllers/API- Start server
rails s - Go to http://dashboard.lvh.me:3000/admin to check Dashboard
- Go to http://localhost:3000/doc to check API
If you use Doorkeeper, the template will set public as a default scope. The template uses Oauth 2.0 as an authorization and use 2 strategy :
- Application context (Client credential flow)
- User context (You need to choose and implement by yourself)
All of your API endpoint must be protected at least using application context.
-
From your
rails c, create your first application :Doorkeeper::Application.create name: "MyApp", redirect_uri: "urn:ietf:wg:oauth:2.0:oob", confidential: true client_id = Doorkeeper::Application.last.uid client_secret = Doorkeeper::Application.last.secret
-
Change API Doc URL to : http://localhost:3000/doc/oauth
-
Create access token using :
- grant_type : client_credential
- client_id
- client_secret
You need 2 steps to protect your endpoint :
- Add scope
- Add header
Use :
oauth2to protect with default scopeoauth2 "public"to protect with public scopeoauth2 "your_scope your_another_scope"to protect with specific scope(s)
Add headers inside your API description block headers AUTHORIZATION_HEADERS.
Example :
desc 'Your protected endpoint' do
detail 'Your protected endpoint'
headers AUTHORIZATION_HEADERS
end
oauth2
get "/hello" do
{ hello: :world }
endIf you choose Capistrano as your deployment tool, you just have to modify these 2 files staging.rb & production.rb inside config/deploy folder, both files are have the same source code except for the value.
set :application, "your_app_name"set :rvm_ruby_version, 'your_ruby_version' # example: ruby-2.5.3set :deploy_to, 'your_path_on_server' # example: /home/ubuntu/your_app_nameserver "server-ip", user: "server-name", roles: %w{app web db} # example [server-ip: 10.10.10.10, server-name: ubuntu]
Currently, the default setting of Capistrano is set to unicorn, in the next release we'll made an options so that user can choose what tools they would like to use.
Please Read todo.md
