lib/hanami/application.rb in hanami-2.0.0.alpha3 vs lib/hanami/application.rb in hanami-2.0.0.alpha4
- old
+ new
@@ -6,10 +6,11 @@
require "pathname"
require "rack"
require "zeitwerk"
require_relative "slice"
require_relative "application/autoloader/inflector_adapter"
+require_relative "application/router"
require_relative "application/routes"
require_relative "application/settings"
module Hanami
# Hanami application class
@@ -69,12 +70,10 @@
slices.values.each(&:init)
slices.freeze
autoloader.setup
- load_routes
-
@inited = true
self
end
def inited?
@@ -146,10 +145,12 @@
def boot(&block)
return self if booted?
init
+ load_router
+
container.finalize!(&block)
slices.values.each(&:boot)
@booted = true
@@ -166,16 +167,10 @@
def settings
@_settings ||= load_settings
end
- def routes
- @_mutex.synchronize do
- @_routes ||= load_routes
- end
- end
-
MODULE_DELIMITER = "::"
private_constant :MODULE_DELIMITER
def namespace
inflector.constantize(name.split(MODULE_DELIMITER)[0..-2].join(MODULE_DELIMITER))
@@ -214,10 +209,45 @@
return unless component_name
providers.detect { |provider| component_name.include?(provider.namespace.to_s) }
end
+ def router
+ @_mutex.synchronize do
+ @_router ||= load_router
+ end
+ end
+
+ def load_router
+ Router.new(
+ routes: routes,
+ resolver: resolver,
+ **configuration.router.options,
+ ) do
+ use Hanami.application[:rack_monitor]
+
+ Hanami.application.config.for_each_middleware do |m, *args, &block|
+ use(m, *args, &block)
+ end
+ end
+ end
+
+ def routes
+ require File.join(configuration.root, configuration.router.routes_path)
+ routes_class = autodiscover_application_constant(configuration.router.routes_class_name)
+ routes_class.routes
+ rescue LoadError
+ proc {}
+ end
+
+ def resolver
+ config.router.resolver.new(
+ slices: slices,
+ inflector: inflector
+ )
+ end
+
private
def prepare_base_load_path
base_path = File.join(root, "lib")
$LOAD_PATH.unshift base_path unless $LOAD_PATH.include?(base_path)
@@ -310,17 +340,10 @@
root: slice_path.realpath
)
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
- def load_routes
- require File.join(configuration.root, configuration.router.routes_path)
- routes_class = autodiscover_application_constant(configuration.router.routes_class_name)
- routes_class.routes
- rescue LoadError # rubocop:disable Lint/SuppressedException
- end
-
def load_settings
prepare_base_load_path
require File.join(configuration.root, configuration.settings_path)
settings_class = autodiscover_application_constant(configuration.settings_class_name)
settings_class.new(configuration.settings_store)
@@ -340,27 +363,10 @@
def initialize(application = self.class)
require_relative "application/router"
application.boot
- resolver = application.config.router.resolver.new(
- slices: application.slices,
- inflector: application.inflector
- )
-
- router = Application::Router.new(
- routes: application.routes,
- resolver: resolver,
- **application.configuration.router.options,
- ) do
- use application[:rack_monitor]
-
- application.config.for_each_middleware do |m, *args, &block|
- use(m, *args, &block)
- end
- end
-
- @app = router.to_rack_app
+ @app = application.router.to_rack_app
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
def call(env)
@app.call(env)