Skip to content

Commit

Permalink
Merge pull request blackcandy-org#226 from blackcandy-org/dev
Browse files Browse the repository at this point in the history
Add app config management
  • Loading branch information
aidewoode authored Nov 25, 2022
2 parents 065d48f + 5d3e48b commit 46af7ba
Show file tree
Hide file tree
Showing 32 changed files with 280 additions and 190 deletions.
24 changes: 0 additions & 24 deletions .devcontainer/Dockerfile

This file was deleted.

26 changes: 0 additions & 26 deletions .devcontainer/devcontainer.json

This file was deleted.

56 changes: 0 additions & 56 deletions .devcontainer/docker-compose.yml

This file was deleted.

4 changes: 0 additions & 4 deletions .env.template

This file was deleted.

14 changes: 6 additions & 8 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ gem "cssbundling-rails", "~> 1.1.0"
# Bundle and transpile JavaScript in Rails
gem "jsbundling-rails", "~> 1.0.0"

if ENV.fetch("DATABASE_ADAPTER", "sqlite") == "postgresql"
gem "pg", "~> 1.3.2"
else
gem "sqlite3", "~> 1.5.0"
end

# Use Puma as the app server
gem "puma", "~> 6.0.0"

Expand Down Expand Up @@ -65,6 +59,12 @@ gem "bcrypt", "~> 3.1.11"
# For sync on library changes
gem "listen", "~> 3.7.1"

# For postgresql database adapter
gem "pg", "~> 1.3.2"

# For sqlite database adapter
gem "sqlite3", "~> 1.5.0"

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

Expand All @@ -73,7 +73,6 @@ gem "bootsnap", "~> 1.10.3", require: false

group :development, :test do
gem "standard", "~> 1.7"
gem "dotenv-rails", "~> 2.8.1"
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[mri mingw x64_mingw]
end
Expand All @@ -88,7 +87,6 @@ group :development do
end

group :test do
# Adds support for Capybara system testing and selenium driver
gem "capybara", "~> 3.38.0"
gem "cuprite", "~> 0.14.3"
gem "webmock", "~> 3.14.0", require: false
Expand Down
7 changes: 2 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ GEM
irb (>= 1.3.6)
reline (>= 0.3.1)
docile (1.4.0)
dotenv (2.8.1)
dotenv-rails (2.8.1)
dotenv (= 2.8.1)
railties (>= 3.2)
erubi (1.11.0)
ferrum (0.13)
addressable (~> 2.5)
Expand Down Expand Up @@ -184,6 +180,7 @@ GEM
parallel (1.22.1)
parser (3.1.2.1)
ast (~> 2.4.1)
pg (1.3.5)
propshaft (0.6.4)
actionpack (>= 7.0.0)
activesupport (>= 7.0.0)
Expand Down Expand Up @@ -321,13 +318,13 @@ DEPENDENCIES
cssbundling-rails (~> 1.1.0)
cuprite (~> 0.14.3)
debug
dotenv-rails (~> 2.8.1)
httparty (~> 0.17.0)
jbuilder (~> 2.11.5)
jsbundling-rails (~> 1.0.0)
listen (~> 3.7.1)
memory_profiler (~> 0.9.13)
pagy (~> 5.6.6)
pg (~> 1.3.2)
propshaft (~> 0.6.4)
puma (~> 6.0.0)
rails (~> 7.0.0)
Expand Down
6 changes: 1 addition & 5 deletions app/controllers/api/v1/stream_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ def find_stream
@stream = Stream.new(song)
end

def nginx_sendfile?
ENV.fetch("NGINX_SENDFILE", "false") == "true"
end

def send_local_file(file_path)
if nginx_sendfile?
if BlackCandy::Config.nginx_sendfile?
set_nginx_header

send_file file_path
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ApplicationController < ActionController::Base
before_action :find_current_user
before_action :require_login

rescue_from BlackCandyError::Forbidden do
rescue_from BlackCandy::Error::Forbidden do
respond_to do |format|
format.js { head :forbidden }
format.json { head :forbidden }
Expand Down Expand Up @@ -75,7 +75,7 @@ def require_login
end

def require_admin
raise BlackCandyError::Forbidden unless is_admin?
raise BlackCandy::Error::Forbidden unless is_admin?
end

def logout_current_user
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/playable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Playable
end

def play
raise BlackCandyError::Forbidden if @song_ids.blank?
raise BlackCandy::Error::Forbidden if @song_ids.blank?

@playlist = Current.user.current_playlist
@playlist.replace(@song_ids)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def show
end

def update
raise BlackCandyError::Forbidden unless is_admin?
raise BlackCandy::Error::Forbidden unless is_admin?
setting = Setting.instance

if setting.update(setting_params)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def find_user
end

def auth_user
raise BlackCandyError::Forbidden unless @user == Current.user
raise BlackCandy::Error::Forbidden unless @user == Current.user
end
end
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def update

def destroy
# Avoit user delete self
raise BlackCandyError::Forbidden if @user == Current.user
raise BlackCandy::Error::Forbidden if @user == Current.user

@user.destroy
flash.now[:success] = t("success.delete")
Expand All @@ -50,7 +50,7 @@ def find_user
end

