Permalink
Browse files

Extract out core gem server functionality into middleware

Fixes #33
  • Loading branch information...
1 parent 774f039 commit cd30dc71881870ad6d1c16411bcce685e76af9de @copiousfreetime committed Oct 15, 2013
Showing with 55 additions and 6 deletions.
  1. +1 −0 HISTORY.md
  2. +14 −0 examples/as_middleware.ru
  3. +1 −0 lib/stickler/middleware.rb
  4. +37 −0 lib/stickler/middleware/server.rb
  5. +2 −6 lib/stickler/server.rb
View
1 HISTORY.md
@@ -6,6 +6,7 @@ Version 2.4.0 - 2013-10-XX
--------------------------
* Ruby 2.0 compatibility (copiousfreetime/stickler/#28
* Implement latest-version command (copiousfreetime/stickler/#29)
+* Extract Server functionality to Middleware (copiousfreetime/stickler/#33)
* Updated dependencies to latest versions
Version 2.3.0 - 2013-03-09
View
14 examples/as_middleware.ru
@@ -0,0 +1,14 @@
+#-----------------------------------------------------------------------
+#-*- vim: set ft=ruby: -*-
+#
+# Example rackup file for using stickler as middleware in a larger application
+#-----------------------------------------------------------------------
+$:.unshift File.expand_path( File.join( File.dirname(__FILE__), "..", "lib" ) )
+
+require 'stickler'
+
+root = File.expand_path( File.join( File.dirname( __FILE__ ), *%w[ .. test data ]))
+
+puts root
+use ::Stickler::Middleware::Server, :stickler_root => root
+run ::Sinatra::Base
View
1 lib/stickler/middleware.rb
@@ -9,3 +9,4 @@ module Middleware
require 'stickler/middleware/local'
require 'stickler/middleware/mirror'
require 'stickler/middleware/not_found'
+require 'stickler/middleware/server'
View
37 lib/stickler/middleware/server.rb
@@ -0,0 +1,37 @@
+require 'pathname'
+module Stickler::Middleware
+ # Server is the entire stickler stack as a single piece of middleware that
+ # may be used by other libraries that would like to include Stickler in their
+ # application.
+ class Server
+ attr_reader :stickler_root
+
+ def initialize( app, opts = {} )
+ @app = app
+ @stickler_root = Pathname.new( opts.fetch( :stickler_root ) ).realpath
+ @run = server_app
+ validate
+ end
+
+ def call( env )
+ @run.call( env )
+ end
+
+ def server_app
+ root = self.stickler_root
+ Rack::Builder.app( @app )do
+ use Stickler::Middleware::Compression
+ use Stickler::Middleware::Gemcutter, :serve_indexes => false, :repo_root => root.join( "gemcutter" )
+ use Stickler::Middleware::Mirror, :serve_indexes => false, :repo_root => root.join( "mirror" )
+ use Stickler::Middleware::Index, :serve_indexes => true
+ end
+ end
+
+ private
+
+ def validate
+ raise ::Stickler::Error, "Stickler root directory '#{stickler_root}' must already exist" unless stickler_root.directory?
+ raise ::Stickler::Error, "Stickler root directory '#{stickler_root}' must be writable" unless stickler_root.writable?
+ end
+ end
+end
View
8 lib/stickler/server.rb
@@ -12,14 +12,10 @@ def initialize( stickler_root )
def app
root = self.stickler_root
- Rack::Builder.app do
+ Rack::Builder.app( Sinatra::Base.new ) do
use Rack::CommonLogger
- use Stickler::Middleware::Compression
- use Stickler::Middleware::Gemcutter, :serve_indexes => false, :repo_root => File.join( root, "gemcutter" )
- use Stickler::Middleware::Mirror, :serve_indexes => false, :repo_root => File.join( root, "mirror" )
- use Stickler::Middleware::Index, :serve_indexes => true
+ use Stickler::Middleware::Server, :stickler_root => root
use Stickler::Middleware::NotFound
- run Sinatra::Base
end
end
end

0 comments on commit cd30dc7

Please sign in to comment.