Class: Modern::App

Inherits:
Object
  • Object
show all
Includes:
ErrorHandling, RequestHandling
Defined in:
lib/modern/app.rb,
lib/modern/app/router.rb,
lib/modern/app/trie_router.rb,
lib/modern/app/error_handling.rb,
lib/modern/app/request_handling.rb,
lib/modern/app/request_handling/input_handling.rb,
lib/modern/app/request_handling/output_handling.rb,
lib/modern/app/request_handling/request_container.rb

Overview

‘App` is the core of Modern. Some Rack application frameworks have you inherit from them to generate your application; however, that makes it pretty difficult to control immutability of the underlying routes. Since we have a need to generate an OpenAPI specification off of our routes and our behaviors, this is not an acceptable trade-off. As such, Modern expects to be passed a Description::Descriptor, which specifies a set of Description::Routes. The app then dispatches requests based on these routes.

Defined Under Namespace

Modules: ErrorHandling, RequestHandling Classes: Router, TrieRouter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(descriptor, configuration = Modern::Configuration.new, services = Services.new) ⇒ App

Returns a new instance of App.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/modern/app.rb', line 42

def initialize(descriptor, configuration = Modern::Configuration.new, services = Services.new)
  @descriptor = IceNine.deep_freeze(DeepDup.deep_dup(descriptor))
  @configuration = IceNine.deep_freeze(DeepDup.deep_dup(configuration))
  @services = services

  # TODO: figure out a good componentized naming scheme for Modern's own logs
  #       so as to clearly differentiate them from user logs.
  @logger = @services.base_logger

  @router = Modern::App::TrieRouter.new(
    routes: Modern::DocGenerator::OpenAPI3.new.decorate_with_openapi_routes(@configuration, @descriptor)
  )
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



39
40
41
# File 'lib/modern/app.rb', line 39

def logger
  @logger
end

#servicesObject (readonly)

Returns the value of attribute services.



40
41
42
# File 'lib/modern/app.rb', line 40

def services
  @services
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/modern/app.rb', line 56

def call(env)
  request = Modern::Request.new(env, logger)
  response = Modern::Response.new(request)
  response.headers["X-Request-Id"] = request.request_id

  route = @router.resolve(request.request_method, request.path_info)

  begin
    raise Modern::Errors::NotFoundError if route.nil?

    process_request(request, response, route)
    response.finish
  rescue Modern::Redirect => redirect
    response.redirect(redirect.redirect_to, redirect.status)
  rescue Modern::Errors::WebError => err
    catch_web_error(response, err)
  rescue StandardError => err
    catch_unhandled_error(response, err)
  ensure
    response.finish
    request.cleanup
  end

  response.to_a
end