This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.rb
109 lines (92 loc) · 2.72 KB
/
environment.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true
require "active_record"
require "docsplit"
require "filemagic"
require "fileutils"
require "json"
require "logger"
require "ostruct"
require "rchardet"
require "resque"
require "resque/server"
require "rest_client"
require "sanitize"
require "gnfinder"
require_relative "models/hash_serializer"
require_relative "models/params"
require_relative "models/result_builder"
require_relative "models/output_builder"
require_relative "models/name_finder_validator"
require_relative "models/name_finder_worker"
require_relative "models/name_finder"
require_relative "models/today"
# Namespace module for Global Names Recognition and Discovery
module Gnrd
ENVIRONMENTS = %i[development test production].freeze
class << self
def init
dir
db_connections
end
# directory to keep temporary files
def dir
@dir ||= lambda do
temp_dir = "#{Gnrd.conf.tmp_dir}/gnrd"
FileUtils.mkdir(temp_dir) unless Dir.exist?(temp_dir)
temp_dir
end[]
end
def env
@env ||= ENV["RACK_ENV"] ? ENV["RACK_ENV"].to_sym : :development
end
def env=(env)
unless ENVIRONMENTS.include?(env)
raise TypeError.new("Wrong environment: '#{env}'")
end
@env = env
end
def conf
@conf ||= OpenStruct.new(conf_default)
end
def db_conf
ENVIRONMENTS.each_with_object({}) do |e, obj|
obj[e.to_s] = { "adapter" => "postgresql", "encoding" => "unicode",
"database" => "gnrd_#{e}", "pool" => 5,
"username" => ENV["GNRD_DB_USER"],
"password" => ENV["GNRD_DB_USER_PASSWORD"],
"host" => ENV["GNRD_DB_HOST"] }
end
end
# rubocop:disable Naming/MemoizedInstanceVariableName
def db_connections
@db ||= connect
@redis ||= redis_connect
end
# rubocop:enable Naming/MemoizedInstanceVariableName
def disconnect
ActiveRecord::Base.connection.disconnect!
rescue ActiveRecord::ConnectionNotEstablished
puts "Setting new connection"
end
private
def connect
ActiveRecord::Base.logger = Logger.new(__dir__ + "/log/postgres.log")
ActiveRecord::Base.logger.level = Logger::INFO
ActiveRecord::Base.configurations = Gnrd.conf.database
ActiveRecord::Base.establish_connection(env)
end
def redis_connect
Resque.redis = Gnrd.conf.redis_host
end
def conf_default
{
redis_host: ENV["REDIS_HOST"], tmp_dir: "/tmp",
database: db_conf, session_secret: ENV["GNRD_SALT"],
gnfinder_host: ENV["GNFINDER_HOST"],
gnfinder_port: ENV["GNFINDER_PORT"],
"disqus_shortname" => "globalnames-rd"
}
end
end
end
Gnrd.init