Skip to content

Commit

Permalink
Merge pull request #10 from SonicGarden/interu-public-files
Browse files Browse the repository at this point in the history
[review] メンテモードでもpublicディレクトリ配下のコンテンツを返すように
  • Loading branch information
interu authored Sep 6, 2023
2 parents 0622b04 + 6308e91 commit 03e8708
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [0.1.6](https://github.com/SonicGarden/sg_fargate_rails/compare/v0.1.5...v0.1.6)

- メンテナンスモード時でも public ディレクトリ配下のコンテンツは配信するように

## [0.1.5](https://github.com/SonicGarden/sg_fargate_rails/compare/v0.1.4...v0.1.5)

- アクセス元IPアドレスの制限で複数IPアドレスを指定可能に
Expand Down
13 changes: 12 additions & 1 deletion lib/sg_fargate_rails/maintenance.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rack'
require 'rack/request'
require 'find'

module SgFargateRails
class Maintenance
Expand All @@ -8,7 +9,7 @@ def initialize(app, options = {})
end

def call(env)
if maintenance_mode?(env)
if maintenance_mode?(env) && !public_file_access?(env)
headers = { 'Content-Type' => 'text/html' }
[503, headers, File.open(maintenance_file_path)]
else
Expand All @@ -22,6 +23,16 @@ def maintenance_mode?(env)
env['HTTP_X_SG_FARGATE_RAILS_MAINTENANCE'].present?
end

# NOTE: cloudfrontでcacheにヒットしなかった場合の対策用
def public_file_access?(env)
@public_files ||= public_files
@public_files.include?(env['PATH_INFO'])
end

def public_files
Find.find(Rails.root.join('public')).select { |f| File.file?(f) }.map { |f| f.remove(Rails.root.join('public').to_s) }
end

def maintenance_file_path
Rails.public_path.join('503.html')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sg_fargate_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module SgFargateRails
VERSION = "0.1.5"
VERSION = "0.1.6"
end

0 comments on commit 03e8708

Please sign in to comment.