Skip to content

Commit

Permalink
Show a welcome page when the application doesn't have routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Dec 17, 2014
1 parent 8bb5fab commit acc758f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/lotus/middleware.rb
Expand Up @@ -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

Expand Down Expand Up @@ -78,18 +78,25 @@ 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
end
end

use Rack::MethodOverride

unless application.routes.defined?
require 'lotus/welcome'
use Lotus::Welcome
end

true
end
end
Expand Down
45 changes: 45 additions & 0 deletions lib/lotus/templates/welcome.html
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<title>Lotus &mdash; A complete web framework for Ruby</title>
<style>
* { margin: 0; padding: 0; }
body { font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; color: #333; background-color: #ecf0f1; font-size: 16px; line-height: 24px; }
h1, h2, h3 { margin: -20px; line-height: 100px; text-align: center; }
h1 { font-size: 60px; color: #551e54; }
h2 { font-size: 30px; }
h3 { font-size: 20px; line-height: 22px; color: #666; }
hr { margin: 2em; color: #ecf0f1; }
ul { text-align: center; }
li { display: inline; margin: 3em; line-height: 24px; list-style-type: none; vertical-align: text-top;}
a { color: #333; padding: 1px; }
#container { margin:0 auto; padding: 24px; width:700px; background-color:#fff; border: 1px solid #999; border-width: 0 1px 1px 1px; }
#content { margin: 30px 10px 0 10px; }
#footer { margin:0 auto 0; padding: 12px; width:700px; line-height: 24px; font-size: 12px; }
.bold { font-weight: bold; }
</style>
</head>
<body>
<div id="container">
<h1>Lotus</h1>
<hr />
<h2>A complete web framework for Ruby</h2>

<h3>Lotus is Open Source Software for MVC web development.<br />
It's simple, fast and lightweight.</h3>

<hr style="margin-top: 4em;"/>
<div id="content">
<ul>
<li><a href="http://lotusrb.org">Website</a></li>
<li><a href="http://www.rubydoc.info/gems/lotusrb">API docs</a></li>
<li><a href="https://github.com/lotus/lotus">Source code</a></li>
<li><a href="https://gitter.im/lotus/chat">Chat</a></li>
</ul>
</div>
</div>
<div id="footer">
&copy; 2014 Luca Guidi &mdash; Lotus is released under the MIT License.
</div>
</body>
</html>
14 changes: 14 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions test/fixtures/welcome_app/application.rb
@@ -0,0 +1,6 @@
module WelcomeApp
class Application < Lotus::Application
configure do
end
end
end
33 changes: 33 additions & 0 deletions 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

0 comments on commit acc758f

Please sign in to comment.