def auth_user
raise BlackCandyError::Forbidden unless @user == Current.user || is_admin?
raise BlackCandy::Error::Forbidden unless @user == Current.user || is_admin?
end

def user_params
Expand Down
9 changes: 0 additions & 9 deletions app/lib/black_candy_error.rb

This file was deleted.

4 changes: 3 additions & 1 deletion app/models/concerns/global_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def has_setting(setting, type: :string, default: nil)

define_singleton_method(setting) do
setting_value = instance.send(setting)
setting_value || default || ENV[setting.to_s.upcase]
default_value = default.is_a?(Proc) ? default.call : default

setting_value || default_value
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Setting < ApplicationRecord

AVAILABLE_BITRATE_OPTIONS = [128, 192, 320].freeze

has_setting :media_path
has_setting :media_path, default: proc { BlackCandy::Config.media_path }
has_setting :discogs_token
has_setting :transcode_bitrate, type: :integer, default: 128
has_setting :allow_transcode_lossless, type: :boolean, default: false
Expand Down
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
require "action_cable/engine"
require "rails/test_unit/railtie"

require_relative "../lib/black_candy/config"
require_relative "../lib/black_candy/error"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
Expand Down
4 changes: 2 additions & 2 deletions config/cable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ development:
test:
adapter: test

<% if ENV["REDIS_URL"].present? || ENV["REDIS_CABLE_URL"].present? %>
<% if BlackCandy::Config.redis_cable_url.present? %>
production:
adapter: redis
url: <%= ENV.fetch("REDIS_CABLE_URL", ENV["REDIS_URL"]) %>
url: <%= BlackCandy::Config.redis_cable_url %>
channel_prefix: black_candy_production
<% else %>
production:
Expand Down
4 changes: 2 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ test:
database: db/test.sqlite3


<% if ENV.fetch("DATABASE_ADAPTER", "sqlite") == "postgresql" %>
<% if BlackCandy::Config.database_adapter == "postgresql" %>
production:
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= ENV['DATABASE_URL'] %>
url: <%= BlackCandy::Config.database_url %>
<% else %>
production:
<<: *default
Expand Down
6 changes: 2 additions & 4 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@
config.log_tags = [:request_id]

# Use a different cache store in production.
config.cache_store = (ENV["REDIS_URL"].present? || ENV["REDIS_CACHE_URL"].present?) ?
[:redis_cache_store, {url: ENV.fetch("REDIS_CACHE_URL", ENV["REDIS_URL"])}] :
[:file_store, "#{root}/tmp/cache/"]
config.cache_store = BlackCandy::Config.redis_cache_url.present? ? [:redis_cache_store, {url: BlackCandy::Config.redis_cache_url}] : [:file_store, "#{root}/tmp/cache/"]

# Use a real queuing backend for Active Job (and separate queues per environment).
config.active_job.queue_adapter = (ENV["REDIS_URL"].present? || ENV["REDIS_SIDEKIQ_URL"].present?) ? :sidekiq : :async
config.active_job.queue_adapter = BlackCandy::Config.redis_sidekiq_url.present? ? :sidekiq : :async

config.active_job.queue_name_prefix = "black_candy_production"

Expand Down
6 changes: 3 additions & 3 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
if Rails.configuration.active_job.queue_adapter == :sidekiq
if BlackCandy::Config.redis_sidekiq_url.present?
Sidekiq.configure_server do |config|
config.redis = {url: ENV.fetch("REDIS_SIDEKIQ_URL", ENV["REDIS_URL"])}
config.redis = {url: BlackCandy::Config.redis_sidekiq_url}
end

Sidekiq.configure_client do |config|
config.redis = {url: ENV.fetch("REDIS_SIDEKIQ_URL", ENV["REDIS_URL"])}
config.redis = {url: BlackCandy::Config.redis_sidekiq_url}
end
end
15 changes: 3 additions & 12 deletions config/puma.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
embedded_sidekiq = ENV.fetch("EMBEDDED_SIDEKIQ", "false") == "true"

# Accroding to the documentation, we should keep embedded sidekiq concurrency very low, i.e. 1-2
embedded_sidekiq_concurrency = [ENV.fetch("EMBEDDED_SIDEKIQ_CONCURRENCY", 2), 2].min
# A good rule of thumb is that your puma threads + sidekiq concurrency should never be greater than 5
max_threads_for_embedded_sidekiq = 5
require_relative "../lib/black_candy/config"

# 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 = embedded_sidekiq ? (max_threads_for_embedded_sidekiq - embedded_sidekiq_concurrency) : ENV.fetch("RAILS_MAX_THREADS", 5)
min_threads_count = embedded_sidekiq ? 1 : ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }

threads min_threads_count, max_threads_count
threads BlackCandy::Config.puma_min_threads_count, BlackCandy::Config.puma_max_threads_count

# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
Expand Down Expand Up @@ -49,7 +40,7 @@
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

if embedded_sidekiq
if BlackCandy::Config.embedded_sidekiq?
workers 2

# Preloading the application is necessary to ensure
Expand Down
Loading

0 comments on commit 46af7ba

Please sign in to comment.