Module: Redwing::Server
- Defined in:
- lib/redwing/server.rb
Class Method Summary collapse
-
.start(host:, port:) ⇒ Object
rubocop:disable Metrics/MethodLength.
Class Method Details
.start(host:, port:) ⇒ Object
rubocop:disable Metrics/MethodLength
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 |
# File 'lib/redwing/server.rb', line 11 def self.start(host:, port:) Redwing.load_controllers not_found = [404, {'content-type' => 'application/json'}, ['{"error":"Not Found"}']] static = Rack::Static.new( ->(_env) { not_found }, urls: [''], root: Redwing.config.public_root, cascade: true ) app = proc do |env| request = Rack::Request.new(env) match = Redwing.routes.match(request.request_method, request.path_info) response = if match dispatcher = Redwing::Dispatcher.new body = dispatcher.call(match[:route], request, match[:params]) case body when Hash then [200, {'content-type' => 'application/json'}, [body.to_json]] when String then [200, {'content-type' => 'text/html; charset=utf-8'}, [body]] else raise Redwing::Error::InvalidResponse, "Route handler must return a Hash or String, got #{body.class}" end elsif %w[GET HEAD].include?(request.request_method) static.call(env) else not_found end Redwing.config.logger.info("#{request.request_method} #{request.path_info} => #{response[0]}") response end handler = Rackup::Handler.get('puma') handler.run(app, Host: host, Port: port) end |