Skip to content

Commit

Permalink
add puma config
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbeales committed Mar 26, 2018
1 parent 732678e commit 3ce1a87
Show file tree
Hide file tree
Showing 10 changed files with 1,119 additions and 61 deletions.
31 changes: 17 additions & 14 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
source 'https://rubygems.org'

gem 'activerecord', '~> 5.1.5' # ORM
gem 'acts-as-taggable-array-on', '~> 0.4.0' # adds tags
gem 'dotenv', '~> 2.2.1' # adds environment variables from .env files
gem 'acts-as-taggable-array-on', '~> 0.4.0', require: false # adds tags
gem 'bugsnag', '~> 6.6', require: false # online bug reporting
gem 'dotenv', '~> 2.2.1', require: false # adds environment variables from .env files
gem 'facets', '~> 3.1.0', require: false # additional core methods
gem 'jsonb_accessor', '~> 1.0.0' # adds methods to access Jsonb fields
gem 'jsonb_accessor', '~> 1.0.0', require: false # adds methods to access Jsonb fields
gem 'kiba', '~> 2.0.0', require: false # ETL
gem 'multi_json', '~> 1.13.1' # common interface to load json
gem 'oj', '~> 3.5.0' # faster json parsing
gem 'pg', '~> 1.0.0', platform: :ruby # for Postgres
gem 'mobility', '~> 0.5.1', require: false # translations
gem 'multi_json', '~> 1.13.1', require: false # common interface to load json
gem 'oj', '~> 3.5.0', require: false # faster json parsing
gem 'pg', '~> 1.0.0', platform: :ruby, require: false # for Postgres
gem 'puma', '~> 3.11.2', require: false # puma web server
gem 'rack-cache', '~> 1.7.1', require: false # http caching
gem 'rack-heartbeat', '~> 1.1.0' # provide /heartbeat URL
gem 'rack-protection', '~> 2.0.1', require: false # protect against web attacks
gem 'rack-ssl', '~> 1.4.1', require: false # ssl for rack in production
gem 'rake', '~> 12.3.1', require: false # so we can run Rake tasks
gem 'ralyxa', '~> 1.7.0' # ruby alexa framework
gem 'ralyxa', '~> 1.7.0', require: false # ruby alexa framework
gem 'rest-client', '~> 2.0.2', require: false # minimum version without security vulnerabilities
gem 'route_downcaser', '~> 1.2.1', require: false # case isnensitive URLs
gem 'sinatra', '~> 2.0.1', require: false # Web server
gem 'sinatra-activerecord', '~> 2.0.13', require: false # for Active Record models
gem 'sinatra-kittens', '~> 0.0.4' # kitten error page
gem 'sinatra_logger', '~> 0.1.0' # for logging
gem 'sinatra_logger', '~> 0.1.0', require: false # for logging
gem 'turnout', '~> 2.4.1', require: false # for maintenance mode
gem 'validates_email_format_of', '~>1.6.3', require: false # validates email format

group :test do
Expand All @@ -39,13 +46,9 @@ end
group :development do
gem 'guard', '~> 2.14.2', require: false # watch for changed files
gem 'guard-rspec', '~> 4.7.3', require: false # run rspec on change
gem 'wdm', '>= 0.1.0' if Gem.win_platform? # supports Guard watching files on Windows OS.
gem 'wdm', '>= 0.1.0', require: false if Gem.win_platform? # supports Guard watching files on Windows OS.
end

group :doc do
gem 'sdoc', '~> 1.0.0', require: false # static documentation
end

