forked from zakaryaa/rails-api-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails-api-template.rb
317 lines (261 loc) · 9.09 KB
/
rails-api-template.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
run 'pgrep spring | xargs kill -9'
# UTILITY CONSOLE COLORS
########################################
def colorize(txt)
puts "\e[47m\e[1m\e[30m#{txt}\e[0m\e[22m\e[0m"
end
# GEMFILE
########################################
run 'rm Gemfile'
file 'Gemfile', <<-RUBY
source 'https://rubygems.org'
ruby '#{RUBY_VERSION}'
gem 'rails'
gem 'pg', '~> 0.21'
gem 'redis', '~> 4.0'
gem 'puma', '~> 3.11'
gem 'jbuilder', '~> 2.5'
gem 'bootsnap'
gem 'devise', '~> 4.6', '>= 4.6.2'
gem 'devise-jwt', '~> 0.7.0'
gem 'dotenv-rails', '~> 2.7', '>= 2.7.2'
# Excel reader for seeds
gem 'creek'
# active admin dep
gem 'activeadmin', '~> 2.5.0'
gem 'inherited_resources', '~> 1.11.0'
gem 'active_admin_flat_skin', '~> 0.1.2'
gem 'font-awesome-sass', '~> 5.11.2'
gem 'activeadmin_quill_editor', '~> 0.2.0'
# Reduces boot times through caching(str) required in config/boot.rb
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'
group :development do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'rails_real_favicon'
gem 'web-console', '>= 3.3.0'
gem 'annotate'
end
group :development, :test do
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'pry-rails'
end
RUBY
# Procfile
########################################
file 'Procfile', <<-YAML
web: bundle exec puma -C config/puma.rb
YAML
colorize '==> Procfile created'
# README
########################################
markdown_file_content = <<-MARKDOWN
Rails app generated with [zakaryaa/jwt-devise-templates](https://github.com/zakaryaa/rails-api-template).
MARKDOWN
file 'README.md', markdown_file_content, force: true
colorize '==> README created'
########################################
# AFTER BUNDLE
########################################
after_bundle do
colorize '==> After bundle starts'
run "mkdir -p app/assets/config && echo '{}' > app/assets/config/manifest.js"
colorize '==> Empty manifest file created'
# Database init
########################################
rails_command 'db:create db:migrate'
colorize '==> created & migrated DB'
########################################
# # Git ignore
# ########################################
append_file '.gitignore', <<-TXT
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep
.vscode/
# Ignore uploaded files in development
/storage/*
!/storage/.keep
.byebug_history
# Ignore master key for decrypting credentials and more.
/config/master.key
.env
/public/packs
/public/packs-test
/node_modules
/yarn-error.log
yarn-debug.log*
.yarn-integrity
*/.DS_Store
.DS_Store
*.swp
TXT
colorize '==> .gitignore file updated'
# Devise install and controller init
########################################
generate('devise:install')
colorize '==> Devise authentication gem installed'
generate('devise', 'user')
colorize '==> Devise default User model generated'
# Routes
########################################
run 'rm config/routes.rb'
routes = <<-RUBY
Rails.application.routes.draw do
devise_for :users, path: 'api/v1/user', class_name: "User",
controllers: {
registrations: 'api/v1/user/registrations',
sessions: 'api/v1/user/sessions',
passwords: 'api/v1/user/passwords'
}, defaults: { format: :json }
namespace :api, :defaults => {:format => :json} do
namespace :v1 do
get 'users/current', to:'users#show'
end
end
end
RUBY
file 'config/routes.rb', routes, force: true
colorize '==> Routes updated'
########################################
# User Model update
########################################
user_model = <<-RUBY
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
# include Devise::JWT::RevocationStrategies::JTIMatcher
devise :database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:validatable,
:jwt_authenticatable,
jwt_revocation_strategy: JwtDenylist
@expires_in = nil
def expires_in
return @expires_in
end
def on_jwt_dispatch(token, payload)
@expires_in = payload['exp']
end
end
RUBY
run 'rm -rf app/models/api'
file 'app/models/user.rb', user_model, force: true
colorize '==> Update user model'
########################################
# App controller
########################################
run 'rm app/controllers/application_controller.rb'
file 'app/controllers/application_controller.rb', <<-RUBY
class ApplicationController < ActionController::Base
skip_before_action :verify_authenticity_token
end
RUBY
colorize '==> Created application controller'
########################################
# Api controller
########################################
#run "mkdir -p app/controllers/api/v1"
file 'app/controllers/api/v1/base_api_controller.rb', <<-RUBY
class Api::V1::BaseApiController < ActionController::API
before_action :authenticate_user!
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :set_locale
protected
def default_url_options
{locale: I18n.locale == I18n.default_locale ? nil : I18n.locale}
end
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
RUBY
colorize '==> Created base api controller'
########################################
# Api JWT migration
########################################
file 'db/migrate/20190618141950_create_jwt_denylist.rb', <<-RUBY
class CreateJwtDenylist < ActiveRecord::Migration[5.2]
def change
create_table :jwt_denylist do |t|
t.string :jti, null: false
t.datetime :exp, null: false
end
add_index :jwt_denylist, :jti
end
end
RUBY
colorize '==> Created base JWT migration '
########################################
# Api JWT model
########################################
file 'app/models/jwt_denylist.rb', <<-RUBY
class JwtDenylist < ApplicationRecord
include Devise::JWT::RevocationStrategies::Denylist
self.table_name = 'jwt_denylist'
end
RUBY
colorize '==> Created base JWT model '
########################################
# devise JWT config
########################################
run 'rm config/initializers/devise.rb'
run 'curl -L https://raw.githubusercontent.com/zakaryaa/rails-api-template/master/devise.rb > config/initializers/devise.rb'
colorize '==> devise.rb initializer updated'
########################################
# devise controller update
########################################
file './app/controllers/api/v1/user/registrations_controller.rb'
file './app/controllers/api/v1/user/sessions_controller.rb'
run 'curl -L https://raw.githubusercontent.com/zakaryaa/rails-api-template/master/registrations_controller.rb> app/controllers/api/v1/user/registrations_controller.rb'
run 'curl -L https://raw.githubusercontent.com/zakaryaa/rails-api-template/master/sessions_controller.rb> app/controllers/api/v1/user/sessions_controller.rb'
colorize '==> Update devise registration & session controllers '
########################################
# migrate
#######################################
rails_command 'db:migrate RAILS_ENV=development'
colorize '==> Db migrate '
########################################
# Dotenv
########################################
file '.env'
run "echo 'DEVISE_JWT_SECRET_KEY=#{SecureRandom.hex(64)}' > .env"
colorize '==> Set DEVISE_JWT_SECRET_KEY '
########################################
# Seed file
########################################
file 'db/seeds.rb', <<-RUBY
User.create(email: '[email protected]', password: "password")
RUBY
rails_command 'db:seed RAILS_ENV=development'
colorize '==> Create a sample User in DB & run seed '
# Clean up
########################################
run "rm -rf 'lib/assets/'"
run "rm -rf 'vendor/assets/'"
run "rm -rf 'app/helpers/'"
run "rm -rf 'app/views/layouts/application.hmtl.erb'"
# # Git
# ########################################
git :init
git add: '.'
# git commit: "-m 'Initial commit with devise template from https://github.com/zakaryaa/rails-api-template'"
end