fix REDIS_URL unix socket path parsing for relative paths

pull/19/head
kouhai dev 2022-11-25 21:31:39 -06:00
parent bd0da3a499
commit c75ac19c8a
4 changed files with 31 additions and 11 deletions

View File

@ -1,6 +1,7 @@
LOCAL_DOMAIN=localhost LOCAL_DOMAIN=localhost
ALTERNATE_DOMAINS=mastodon.internal
DB_HOST=$(pwd)/data/postgres DB_HOST=$(pwd)/data/postgres
DB_USER=mastodon DB_USER=mastodon
DB_NAME=mastodon_dev DB_NAME=mastodon_dev
REDIS_URL=redis://$(pwd)/data/redis/redis-dev.sock REDIS_URL=unix://./data/redis/redis-dev.sock

View File

@ -7,4 +7,4 @@ LOCAL_HTTPS=true
DB_HOST=$(pwd)/data/postgres DB_HOST=$(pwd)/data/postgres
DB_USER=mastodon DB_USER=mastodon
DB_NAME=mastodon_dev DB_NAME=mastodon_dev
REDIS_URL=redis://$(pwd)/data/redis/redis-test.sock REDIS_URL=unix://./data/redis/redis-dev.sock

View File

@ -1,17 +1,36 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'pathname'
def setup_redis_env_url(prefix = nil, defaults = true) def setup_redis_env_url(prefix = nil, defaults = true)
prefix = prefix.to_s.upcase + '_' unless prefix.nil? prefix = "#{prefix.to_s.upcase}_" unless prefix.nil?
prefix = '' if prefix.nil? prefix = '' if prefix.nil?
redis_url_key = "#{prefix}REDIS_URL"
return if ENV[prefix + 'REDIS_URL'].present? if ENV[redis_url_key].present?
conn = +ENV["#{prefix}REDIS_URL"].sub(/redis:\/\//i, '')
password = ENV.fetch(prefix + 'REDIS_PASSWORD') { '' if defaults } # Strip any prefixing `unix://`
host = ENV.fetch(prefix + 'REDIS_HOST') { 'localhost' if defaults } unix = !conn.sub!(/^unix:\/\//i, '').nil?
port = ENV.fetch(prefix + 'REDIS_PORT') { 6379 if defaults } # Strip any prefixing `./`
db = ENV.fetch(prefix + 'REDIS_DB') { 0 if defaults } unix |= conn.sub!(/^(\.\/)+/, '')
unix |= conn.start_with?('/')
ENV[prefix + 'REDIS_URL'] = begin if unix
pn = Pathname.new(conn)
pn = Pathname.getwd / pn if pn.relative?
ENV[redis_url_key] = "unix://#{pn}"
end
return
end
password = ENV.fetch("#{prefix}REDIS_PASSWORD") { '' if defaults }
host = ENV.fetch("#{prefix}REDIS_HOST") { 'localhost' if defaults }
port = ENV.fetch("#{prefix}REDIS_PORT") { 6379 if defaults }
db = ENV.fetch("#{prefix}REDIS_DB") { 0 if defaults }
ENV["#{prefix}REDIS_URL"] = begin
if [password, host, port, db].all?(&:nil?) if [password, host, port, db].all?(&:nil?)
ENV['REDIS_URL'] ENV['REDIS_URL']
else else
@ -27,7 +46,7 @@ setup_redis_env_url(:cache, false)
setup_redis_env_url(:sidekiq, false) setup_redis_env_url(:sidekiq, false)
namespace = ENV.fetch('REDIS_NAMESPACE', nil) namespace = ENV.fetch('REDIS_NAMESPACE', nil)
cache_namespace = namespace ? namespace + '_cache' : 'cache' cache_namespace = namespace ? "#{namespace}_cache" : 'cache'
sidekiq_namespace = namespace sidekiq_namespace = namespace
REDIS_CACHE_PARAMS = { REDIS_CACHE_PARAMS = {

View File

@ -18,7 +18,7 @@ const env = process.env.NODE_ENV || 'development';
const alwaysRequireAuth = process.env.LIMITED_FEDERATION_MODE === 'true' || process.env.WHITELIST_MODE === 'true' || process.env.AUTHORIZED_FETCH === 'true'; const alwaysRequireAuth = process.env.LIMITED_FEDERATION_MODE === 'true' || process.env.WHITELIST_MODE === 'true' || process.env.AUTHORIZED_FETCH === 'true';
dotenv.config({ dotenv.config({
path: env === 'production' ? '.env.production' : '.env', path: env === 'production' || env === 'development' ? `.env.${env}` : '.env',
}); });
log.level = process.env.LOG_LEVEL || 'verbose'; log.level = process.env.LOG_LEVEL || 'verbose';