Skip to content

Commit

Permalink
Better comments, fixed some typos, and MUCH smarter template serving
Browse files Browse the repository at this point in the history
  • Loading branch information
juliocesar committed Feb 2, 2013
1 parent fadd8f6 commit 546c771
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions Rakefile
Expand Up @@ -26,7 +26,11 @@ rescue LoadError
$ gem install #{Gems.join ' '}
Or add them to your Gemfile and run `bundle install`.
Or add them to your Gemfile and run `bundle install`. Additionally, you
may want to run:
$ gem install haml coffee_script
)
exit 1
end
Expand All @@ -52,8 +56,8 @@ class HoplaLogger < Logger
end
end

# Steal everything coming out to stdout and ensure it conforms with
# our asthetic standards
# Steal everything going to stdout and ensure it conforms with
# our aesthetic standards
def $stdout.puts *args
$logger.info *args
end
Expand Down Expand Up @@ -85,35 +89,32 @@ Compiler = Sprockets::Environment.new Pathname(Root) do |env|
end
# ---

# The HTTP server
# The preview server
server = Rack::Builder.app do
FileServer = Rack::File.new Public

def index
static = File.open("#{Public}/index.html") rescue nil
template = Dir["#{Templates}/index.*"][0]
if static
[200, {'Content-Type' => 'text/html'}, static]
elsif template
# Tiny middleware for serving a compiled template if it exists.
# Requesting /foo.html looks for assets/templates/foo.*
Template = lambda { |env|
noext = env['PATH_INFO'].sub /\.\w+$/, ''
path = noext == "/" ? 'index' : noext
template = Dir["#{Templates/path}.*"][0]
if template
[200, {'Content-Type' => 'text/html'}, [Tilt.new(template).render]]
else
not_found "index.* template"
[404, {'Content-Type' => 'text/plain'}, ["Template not found: #{path}"]]
end
end

def not_found path
[404, {'Content-Type' => 'text/plain'}, ["File not found: #{path}"]]
end
}

# Serve, by priority:
# 1 - any static files that exist in the public dir
# 2 - scripts and stylesheets, compiling them before serving
# 3 - templates matching the path requested (compiled)
run lambda { |env|
response = FileServer.call env
response = Compiler.call env if response[0] == 404
if response[0] == 404
return index if env['PATH_INFO'] == '/'
not_found env['PATH_INFO']
else
response
end
response = Template.call env if response[0] == 404
response
}
end
# ---
Expand Down

0 comments on commit 546c771

Please sign in to comment.