From 1d8c3828a3c792b0eaa2e14288bbf4d432e7d3d2 Mon Sep 17 00:00:00 2001 From: Ryan W Date: Sat, 30 Sep 2023 10:32:08 -0700 Subject: [PATCH] Clean up templates from a fresh Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first. Type 'rails' for help. --- Gemfile | 16 ++++---- Gemfile.lock | 18 +++------ Rakefile | 2 +- app/mailers/application_mailer.rb | 4 +- app/views/layouts/mailer.html.erb | 2 +- bin/bundle | 63 +++++++++++++------------------ bin/docker-entrypoint | 8 ++++ config.ru | 2 +- config/database.yml | 2 - config/locales/en.yml | 24 ++++++------ config/puma.rb | 42 +++++++++------------ config/routes.rb | 8 +++- db/seeds.rb | 10 +++-- 13 files changed, 92 insertions(+), 109 deletions(-) create mode 100755 bin/docker-entrypoint diff --git a/Gemfile b/Gemfile index 8ff1cc2..2b79e3e 100644 --- a/Gemfile +++ b/Gemfile @@ -21,10 +21,10 @@ gem "puma", "~> 6.3" # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] gem "importmap-rails" -# Hotwire"s SPA-like page accelerator [https://turbo.hotwired.dev] +# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] gem "turbo-rails" -# Hotwire"s modest JavaScript framework [https://stimulus.hotwired.dev] +# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] gem "stimulus-rails" # Build JSON APIs with ease [https://github.com/rails/jbuilder] @@ -40,16 +40,13 @@ gem "redis", "~> 5.0" # gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby] +gem "tzinfo-data", platforms: %i[ windows jruby ] # Reduces boot times through caching; required in config/boot.rb gem "bootsnap", require: false -# Use Sass to process CSS -# gem "sassc-rails", "~> 2.1" - # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] -gem "image_processing", "~> 1.12" +gem "image_processing", "~> 1.2" gem "brakeman" gem "bundler-audit" @@ -57,7 +54,7 @@ gem "rubocop-rails" group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem - gem "debug", "1.8.0", platforms: %i[ mri mingw x64_mingw ] + gem "debug", platforms: %i[ mri windows ] gem "rspec-rails" gem "rubocop-rspec" end @@ -71,11 +68,12 @@ group :development do # Speed up commands on slow machines / big apps [https://github.com/rails/spring] # gem "spring" + + gem "error_highlight", ">= 0.4.0", platforms: [:ruby] end group :test do # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing] gem "capybara" gem "selenium-webdriver" - gem "webdrivers" end diff --git a/Gemfile.lock b/Gemfile.lock index c7fffbc..cc5466a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -106,6 +106,7 @@ GEM diff-lcs (1.5.0) drb (2.1.1) ruby2_keywords + error_highlight (0.5.1) erubi (1.12.0) ffi (1.16.2) globalid (1.2.1) @@ -156,8 +157,6 @@ GEM racc (~> 1.4) nokogiri (1.15.4-arm64-darwin) racc (~> 1.4) - nokogiri (1.15.4-x86_64-darwin) - racc (~> 1.4) nokogiri (1.15.4-x86_64-linux) racc (~> 1.4) parallel (1.23.0) @@ -298,10 +297,6 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) - webdrivers (5.2.0) - nokogiri (~> 1.6) - rubyzip (>= 1.3.0) - selenium-webdriver (~> 4.0) webrick (1.8.1) websocket (1.2.10) websocket-driver (0.7.6) @@ -313,10 +308,7 @@ GEM PLATFORMS aarch64-linux - arm64-darwin-20 - arm64-darwin-21 arm64-darwin-22 - x86_64-darwin-22 x86_64-linux DEPENDENCIES @@ -324,8 +316,9 @@ DEPENDENCIES brakeman bundler-audit capybara - debug (= 1.8.0) - image_processing (~> 1.12) + debug + error_highlight (>= 0.4.0) + image_processing (~> 1.2) importmap-rails jbuilder pg (~> 1.5) @@ -342,10 +335,9 @@ DEPENDENCIES turbo-rails tzinfo-data web-console - webdrivers RUBY VERSION ruby 3.2.2p53 BUNDLED WITH - 2.4.19 + 2.4.20 diff --git a/Rakefile b/Rakefile index 488c551..d2a78aa 100644 --- a/Rakefile +++ b/Rakefile @@ -3,6 +3,6 @@ # Add your own tasks in files placed in lib/tasks ending in .rake, # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. -require_relative 'config/application' +require_relative "config/application" Rails.application.load_tasks diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index d84cb6e..5cc63a0 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true class ApplicationMailer < ActionMailer::Base - default from: 'from@example.com' - layout 'mailer' + default from: "from@example.com" + layout "mailer" end diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb index cbd34d2..3aac900 100644 --- a/app/views/layouts/mailer.html.erb +++ b/app/views/layouts/mailer.html.erb @@ -1,7 +1,7 @@ - + diff --git a/bin/bundle b/bin/bundle index 73ec947..42c7fd7 100755 --- a/bin/bundle +++ b/bin/bundle @@ -8,46 +8,46 @@ # this file is here to facilitate running it. # -require 'rubygems' +require "rubygems" m = Module.new do module_function def invoked_as_script? - File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__) + File.expand_path($0) == File.expand_path(__FILE__) end def env_var_version - ENV['BUNDLER_VERSION'] + ENV["BUNDLER_VERSION"] end def cli_arg_version return unless invoked_as_script? # don't want to hijack other binstubs - return unless 'update'.start_with?(ARGV.first || ' ') # must be running `bundle update` - + return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update` bundler_version = nil update_index = nil ARGV.each_with_index do |a, i| - bundler_version = a if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN + if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN + bundler_version = a + end next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ - - bundler_version = Regexp.last_match(1) + bundler_version = $1 update_index = i end bundler_version end def gemfile - gemfile = ENV['BUNDLE_GEMFILE'] + gemfile = ENV["BUNDLE_GEMFILE"] return gemfile if gemfile && !gemfile.empty? - File.expand_path('../Gemfile', __dir__) + File.expand_path("../Gemfile", __dir__) end def lockfile lockfile = case File.basename(gemfile) - when 'gems.rb' then gemfile.sub(/\.rb$/, gemfile) + when "gems.rb" then gemfile.sub(/\.rb$/, ".locked") else "#{gemfile}.lock" end File.expand_path(lockfile) @@ -55,52 +55,41 @@ m = Module.new do def lockfile_version return unless File.file?(lockfile) - lockfile_contents = File.read(lockfile) return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ - Regexp.last_match(1) end - def bundler_version - @bundler_version ||= - env_var_version || cli_arg_version || - lockfile_version + def bundler_requirement + @bundler_requirement ||= + env_var_version || + cli_arg_version || + bundler_requirement_for(lockfile_version) end - def bundler_requirement - return "#{Gem::Requirement.default}.a" unless bundler_version + def bundler_requirement_for(version) + return "#{Gem::Requirement.default}.a" unless version - bundler_gem_version = Gem::Version.new(bundler_version) + bundler_gem_version = Gem::Version.new(version) - requirement = bundler_gem_version.approximate_recommendation - - return requirement unless Gem::Version.new(Gem::VERSION) < Gem::Version.new('2.7.0') - - requirement += '.a' if bundler_gem_version.prerelease? - - requirement + bundler_gem_version.approximate_recommendation end def load_bundler! - ENV['BUNDLE_GEMFILE'] ||= gemfile + ENV["BUNDLE_GEMFILE"] ||= gemfile activate_bundler end def activate_bundler gem_error = activation_error_handling do - gem 'bundler', bundler_requirement + gem "bundler", bundler_requirement end return if gem_error.nil? - require_error = activation_error_handling do - require 'bundler/version' + require "bundler/version" end - if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION)) - return - end - + return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION)) warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`" exit 42 end @@ -115,4 +104,6 @@ end m.load_bundler! -load Gem.bin_path('bundler', 'bundle') if m.invoked_as_script? +if m.invoked_as_script? + load Gem.bin_path("bundler", "bundle") +end diff --git a/bin/docker-entrypoint b/bin/docker-entrypoint new file mode 100755 index 0000000..dffd4ba --- /dev/null +++ b/bin/docker-entrypoint @@ -0,0 +1,8 @@ +#!/bin/bash -e + +# If running the rails server then create or migrate existing database +if [ "${*}" == "./bin/rails server" ]; then + ./bin/rails db:prepare +fi + +exec "${@}" diff --git a/config.ru b/config.ru index 6dc8321..2e03084 100644 --- a/config.ru +++ b/config.ru @@ -2,7 +2,7 @@ # This file is used by Rack-based servers to start the application. -require_relative 'config/environment' +require_relative "config/environment" run Rails.application Rails.application.load_server diff --git a/config/database.yml b/config/database.yml index 37db827..33a842c 100644 --- a/config/database.yml +++ b/config/database.yml @@ -4,8 +4,6 @@ # gem install pg # On macOS with Homebrew: # gem install pg -- --with-pg-config=/usr/local/bin/pg_config -# On macOS with MacPorts: -# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config # On Windows: # gem install pg # Choose the win32 build. diff --git a/config/locales/en.yml b/config/locales/en.yml index 8ca56fc..6c349ae 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,6 +1,6 @@ -# Files in the config/locales directory are used for internationalization -# and are automatically loaded by Rails. If you want to use locales other -# than English, add the necessary files in this directory. +# Files in the config/locales directory are used for internationalization and +# are automatically loaded by Rails. If you want to use locales other than +# English, add the necessary files in this directory. # # To use the locales, use `I18n.t`: # @@ -16,18 +16,16 @@ # # This would use the information in config/locales/es.yml. # -# The following keys must be escaped otherwise they will not be retrieved by -# the default I18n backend: +# To learn more about the API, please read the Rails Internationalization guide +# at https://guides.rubyonrails.org/i18n.html. # -# true, false, on, off, yes, no +# Be aware that YAML interprets the following case-insensitive strings as +# booleans: `true`, `false`, `on`, `off`, `yes`, `no`. Therefore, these strings +# must be quoted to be interpreted as strings. For example: # -# Instead, surround them with single quotes. -# -# en: -# "true": "foo" -# -# To learn more, please read the Rails Internationalization guide -# available at https://guides.rubyonrails.org/i18n.html. +# en: +# "yes": yup +# enabled: "ON" en: hello: "Hello world" diff --git a/config/puma.rb b/config/puma.rb index c990fa9..40efb18 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -1,45 +1,37 @@ # frozen_string_literal: true +# This configuration file will be evaluated by Puma. The top-level methods that +# are invoked here are part of Puma's configuration DSL. For more information +# about methods provided by the DSL, see https://puma.io/puma/Puma/DSL.html. + # 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. -# -max_threads_count = ENV.fetch('RAILS_MAX_THREADS', 5) -min_threads_count = ENV.fetch('RAILS_MIN_THREADS') { max_threads_count } +max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } +min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } threads min_threads_count, max_threads_count +# Specifies that the worker count should equal the number of processors in production. +if ENV["RAILS_ENV"] == "production" + require "concurrent-ruby" + worker_count = Integer(ENV.fetch("WEB_CONCURRENCY") { Concurrent.physical_processor_count }) + workers worker_count if worker_count > 1 +end + # Specifies the `worker_timeout` threshold that Puma will use to wait before # terminating a worker in development environments. -# -worker_timeout 3600 if ENV.fetch('RAILS_ENV', 'development') == 'development' +worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" # Specifies the `port` that Puma will listen on to receive requests; default is 3000. -# -port ENV.fetch('PORT', 3000) +port ENV.fetch("PORT") { 3000 } # Specifies the `environment` that Puma will run in. -# -environment ENV.fetch('RAILS_ENV', 'development') +environment ENV.fetch("RAILS_ENV") { "development" } # Specifies the `pidfile` that Puma will use. -pidfile ENV.fetch('PIDFILE', 'tmp/pids/server.pid') - -# Specifies the number of `workers` to boot in clustered mode. -# Workers are forked web server 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") { 4 } - -# 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. -# -preload_app! +pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } # Allow puma to be restarted by `bin/rails restart` command. plugin :tmp_restart diff --git a/config/routes.rb b/config/routes.rb index 79a5694..1a45f06 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,15 @@ # frozen_string_literal: true Rails.application.routes.draw do - get 'welcome/index' + get "welcome/index" + + # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. + # Can be used by load balancers and uptime monitors to verify that the app is live. + get "up" => "rails/health#show", as: :rails_health_check resources :articles do resources :comments end - root 'welcome#index' + root "welcome#index" end diff --git a/db/seeds.rb b/db/seeds.rb index 0664d1b..07b11e8 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,8 +1,10 @@ # frozen_string_literal: true -# This file should contain all the record creation needed to seed the database with its default values. +# This file should ensure the existence of records required to run the application in every environment (production, +# development, test). The code here should be idempotent so that it can be executed at any point in every environment. # The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). # -# Examples: +# Example: # -# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) -# Character.create(name: "Luke", movie: movies.first) +# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name| +# MovieGenre.find_or_create_by!(name: genre_name) +# end