/
dynamodb_store.rb
38 lines (33 loc) · 1.4 KB
/
dynamodb_store.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true
require 'aws-sessionstore-dynamodb'
require 'action_dispatch/middleware/session/abstract_store'
module ActionDispatch
module Session
# Uses the Dynamo DB Session Store implementation to create a class that
# extends ActionDispatch::Session. Rails will create a :dynamodb_store
# configuration for session_store from this class name.
#
# This class will use the Rails secret_key_base unless otherwise provided.
#
# Configuration can also be provided in YAML files from Rails config, either
# in "config/session_store.yml" or "config/session_store/#\\{Rails.env}.yml".
# Configuration files that are environment-specific will take precedence.
#
# @see https://docs.aws.amazon.com/sdk-for-ruby/aws-sessionstore-dynamodb/api/Aws/SessionStore/DynamoDB/Configuration.html
class DynamodbStore < Aws::SessionStore::DynamoDB::RackMiddleware
include StaleSessionCheck
include SessionObject
def initialize(app, options = {})
options[:config_file] ||= config_file if File.exist?(config_file)
options[:secret_key] ||= Rails.application.secret_key_base
super
end
private
def config_file
file = Rails.root.join("config/dynamo_db_session_store/#{Rails.env}.yml")
file = Rails.root.join('config/dynamo_db_session_store.yml') unless File.exist?(file)
file
end
end
end
end