Skip to content

Commit 423e8ec

Browse files
committed
Static Server based on Sinatra
Using Sinatra provides better handling of mime types and supports 304's/etc. Also updated the Gemfile to exclude most gem outside of developemnt (default).
1 parent 23f05c1 commit 423e8ec

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

Gemfile

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
source "http://rubygems.org"
22

3-
gem 'rake'
4-
gem 'rack'
5-
gem 'jekyll'
6-
gem 'rdiscount'
7-
gem 'pygments.rb'
8-
gem 'RedCloth'
9-
gem 'haml', '>= 3.1'
10-
gem 'compass', '>= 0.11'
11-
gem 'rubypants'
12-
gem 'rb-fsevent'
13-
gem 'stringex'
3+
group :development do
4+
gem 'rake'
5+
gem 'rack'
6+
gem 'jekyll'
7+
gem 'rdiscount'
8+
gem 'pygments.rb'
9+
gem 'RedCloth'
10+
gem 'haml', '>= 3.1'
11+
gem 'compass', '>= 0.11'
12+
gem 'rubypants'
13+
gem 'rb-fsevent'
14+
gem 'stringex'
15+
end
16+
17+
gem 'sinatra', '1.2.6'

Gemfile.lock

+5
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ GEM
4040
blankslate (>= 2.1.2.3)
4141
ffi (~> 1.0.7)
4242
sass (3.1.5)
43+
sinatra (1.2.6)
44+
rack (~> 1.1)
45+
tilt (>= 1.2.2, < 2.0)
4346
stringex (1.3.0)
4447
syntax (1.0.0)
48+
tilt (1.3.2)
4549

4650
PLATFORMS
4751
ruby
@@ -57,4 +61,5 @@ DEPENDENCIES
5761
rb-fsevent
5862
rdiscount
5963
rubypants
64+
sinatra (= 1.2.6)
6065
stringex

config.ru

+15-25
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
1-
require 'rubygems'
21
require 'bundler/setup'
3-
require 'rack'
2+
require 'sinatra/base'
43

54
# The project root directory
65
$root = ::File.dirname(__FILE__)
76

8-
# Common Rack Middleware
9-
use Rack::ShowStatus # Nice looking 404s and other messages
10-
use Rack::ShowExceptions # Nice looking errors
7+
class SinatraStaticServer < Sinatra::Base
118

12-
#
13-
# From Rack::DirectoryIndex:
14-
# https://github.com/craigmarksmith/rack-directory-index/
15-
#
16-
module Rack
17-
class DirectoryIndex
18-
def initialize(app)
19-
@app = app
20-
end
21-
def call(env)
22-
index_path = ::File.join($root, 'public', Rack::Request.new(env).path.split('/'), 'index.html')
23-
if ::File.exists?(index_path)
24-
return [200, {"Content-Type" => "text/html"}, [::File.read(index_path)]]
25-
else
26-
@app.call(env)
27-
end
28-
end
9+
get(/.+/) do
10+
send_sinatra_file(request.path) {404}
2911
end
30-
end
3112

32-
use Rack::DirectoryIndex
13+
not_found do
14+
send_sinatra_file('404.html') {"Sorry, I cannot find #{request.path}"}
15+
end
3316

34-
run Rack::Directory.new($root + '/public')
17+
def send_sinatra_file(path, &missing_file_block)
18+
file_path = File.join(File.dirname(__FILE__), 'public', path)
19+
file_path = File.join(file_path, 'index.html') unless file_path =~ /\.[a-z]+$/i
20+
File.exist?(file_path) ? send_file(file_path) : missing_file_block.call
21+
end
22+
23+
end
3524

25+
run SinatraStaticServer

0 commit comments

Comments
 (0)