chore: ability to run Chatwoot as API only server (#2344)

- Ability to run chatwoot as an API only server
- Removes action cable testing gem since it is merged to rails 6
This commit is contained in:
Sojan Jose 2021-05-27 20:07:21 +05:30 committed by GitHub
parent 9c555e70c9
commit 3b39eb3e33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 11 deletions

View file

@ -143,6 +143,11 @@ USE_INBOX_AVATAR_FOR_BOT=true
# maxmindb api key to use geoip2 service # maxmindb api key to use geoip2 service
# IP_LOOKUP_API_KEY= # IP_LOOKUP_API_KEY=
## Running chatwoot as an API only server
## setting this value to true will disable the frontend dashboard endpoints
# CW_API_ONLY_SERVER=false
## Development Only Config ## Development Only Config
# if you want to use letter_opener for local emails # if you want to use letter_opener for local emails
# LETTER_OPENER=true # LETTER_OPENER=true

View file

@ -132,8 +132,6 @@ group :test do
end end
group :development, :test do group :development, :test do
# locking until https://github.com/codeclimate/test-reporter/issues/418 is resolved
gem 'action-cable-testing'
gem 'bundle-audit', require: false gem 'bundle-audit', require: false
gem 'byebug', platform: :mri gem 'byebug', platform: :mri
gem 'factory_bot_rails' gem 'factory_bot_rails'

View file

@ -16,8 +16,6 @@ GIT
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
action-cable-testing (0.6.1)
actioncable (>= 5.0)
actioncable (6.0.3.7) actioncable (6.0.3.7)
actionpack (= 6.0.3.7) actionpack (= 6.0.3.7)
nio4r (~> 2.0) nio4r (~> 2.0)
@ -613,7 +611,6 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
action-cable-testing
activerecord-import activerecord-import
acts-as-taggable-on acts-as-taggable-on
administrate administrate

View file

@ -76,4 +76,13 @@ Rails.application.configure do
Bullet.bullet_logger = true Bullet.bullet_logger = true
Bullet.rails_logger = true Bullet.rails_logger = true
end end
# ref: https://github.com/cyu/rack-cors
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '/packs/*', headers: :any, methods: [:get, :options]
resource '*', headers: :any, methods: :any, expose: ['access-token', 'client', 'uid', 'expiry']
end
end
end end

View file

@ -110,10 +110,14 @@ Rails.application.configure do
# font cors issue with CDN # font cors issue with CDN
# Ref: https://stackoverflow.com/questions/56960709/rails-font-cors-policy # Ref: https://stackoverflow.com/questions/56960709/rails-font-cors-policy
# ref: https://github.com/cyu/rack-cors
config.middleware.insert_before 0, Rack::Cors do config.middleware.insert_before 0, Rack::Cors do
allow do allow do
origins '*' origins '*'
resource '/packs/*', headers: :any, methods: [:get, :options] resource '/packs/*', headers: :any, methods: [:get, :options]
if ActiveModel::Type::Boolean.new.cast(ENV.fetch('CW_API_ONLY_SERVER', false))
resource '*', headers: :any, methods: :any, expose: ['access-token', 'client', 'uid', 'expiry']
end
end end
end end
end end

View file

@ -8,14 +8,19 @@ Rails.application.routes.draw do
token_validations: 'devise_overrides/token_validations' token_validations: 'devise_overrides/token_validations'
}, via: [:get, :post] }, via: [:get, :post]
root to: 'dashboard#index' ## renders the frontend paths only if its not an api only server
if ActiveModel::Type::Boolean.new.cast(ENV.fetch('CW_API_ONLY_SERVER', false))
root to: 'api#index'
else
root to: 'dashboard#index'
get '/app', to: 'dashboard#index' get '/app', to: 'dashboard#index'
get '/app/*params', to: 'dashboard#index' get '/app/*params', to: 'dashboard#index'
get '/app/accounts/:account_id/settings/inboxes/new/twitter', to: 'dashboard#index', as: 'app_new_twitter_inbox' get '/app/accounts/:account_id/settings/inboxes/new/twitter', to: 'dashboard#index', as: 'app_new_twitter_inbox'
get '/app/accounts/:account_id/settings/inboxes/new/:inbox_id/agents', to: 'dashboard#index', as: 'app_twitter_inbox_agents' get '/app/accounts/:account_id/settings/inboxes/new/:inbox_id/agents', to: 'dashboard#index', as: 'app_twitter_inbox_agents'
resource :widget, only: [:show] resource :widget, only: [:show]
end
get '/api', to: 'api#index' get '/api', to: 'api#index'
namespace :api, defaults: { format: 'json' } do namespace :api, defaults: { format: 'json' } do

View file

@ -17,4 +17,18 @@ describe '/app/login', type: :request do
ENV['DEFAULT_LOCALE'] = 'en' ENV['DEFAULT_LOCALE'] = 'en'
end end
end end
# Routes are loaded once on app start
# hence Rails.application.reload_routes! is used in this spec
# ref : https://stackoverflow.com/a/63584877/939299
context 'with CW_API_ONLY_SERVER true' do
it 'returns 404' do
ENV['CW_API_ONLY_SERVER'] = 'true'
Rails.application.reload_routes!
get '/app/login'
expect(response).to have_http_status(:not_found)
ENV['CW_API_ONLY_SERVER'] = nil
Rails.application.reload_routes!
end
end
end end

View file

@ -66,6 +66,7 @@ RSpec.configure do |config|
config.include SlackStubs config.include SlackStubs
config.include Devise::Test::IntegrationHelpers, type: :request config.include Devise::Test::IntegrationHelpers, type: :request
config.include ActiveSupport::Testing::TimeHelpers config.include ActiveSupport::Testing::TimeHelpers
config.include ActionCable::TestHelper
end end
Shoulda::Matchers.configure do |config| Shoulda::Matchers.configure do |config|