Browse code

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).

Scott Watermasysk authored on 22/08/2011 at 01:06:21
Showing 3 changed files
... ...
@@ -1,13 +1,17 @@
1 1
 source "http://rubygems.org"
2 2
 
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'
14 3
\ No newline at end of file
4
+group :development do 
5
+  gem 'rake'
6
+  gem 'rack'
7
+  gem 'jekyll'
8
+  gem 'rdiscount'
9
+  gem 'pygments.rb'
10
+  gem 'RedCloth'
11
+  gem 'haml', '>= 3.1'
12
+  gem 'compass', '>= 0.11'
13
+  gem 'rubypants'
14
+  gem 'rb-fsevent'
15
+  gem 'stringex'
16
+end
17
+
18
+gem 'sinatra', '1.2.6'
15 19
\ No newline at end of file
... ...
@@ -40,8 +40,12 @@ GEM
40 40
       blankslate (>= 2.1.2.3)
41 41
       ffi (~> 1.0.7)
42 42
     sass (3.1.5)
43
+    sinatra (1.2.6)
44
+      rack (~> 1.1)
45
+      tilt (>= 1.2.2, < 2.0)
43 46
     stringex (1.3.0)
44 47
     syntax (1.0.0)
48
+    tilt (1.3.2)
45 49
 
46 50
 PLATFORMS
47 51
   ruby
... ...
@@ -57,4 +61,5 @@ DEPENDENCIES
57 57
   rb-fsevent
58 58
   rdiscount
59 59
   rubypants
60
+  sinatra (= 1.2.6)
60 61
   stringex
... ...
@@ -1,35 +1,25 @@
1
-require 'rubygems'
2 1
 require 'bundler/setup'
3
-require 'rack'
2
+require 'sinatra/base'
4 3
 
5 4
 # The project root directory
6 5
 $root = ::File.dirname(__FILE__)
7 6
 
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  
11 8
 
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}
29 11
   end
30
-end
31 12
 
32
-use Rack::DirectoryIndex
13
+  not_found do
14
+    send_sinatra_file('404.html') {"Sorry, I cannot find #{request.path}"}
15
+  end
33 16
 
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
35 24
 
25
+run SinatraStaticServer
36 26
\ No newline at end of file