lib/piedesaint.rb in piedesaint-0.0.2 vs lib/piedesaint.rb in piedesaint-0.1.0

- old
+ new

@@ -4,30 +4,48 @@ require 'puma/events' #require 'puma/minissl' #For Puma 2.0.1 require 'openssl' require 'rack/ssl-enforcer' +require 'rack/cache' + require 'rubygems/package' require "stringio" +require 'time' module Piedesaint module Rack class DirectoryCompress < ::Rack::Directory def list_directory - return [200, {}, ::Piedesaint.tar(@path)] + tarball = ::Piedesaint.tar(@path) + etag = Digest::MD5.base64digest tarball.string + headers = {"Cache-Control" => "public", "ETag" => etag } + + return [304, headers, []] if etag == @env['HTTP_IF_NONE_MATCH'] + return [200, headers, tarball] end end - class DirectoriesCompress - def initialize(roots, app=nil) + class FileEtag < ::Rack::File + def serving(env) + etag = Digest::MD5.file(@path).base64digest + @headers['Cache-Control'] = "public" + @headers['ETag'] = etag + super + end + end + + class DirectoriesTraversal + def initialize(options, app=nil) @apps = [] - roots.each do |root| + @options = options.dup + @options[:folders].each do |root| root = File.expand_path root puts "Service root: #{root}" - @apps << Rack::DirectoryCompress.new(root, app) + @apps << Rack::DirectoryCompress.new(root, app ? app : Rack::FileEtag.new(root) ) end end def call(env) response = nil @@ -59,13 +77,17 @@ use ::Rack::SslEnforcer, http_port: options[:http_port], https_port: options[:https_port] use ::Rack::Deflater use ::Rack::Auth::Basic, "Icecreamland" do |username, password| ( options[:username] == username ) && ( options[:password] == password ) end - + use ::Rack::Deflater + use ::Rack::Cache, verbose: true, + metastore: 'file:/tmp/rack/meta', + entitystore: 'file:/tmp/rack/body', + default_ttl: options[:freshness] map "/" do - run Rack::DirectoriesCompress.new(options[:folders]) + run Rack::DirectoriesTraversal.new(options) end end end def self.execute ( options = {} ) @@ -88,11 +110,11 @@ puma.add_tcp_listener options[:host], options[:http_port] puma.add_ssl_listener options[:host], options[:https_port], ctx puma.min_threads = 1 - puma.max_threads = 1 + puma.max_threads = 10 begin Signal.trap "SIGUSR2" do @restart = true puma.begin_restart @@ -114,33 +136,25 @@ puma.run.join rescue Interrupt graceful_stop puma end - if @restart - p "* Restarting..." - @status.stop true if @status - restart! + if @restart + p "* Restarting..." + @status.stop true if @status + restart! end end def self.graceful_stop(puma) p " - Gracefully stopping, waiting for requests to finish" @status.stop(true) if @status puma.stop(true) - delete_pidfile p " - Goodbye!" end - def self.delete_pidfile - #˘if path = @options[:pidfile] - # File.unlink path - #end - end - - def self.tar(path) tar = StringIO.new Gem::Package::TarWriter.new(tar) do |tarwriter| Dir[File.join(path, "**/{*,.*}")].each do |file| mode = File.stat(file).mode @@ -158,6 +172,6 @@ end tar.rewind tar end -end \ No newline at end of file +end