Module: StraightServer::Initializer
- Included in:
- Server
- Defined in:
- lib/straight-server/initializer.rb
Defined Under Namespace
Modules: ConfigDir
Constant Summary collapse
- GEM_ROOT =
File.('../..', File.dirname(__FILE__))
- MIGRATIONS_ROOT =
GEM_ROOT + '/db/migrations/'
Instance Method Summary collapse
- #add_route(path, &block) ⇒ Object
- #connect_to_db ⇒ Object
- #create_config_files ⇒ Object
- #create_logger ⇒ Object
- #initialize_routes ⇒ Object
-
#load_addons ⇒ Object
Loads addon modules into StraightServer::Server.
- #migrations_pending? ⇒ Boolean
- #prepare ⇒ Object
- #read_config_file ⇒ Object
-
#resume_tracking_active_orders! ⇒ Object
Finds orders that have statuses < 2 and starts querying the blockchain for them (unless they are also expired).
- #run_migrations ⇒ Object
-
#setup_redis_connection ⇒ Object
Loads redis gem and sets up key prefixes for order counters for the current straight environment.
Instance Method Details
#add_route(path, &block) ⇒ Object
48 49 50 |
# File 'lib/straight-server/initializer.rb', line 48 def add_route(path, &block) @routes[path] = block end |
#connect_to_db ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/straight-server/initializer.rb', line 87 def connect_to_db # symbolize keys for convenience db_config = StraightServer::Config.db.keys_to_sym db_name = if db_config[:adapter] == 'sqlite' ConfigDir.path + "/" + db_config[:name] else db_config[:name] end StraightServer.db_connection = Sequel.connect( "#{db_config[:adapter]}://" + "#{db_config[:user]}#{(":" if db_config[:user])}" + "#{db_config[:password]}#{("@" if db_config[:user] || db_config[:password])}" + "#{db_config[:host]}#{(":" if db_config[:port])}" + "#{db_config[:port]}#{("/" if db_config[:host] || db_config[:port])}" + "#{db_name}" ) end |
#create_config_files ⇒ Object
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 |
# File 'lib/straight-server/initializer.rb', line 52 def create_config_files FileUtils.mkdir_p(ConfigDir.path) unless File.exist?(ConfigDir.path) unless File.exist?(ConfigDir.path + '/addons.yml') puts "\e[1;33mNOTICE!\e[0m \e[33mNo file #{ConfigDir.path}/addons.yml was found. Created an empty sample for you.\e[0m" puts "No need to restart until you actually list your addons there. Now will continue loading StraightServer." FileUtils.cp(GEM_ROOT + '/templates/addons.yml', ConfigDir.path) end unless File.exist?(ConfigDir.path + '/server_secret') puts "\e[1;33mNOTICE!\e[0m \e[33mNo file #{ConfigDir.path}/server_secret was found. Created one for you.\e[0m" puts "No need to restart so far. Now will continue loading StraightServer." File.open(ConfigDir.path + '/server_secret', "w") do |f| f.puts String.random(16) end end unless File.exist?(ConfigDir.path + '/config.yml') puts "\e[1;33mWARNING!\e[0m \e[33mNo file #{ConfigDir.path}/config.yml was found. Created a sample one for you.\e[0m" puts "You should edit it and try starting the server again.\n" FileUtils.cp(GEM_ROOT + '/templates/config.yml', ConfigDir.path) puts "Shutting down now.\n\n" exit end end |
#create_logger ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/straight-server/initializer.rb', line 118 def create_logger return unless Config.logmaster require_relative 'logger' StraightServer.logger = StraightServer::Logger.new( log_level: ::Logger.const_get(Config.logmaster['log_level'].upcase), file: ConfigDir.path + '/' + Config.logmaster['file'], raise_exception: Config.logmaster['raise_exception'], name: Config.logmaster['name'], email_config: Config.logmaster['email_config'] ) end |
#initialize_routes ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/straight-server/initializer.rb', line 130 def initialize_routes @routes = {} add_route %r{\A/gateways/.+?/orders(/.+)?\Z} do |env| controller = OrdersController.new(env) controller.response end add_route %r{\A/gateways/.+?/last_keychain_id\Z} do |env| controller = OrdersController.new(env) controller.response end end |
#load_addons ⇒ Object
Loads addon modules into StraightServer::Server. To be useful, an addon most probably has to implement self.extended(server) callback. That way, it can access the server object and, for example, add routes with StraightServer::Server#add_route.
Addon modules can be both rubygems or files under ~/.straight/addons/. If ~/.straight/addons.yml contains a ‘path’ key for a particular addon, then it means the addon is placed under the ~/.straight/addons/. If not, it is assumed it is already in the LOAD_PATH somehow, with rubygems for example.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/straight-server/initializer.rb', line 151 def load_addons # load ~/.straight/addons.yml addons = YAML.load_file(ConfigDir.path + '/addons.yml') addons.each do |name, addon| StraightServer.logger.info "Loading #{name} addon" if addon['path'] # First, check the ~/.straight/addons dir require ConfigDir.path + '/' + addon['path'] else # then assume it's already loaded using rubygems require name end # extending the current server object with the addon extend Kernel.const_get("StraightServer::Addon::#{addon['module']}") end if addons end |
#migrations_pending? ⇒ Boolean
114 115 116 |
# File 'lib/straight-server/initializer.rb', line 114 def migrations_pending? !Sequel::Migrator.is_current?(StraightServer.db_connection, MIGRATIONS_ROOT) end |
#prepare ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/straight-server/initializer.rb', line 37 def prepare ConfigDir.set! create_config_files read_config_file create_logger connect_to_db run_migrations if migrations_pending? setup_redis_connection initialize_routes end |
#read_config_file ⇒ Object
80 81 82 83 84 85 |
# File 'lib/straight-server/initializer.rb', line 80 def read_config_file YAML.load_file(ConfigDir.path + '/config.yml').each do |k,v| StraightServer::Config.send(k + '=', v) end StraightServer::Config.server_secret = File.read(ConfigDir.path + '/server_secret').chomp end |
#resume_tracking_active_orders! ⇒ Object
Finds orders that have statuses < 2 and starts querying the blockchain for them (unless they are also expired). This is for cases when the server was shut down, but some orders statuses are not resolved.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/straight-server/initializer.rb', line 169 def resume_tracking_active_orders! StraightServer::Order.where('status < 2').each do |order| # Order is expired, but status is < 2! Suspcicious, probably # an unclean shutdown of the server. Let's check and update the status manually once. if order.time_left_before_expiration < 1 StraightServer.logger.info "Order #{order.id} seems to be expired, but status remains #{order.status}. Will check for status update manually." order.gateway.test_mode = true if order.test_mode order.status(reload: true) # if we still see no transactions to that address, # consider the order truly expired and update the status accordingly order.status = StraightServer::Order::STATUSES[:expired] if order.status < 2 order.save StraightServer.logger.info "Order #{order.id} status updated, new status is #{order.status}" # Order is NOT expired and status is < 2. Let's keep tracking it. else StraightServer.logger.info "Resuming tracking of order #{order.id}, current status is #{order.status}, time before expiration: #{order.time_left_before_expiration} seconds." StraightServer::Thread.new do order.start_periodic_status_check end end end end |
#run_migrations ⇒ Object
108 109 110 111 112 |
# File 'lib/straight-server/initializer.rb', line 108 def run_migrations print "\nPending migrations for the database detected. Migrating..." Sequel::Migrator.run(StraightServer.db_connection, MIGRATIONS_ROOT) print "done\n\n" end |
#setup_redis_connection ⇒ Object
Loads redis gem and sets up key prefixes for order counters for the current straight environment.
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/straight-server/initializer.rb', line 197 def setup_redis_connection raise "Redis not configured" unless Config.redis Config.redis = Config.redis.keys_to_sym Config.redis[:prefix] ||= "StraightServer:#{Config.environment}" StraightServer.redis_connection = Redis.new( host: Config.redis[:host], port: Config.redis[:port], db: Config.redis[:db], password: Config.redis[:password] ) end |