Class: StraightServer::Server
- Inherits:
-
Goliath::API
- Object
- Goliath::API
- StraightServer::Server
- Includes:
- Initializer
- Defined in:
- lib/straight-server/server.rb
Constant Summary
Constants included from Initializer
Initializer::GEM_ROOT, Initializer::MIGRATIONS_ROOT
Instance Method Summary collapse
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #options_parser(opts, options) ⇒ Object
-
#process_request(env) ⇒ Object
This is a separate method now because of the need to rescue Sequel::DatabaseDisconnectError As soon as we figure out where should #connect_to_db be placed so that it is executed AFTER the process is daemonized, I’ll refactor the code.
- #response(env) ⇒ Object
Methods included from Initializer
#add_route, #connect_to_db, #create_config_files, #create_logger, #initialize_routes, #load_addons, #migrations_pending?, #prepare, #read_config_file, #resume_tracking_active_orders!, #run_migrations, #setup_redis_connection
Constructor Details
#initialize ⇒ Server
8 9 10 11 12 13 14 15 16 |
# File 'lib/straight-server/server.rb', line 8 def initialize super prepare StraightServer.logger.info "starting Straight Server v #{StraightServer::VERSION}" require_relative 'order' require_relative 'gateway' load_addons resume_tracking_active_orders! end |
Instance Method Details
#options_parser(opts, options) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/straight-server/server.rb', line 18 def (opts, ) # Even though we define that option here, it is purely for the purposes of compliance with # Goliath server. If don't do that, there will be an exception saying "unrecognized argument". # In reality, we make use of --config-dir value in the in StraightServer::Initializer and stored # it in StraightServer::Initializer.config_dir property. opts.on('-c', '--config-dir STRING', "Directory where config files and addons are placed") do |val| [:config_dir] = File.(val || ENV['HOME'] + '/.straight' ) end end |
#process_request(env) ⇒ Object
This is a separate method now because of the need to rescue Sequel::DatabaseDisconnectError As soon as we figure out where should #connect_to_db be placed so that it is executed AFTER the process is daemonized, I’ll refactor the code.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/straight-server/server.rb', line 59 def process_request(env) # This is a client implementation example, an html page + a dart script # supposed to only be loaded in development. if Goliath.env == :development if env['REQUEST_PATH'] == '/' return [200, {}, IO.read(Initializer::GEM_ROOT + '/examples/client/client.html')] elsif Goliath.env == :development && env['REQUEST_PATH'] == '/client.js' return [200, {}, IO.read(Initializer::GEM_ROOT + '/examples/client/client.js')] end end @routes.each do |path, action| # path is a regexp return action.call(env) if env['REQUEST_PATH'] =~ path end # no block was called, means no route matched. Let's render 404 return [404, {}, "#{env['REQUEST_METHOD']} #{env['REQUEST_PATH']} Not found"] end |
#response(env) ⇒ Object
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 |
# File 'lib/straight-server/server.rb', line 28 def response(env) # POST /gateways/1/orders - create order # GET /gateways/1/orders/1 - see order info # /gateways/1/orders/1/websocket - subscribe to order status changes via a websocket # This will be more complicated in the future. For now it # just checks that the path starts with /gateways/:id/orders StraightServer.logger.watch_exceptions do # If the process is daemonized, we get Sequel::DatabaseDisconnectError with Postgres. # The explanation is here: https://github.com/thuehlinger/daemons/issues/31 # Until I figure out where to call connect_to_db so that it connects to the DB # AFTER the process is daemonized, this shall remain as it is now. begin return process_request(env) rescue Sequel::DatabaseDisconnectError connect_to_db return process_request(env) end end # Assume things went wrong, if they didn't go right [500, {}, "#{env['REQUEST_METHOD']} #{env['REQUEST_PATH']} Server Error"] end |