gem 'bugsnag', '~> 6.6' # online bug reporting
gem 'mobility', '~> 0.5.1' # translations
gem 'rest-client', '~> 2.0.2' # minimum version without security vulnerabilities
27 changes: 22 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
acts-as-taggable-array-on (0.4.1)
acts-as-taggable-array-on (0.4.2)
activerecord (>= 4)
activesupport (>= 4)
addressable (2.4.0)
Expand Down Expand Up @@ -127,8 +127,16 @@ GEM
json
websocket (~> 1.0)
rack (2.0.4)
rack-accept (0.4.5)
rack (>= 0.4)
rack-cache (1.7.1)
rack (>= 0.4)
rack-heartbeat (1.1.0)
rack
rack-protection (2.0.1)
rack
rack-ssl (1.4.1)
rack
rack-test (0.8.3)
rack (>= 1.0, < 3)
rainbow (3.0.0)
Expand All @@ -138,7 +146,7 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
rdoc (6.0.2)
rdoc (6.0.3)
request_store (1.4.1)
rack (>= 1.4)
rest-client (2.0.2)
Expand All @@ -150,6 +158,8 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
route_downcaser (1.2.1)
activesupport (>= 3.2)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
Expand Down Expand Up @@ -188,8 +198,6 @@ GEM
sinatra-activerecord (2.0.13)
activerecord (>= 3.2)
sinatra (>= 1.0)
sinatra-kittens (0.0.4)
sinatra (>= 1.0)
sinatra_logger (0.1.0)
term-ansicolor (1.6.0)
tins (~> 1.0)
Expand All @@ -209,6 +217,11 @@ GEM
typhoeus (~> 0.6, >= 0.6.8)
travis_check_rubies (0.2.5)
fspath (~> 3.0)
turnout (2.4.1)
i18n (~> 0.7)
rack (>= 1.3, < 3)
rack-accept (~> 0.4)
tilt (>= 1.4, < 3)
typhoeus (0.8.0)
ethon (>= 0.8.0)
tzinfo (1.2.5)
Expand Down Expand Up @@ -247,22 +260,26 @@ DEPENDENCIES
oj (~> 3.5.0)
pg (~> 1.0.0)
puma (~> 3.11.2)
rack-cache (~> 1.7.1)
rack-heartbeat (~> 1.1.0)
rack-protection (~> 2.0.1)
rack-ssl (~> 1.4.1)
rack-test (~> 0.8.3)
rake (~> 12.3.1)
ralyxa (~> 1.7.0)
rest-client (~> 2.0.2)
route_downcaser (~> 1.2.1)
rspec (~> 3.7.0)
rubocop (~> 0.54.0)
sdoc (~> 1.0.0)
simplecov (~> 0.16.1)
sinatra (~> 2.0.1)
sinatra-activerecord (~> 2.0.13)
sinatra-kittens (~> 0.0.4)
sinatra_logger (~> 0.1.0)
timecop (~> 0.9.1)
travis (~> 1.8.8)
travis_check_rubies (~> 0.2.5)
turnout (~> 2.4.1)
validates_email_format_of (~> 1.6.3)
wdm (>= 0.1.0)

Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require 'sinatra/activerecord'
require 'sinatra/activerecord/rake'
require 'rspec/core/rake_task'
require 'dotenv'
require 'turnout/rake_tasks' # rake maintenance:start, rake maintenance:end

Dotenv.load

Expand Down
65 changes: 53 additions & 12 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@

require 'sinatra'
require 'ralyxa'
require 'bugsnag'
require 'sinatra_logger'
require 'sinatra/kittens'

# require 'paper_trail'
# require 'paper_trail-sinatra'
require_relative 'config/db'
require_relative 'app/services/init'
require_relative 'app/models/init'

if ENV['RACK_ENV'] == ['development'] || ENV['RACK_ENV'] == ['production']
LOGGER ||= SinatraLogger::Loggers.file_logger('./log/sinatra.log')
LOGGER ||= SinatraLogger::Loggers.stdout_logger
end
configure { set :server, :puma }
set :app_file, __FILE__
set :root, File.dirname(__FILE__)
set :public_folder, File.dirname(__FILE__) + '/public'

