From acc758f9848d4b83fccabebbc14ca1a646e5e201 Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Wed, 17 Dec 2014 14:24:04 +0100 Subject: [PATCH] Show a welcome page when the application doesn't have routes --- lib/lotus/middleware.rb | 11 ++++-- lib/lotus/templates/welcome.html | 45 ++++++++++++++++++++++++ lib/lotus/welcome.rb | 14 ++++++++ test/fixtures/welcome_app/application.rb | 6 ++++ test/integration/welcome_page_test.rb | 33 +++++++++++++++++ 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 lib/lotus/templates/welcome.html create mode 100644 lib/lotus/welcome.rb create mode 100644 test/fixtures/welcome_app/application.rb create mode 100644 test/integration/welcome_page_test.rb diff --git a/lib/lotus/middleware.rb b/lib/lotus/middleware.rb index fd1bd23f8..b55529e6d 100644 --- a/lib/lotus/middleware.rb +++ b/lib/lotus/middleware.rb @@ -32,7 +32,7 @@ def initialize(configuration) def load!(application, namespace) @namespace = namespace @builder = ::Rack::Builder.new - load_default_stack + load_default_stack(application) @stack.each { |m, args, block| @builder.use load_middleware(m), *args, &block } @builder.run application.routes @@ -78,11 +78,12 @@ def load_middleware(middleware) # @api private # @since x.x.x - def load_default_stack + def load_default_stack(application) @default_stack_loaded ||= begin if @configuration.sessions.enabled? use(*@configuration.sessions.middleware) end + if @configuration.serve_assets @configuration.assets.entries.each do |path, children| use Rack::Static, urls: children, root: path @@ -90,6 +91,12 @@ def load_default_stack end use Rack::MethodOverride + + unless application.routes.defined? + require 'lotus/welcome' + use Lotus::Welcome + end + true end end diff --git a/lib/lotus/templates/welcome.html b/lib/lotus/templates/welcome.html new file mode 100644 index 000000000..a9274a0cf --- /dev/null +++ b/lib/lotus/templates/welcome.html @@ -0,0 +1,45 @@ + + + + Lotus — A complete web framework for Ruby + + + +
+

Lotus

+
+

A complete web framework for Ruby

+ +

Lotus is Open Source Software for MVC web development.
+ It's simple, fast and lightweight.

+ +
+ +
+ + + diff --git a/lib/lotus/welcome.rb b/lib/lotus/welcome.rb new file mode 100644 index 000000000..f6223fff2 --- /dev/null +++ b/lib/lotus/welcome.rb @@ -0,0 +1,14 @@ +require 'pathname' + +module Lotus + class Welcome + def initialize(app) + @root = Pathname.new(__dir__).join('templates').realpath + @body = [@root.join('welcome.html').read] + end + + def call(env) + [200, {}, @body] + end + end +end diff --git a/test/fixtures/welcome_app/application.rb b/test/fixtures/welcome_app/application.rb new file mode 100644 index 000000000..f444c4262 --- /dev/null +++ b/test/fixtures/welcome_app/application.rb @@ -0,0 +1,6 @@ +module WelcomeApp + class Application < Lotus::Application + configure do + end + end +end diff --git a/test/integration/welcome_page_test.rb b/test/integration/welcome_page_test.rb new file mode 100644 index 000000000..51c715fa3 --- /dev/null +++ b/test/integration/welcome_page_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' +require 'rack/test' +require 'fixtures/welcome_app/application' + +describe 'Welcome page' do + include Rack::Test::Methods + + before do + @current_dir = Dir.pwd + Dir.chdir FIXTURES_ROOT.join('welcome') + @app = WelcomeApp::Application.new + end + + after do + Dir.chdir @current_dir + @current_dir = nil + end + + def app + @app + end + + def response + last_response + end + + it 'shows a welcome page when no routes were set' do + get '/' + + response.status.must_equal 200 + response.body.must_include %(Lotus) + end +end