Skip to content

Commit

Permalink
Remove nginx from default stack
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Nov 22, 2022
1 parent eee79d7 commit 776895b
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 95 deletions.
6 changes: 3 additions & 3 deletions app/controllers/api/v1/stream_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class StreamController < ApiController
before_action :find_stream

def new
if nginx_senfile?
if nginx_sendfile?
# Let nginx can get value of media_path dynamically in the nginx config,
# when use X-Accel-Redirect header to send file.
response.headers["X-Media-Path"] = Setting.media_path
Expand All @@ -23,8 +23,8 @@ def find_stream
@stream = Stream.new(song)
end

def nginx_senfile?
Rails.configuration.action_dispatch.x_sendfile_header == "X-Accel-Redirect"
def nginx_sendfile?
ENV.fetch("NGINX_SENDFILE", "false") == "true"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/transcoded_stream_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def new
def find_cache
return unless valid_cache?

if nginx_senfile?
if nginx_sendfile?
response.headers["X-Accel-Redirect"] = File.join("/private_cache_media", @stream.transcode_cache_file_path.sub(Stream::TRANSCODE_CACHE_DIRECTORY.to_s, ""))
end

Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache
config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX
# config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
Expand Down
2 changes: 0 additions & 2 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,4 @@
# config.action_view.annotate_rendered_view_with_filenames = true

config.action_controller.action_on_unpermitted_parameters = :raise

config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX
end
81 changes: 0 additions & 81 deletions config/nginx/nginx.conf

This file was deleted.

9 changes: 9 additions & 0 deletions config/nginx/sendfile.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
location /private_media/ {
internal;
alias $upstream_http_x_media_path/;
}

location /private_cache_media/ {
internal;
alias /app/tmp/cache/media_file/;
}
12 changes: 7 additions & 5 deletions test/controllers/api/v1/stream_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ class Api::V1::StreamControllerTest < ActionDispatch::IntegrationTest
end

test "should set header for nginx send file" do
get new_api_v1_stream_url(song_id: songs(:mp3_sample).id)
stub_env("NGINX_SENDFILE", "true") do
get new_api_v1_stream_url(song_id: songs(:mp3_sample).id)

assert_equal Setting.media_path, @response.get_header("X-Media-Path")
assert_equal "/private_media/artist1_album2.mp3", @response.get_header("X-Accel-Redirect")
assert_equal Setting.media_path, @response.get_header("X-Media-Path")
assert_equal "/private_media/artist1_album2.mp3", @response.get_header("X-Accel-Redirect")
end
end

test "should respond file data" do
get new_api_v1_stream_url(song_id: songs(:mp3_sample).id)
assert_equal binary_data(file_fixture("artist1_album2.mp3")), response.body
end

test "should respond file data when not set nginx send file header" do
Rails.configuration.action_dispatch.stub(:x_sendfile_header, "") do
test "should respond file data when set nginx send file header" do
stub_env("NGINX_SENDFILE", "true") do
get new_api_v1_stream_url(song_id: songs(:mp3_sample).id)
assert_equal binary_data(file_fixture("artist1_album2.mp3")), response.body
end
Expand Down
14 changes: 12 additions & 2 deletions test/controllers/api/v1/transcoded_stream_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ class Api::V1::TranscodedStreamControllerTest < ActionDispatch::IntegrationTest
assert_response :success

get new_api_v1_transcoded_stream_url(song_id: songs(:flac_sample).id)
assert_equal "/private_cache_media/2/ZmxhY19zYW1wbGVfbWQ1X2hhc2g=_128.mp3", @response.get_header("X-Accel-Redirect")
assert_equal "audio/mpeg", @response.get_header("Content-Type")
assert_equal binary_data(Stream.new(songs(:flac_sample)).transcode_cache_file_path), response.body
end

test "should send cached transcoded stream file when found cache and send file with nginx" do
get new_api_v1_transcoded_stream_url(song_id: songs(:flac_sample).id)
assert_response :success

stub_env("NGINX_SENDFILE", "true") do
get new_api_v1_transcoded_stream_url(song_id: songs(:flac_sample).id)
assert_equal "/private_cache_media/2/ZmxhY19zYW1wbGVfbWQ1X2hhc2g=_128.mp3", @response.get_header("X-Accel-Redirect")
assert_equal "audio/mpeg", @response.get_header("Content-Type")
end
end

test "should regenerate new cache when cache is invalid" do
Expand Down
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,12 @@ def with_forgery_protection
ensure
ActionController::Base.allow_forgery_protection = old
end

def stub_env(name, value)
old_value = ENV[name]
ENV[name] = value
yield
ensure
ENV[name] = old_value
end
end

0 comments on commit 776895b

Please sign in to comment.