add configuration class
This commit is contained in:
50
lib/config.rb
Normal file
50
lib/config.rb
Normal file
@ -0,0 +1,50 @@
|
||||
require "sensitive"
|
||||
|
||||
class Config
|
||||
attr_accessor :cat
|
||||
|
||||
attr_reader :jwt_secret, :session_secret
|
||||
|
||||
def initialize(prefix = ENV_PREFIX, jwt_secret = nil, session_secret = nil, cat = nil)
|
||||
@prefix = prefix
|
||||
@cat = cat
|
||||
|
||||
session_secret ||= ENV.fetch "SESSION_SECRET" do
|
||||
SecureRandom.hex SESSION_SECRET_HEX_LENGTH
|
||||
end
|
||||
|
||||
jwt_secret ||= fetch_env "JWT_SECRET" do
|
||||
SecureRandom.hex JWT_SECRET_HEX_LENGTH
|
||||
end
|
||||
|
||||
@session_secret = Sensitive.new session_secret
|
||||
@jwt_secret = Sensitive.new jwt_secret
|
||||
@cat ||= ENV.fetch "#{@prefix}_CAT", nil
|
||||
end
|
||||
|
||||
def fetch_env(name, &)
|
||||
ENV.fetch "#{@prefix}_#{name}", &
|
||||
end
|
||||
|
||||
def as_json(options = nil)
|
||||
{jwt_secret: jwt_secret, session_secret: @session_secret, cat: @cat}
|
||||
end
|
||||
|
||||
def to_json(options = nil)
|
||||
if options &&
|
||||
options.key?(:pretty) &&
|
||||
options[:pretty] == true
|
||||
JSON.pretty_generate as_json(options)
|
||||
else
|
||||
JSON.generate as_json(options)
|
||||
end
|
||||
end
|
||||
|
||||
def session_secret=(v)
|
||||
@session_secret = Sensitive.new v
|
||||
end
|
||||
|
||||
def jwt_secret=(v)
|
||||
@jwt_secret = Sensitive.new v
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user