-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.rb
211 lines (186 loc) · 5.51 KB
/
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
# frozen_string_literal: true
require 'fileutils'
require 'shellwords'
DEFAULT_DEVISE_AUTH_MODE = 'devise_jwt_strategy'
NO_QUESTIONS = ENV['INTERACTIVE'] == 'false'
OUTPUT_COLOR = %i[blue bold].freeze
# Copied from: https://github.com/mattbrictson/rails-template
# Add this template directory to source_paths so that Thor actions like
# copy_file and template resolve against our source files. If this file was
# invoked remotely via HTTP, that means the files are not present locally.
# In that case, use `git clone` to download them to a local temporary dir.
def add_template_repository_to_source_path
if __FILE__ =~ %r{\Ahttps?://}
require 'tmpdir'
source_paths.unshift(tempdir = Dir.mktmpdir('jumpstart-rails-api-'))
at_exit { FileUtils.remove_entry(tempdir) }
git clone: [
'--quiet',
'https://github.com/beyode/jumpstart-rails-api.git',
tempdir
].map(&:shellescape).join(' ')
if (branch = __FILE__[%r{jumpstart-rails-api/(.+)/template.rb}, 1])
Dir.chdir(tempdir) { git checkout: branch }
end
else
source_paths.unshift(File.dirname(__FILE__))
end
end
# Gems
def add_gems
gem 'devise', '~> 4.7', '>= 4.7.1'
gem 'jwt', '~> 2.2', '>= 2.2.1'
gem 'jsonapi-serializer', '~> 2.1.0'
gem 'dotenv-rails', '~> 2.7', '>= 2.7.5'
gem 'sidekiq', '~> 6.0', '>= 6.0.7'
gem 'whenever', '~> 1.0'
gem 'rack-cors'
gem 'simple_token_authentication', '~> 1.17'
gem_group :development, :test do
gem 'vcr', '~> 5.1'
gem 'foreman', '~> 0.87.1'
end
end
def install_devise
generate 'devise:install'
environment "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }",
env: 'development'
generate :devise, 'User', 'first_name:string', 'last_name:string', 'admin:boolean'
# route "root to: 'home#index'"
gsub_file 'config/initializers/devise.rb',
/ # config.secret_key = .+/,
' config.secret_key = Rails.application.credentials.secret_key_base'
end
def devise_jwt_strategy
# comment devise generated routes
comment_lines 'config/routes.rb', 'devise_for :users'
# add custom routes
insert_into_file 'config/routes.rb', after: 'Rails.application.routes.draw do' do
"
devise_for :users, skip: :all, controllers: {
sessions: 'api/v1/sessions'
}
devise_scope :user do
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :registration, only: ['create']
resources :sessions, only: %w[create destroy]
#resources :posts
end
end
end
"
end
# implement devise strategy
content = <<-RUBY
config.warden do |manager|
#manager.intercept_401 = false
manager.strategies.add :jwt, Devise::Strategies::JWT
manager.default_strategies(scope: :user).unshift :jwt
manager.failure_app = CustomFailure::CustomFailureApp
end
RUBY
insert_into_file('config/initializers/devise.rb',
"#{content}\n\n", before: '# ==> Mountable engine configurations')
# add class used by strategy
append_to_file 'config/initializers/devise.rb' do
"
module Devise
module Strategies
class JWT < Base
def valid?
request.headers['Authorization'].present?
end
def authenticate!
token = request.headers.fetch('Authorization', '').split(' ').last
payload = JsonWebToken.decode(token)
success! User.find(payload['sub'])
rescue ::JWT::ExpiredSignature
fail! 'Expired Token'
rescue ::JWT::DecodeError
fail! 'auth Token invalid'
end
end
end
end
module CustomFailure
class CustomFailureApp < Devise::FailureApp
def respond
if request.format == :json
self.status = 401
self.content_type = 'application/json'
self.response_body = {
errors: {
code: '401',
title: :unauthorized,
detail: i18n_message
}
}.to_json
end
end
end
end
"
end
end
def devise_simple_token_auth
# authenticable model
insert_into_file 'app/models/user.rb', after: 'class User < ApplicationRecord' do
"
acts_as_token_authenticatable
"
end
# add token column
generate(:migration, 'add_authentication_token_to_users', 'authentication_token:string{30}:uniq')
# allow controller to handle authentication
insert_into_file 'app/controllers/application_controller.rb', after: 'class ApplicationController < ActionController::API' do
"
acts_as_token_authentication_handler_for User
"
end
end
def auth_mode
say
if yes?('Use JWT instead of simple Token? [y/n]', OUTPUT_COLOR)
devise_jwt_strategy
else
devise_simple_token_auth
end
end
def copy_templates
copy_file 'Procfile'
copy_file 'Procfile.dev'
copy_file '.foreman'
directory 'app', force: true
directory 'config', force: true
end
def stop_spring
run 'spring stop'
end
def install_sidekiq
environment 'config.active_job.queue_adapter = :sidekiq', env: 'development'
end
# setup
add_template_repository_to_source_path
# Add gems
add_gems
after_bundle do
stop_spring
install_devise
copy_templates
install_sidekiq
NO_QUESTIONS ? eval(DEFAULT_DEVISE_AUTH_MODE) : auth_mode
# commit all to git
git :init
git add: '.'
# git commit will fail if user.email is not configured
begin
git commit: %( -m 'Initial commit')
rescue Exception => e
puts e.message
end
say
say 'Application generated successfully', OUTPUT_COLOR
say
say "cd #{app_name} to switch to app", OUTPUT_COLOR
end