# This class is the main entry point to Application.
# Takes an incoming Alexa requests and dispatches
Expand All @@ -35,14 +33,57 @@ class App < Sinatra::Base
# TODO: Enable Register PaperTrail when paper_trail gem in 9 and paper_trail-sinatra supports it
# to register Paper Trail auditing and version framework
# register PaperTrail::Sinatra
require 'route_downcaser'
use RouteDowncaser::DowncaseRouteMiddleware # case insensitive URLs

require 'sinatra_logger'
enable :logging
LOGGER ||= SinatraLogger::Loggers.file_logger('./log/sinatra.log')
LOGGER ||= SinatraLogger::Loggers.stdout_logger
use Rack::CommonLogger, LOGGER

configure :development, :production do
enable :logging
use Rack::CommonLogger, LOGGER
require 'rack/turnout'
use Rack::Turnout

# report errors to bugsnag.com
require 'bugsnag'
Bugsnag.configure do |config|
config.api_key = 'b27cf77d548381f51613fb5c142ae212'
config.app_version = '1.0'
config.auto_capture_sessions = true
config.project_root = '/var/www/mental-test'
config.send_environment = true
end
use Bugsnag::Rack
set :raise_errors, true
set :show_exceptions, false
end

configure do
register Sinatra::Kittens

configure :production do

require 'rack/ssl'
use Rack::SSL

require 'rack/cache'
use Rack::Cache
sha1, date = `git log HEAD~1..HEAD --pretty=format:%h^%ci`.strip.split('^')

before do
cache_control :public, :must_revalidate, max_age: 300
etag sha1
last_modified date
end
end

AlexaVerifier.configure do |config|
config.enabled = false # Disables all checks, even though we enable them individually below
config.verify_uri = true
config.verify_timeliness = true
config.verify_certificate = false
config.verify_signature = false
end

# Entry point for requests from Amazon Alexa.
# The incoming requests are dispatched to intents in the intents folder by Ralyxa.
post '/' do
Expand Down
File renamed without changes.
26 changes: 1 addition & 25 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,8 @@ require 'rubygems'
require 'bundler'
Bundler.require(:default, ENV['RACK_ENV'].to_sym)

require 'rack/ssl'
use Rack::SSL if Sinatra::Application.production?

require 'dotenv'
Dotenv.load # load environment variables from .env configuration file

# report errors to bugsnag.com
Bugsnag.configure do |config|
config.api_key = 'b27cf77d548381f51613fb5c142ae212'
config.app_version = '1.0'
config.auto_capture_sessions = true
config.project_root = '/var/www/mental-test'
config.send_environment = true
end

use Bugsnag::Rack
set :raise_errors, true
set :show_exceptions, false

AlexaVerifier.configure do |config|
config.enabled = false # Disables all checks, even though we enable them individually below
config.verify_uri = true
config.verify_timeliness = true
config.verify_certificate = false
config.verify_signature = false
end

require './app'
run App
run App.new
49 changes: 49 additions & 0 deletions config/puma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum, this matches the default thread size of Active Record.
#
threads_count = ENV.fetch('SINATRA_MAX_THREADS') { 5 }.to_i
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
#
port ENV.fetch('PORT') { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch('RACK_ENV') { 'production' }

unless Gem.win_platform?
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).

workers ENV.fetch('WEB_CONCURRENCY') { 2 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.

preload_app!

# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted this block will be run, if you are using `preload_app!`
# option you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, Ruby
# cannot share connections between processes.

on_worker_boot do
dbconfig = YAML.safe_load(File.read('config/database.yml'))
ActiveRecord::Base.establish_connection dbconfig[ENV['RACK_ENV']]
end
end
5 changes: 5 additions & 0 deletions spec/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
subject { App.new }

it { expect(subject).to be_a Sinatra::Wrapper }

it 'should not allow accessing the home page' do
get '/'
expect(last_response).not_to be_ok
end
end

0 comments on commit 3ce1a87

Please sign in to comment.