lib/trellis/trellis.rb in trellis-0.0.5 vs lib/trellis/trellis.rb in trellis-0.0.6

- old
+ new

@@ -38,10 +38,11 @@ require 'redcloth' require 'bluecloth' require 'facets' require 'directory_watcher' require 'erubis' +require 'ostruct' module Trellis # -- Application -- # Represents a Trellis Web Application. An application can define one or more @@ -52,19 +53,25 @@ # descendant application classes get a singleton class level instances for # holding homepage, dependent pages, static resource routing paths def self.inherited(child) #:nodoc: child.class_attr_reader(:homepage) + child.class_attr_reader(:session_config) child.attr_array(:static_routes) child.meta_def(:logger) { Application.logger } + child.instance_variable_set(:@session_config, OpenStruct.new({:impl => :cookie})) super end # class method that defines the homepage or entry point of the application # the entry point is the URL pattern / where the application is mounted def self.home(sym) @homepage = sym + end + + def self.session(sym, options={}) + @session_config = OpenStruct.new({:impl => sym, :options => options}) end # define url paths for static resources def self.map_static(urls = [], root = File.expand_path("#{File.dirname($0)}/../html/")) @static_routes << {:urls => urls, :root => root} @@ -75,28 +82,38 @@ Application.logger.info "Starting Trellis Application #{self.class} on port #{port}" directory_watcher = configure_directory_watcher directory_watcher.start - Rack::Handler::Mongrel.run configured_instance, :Port => port do |server| + Rack::Handler::Mongrel.run configured, :Port => port do |server| trap(:INT) do Application.logger.info "Exiting Trellis Application #{self.class}" directory_watcher.stop server.stop end end rescue Exception => e Application.logger.warn "#{ e } (#{ e.class })!" end - def configured_instance + def configured # configure rack middleware application = Rack::ShowStatus.new(self) application = Rack::ShowExceptions.new(application) application = Rack::Reloader.new(application) application = Rack::CommonLogger.new(application, Application.logger) - application = Rack::Session::Cookie.new(application) + + # configure rack session + session_config = self.class.session_config + case session_config.impl + when :pool + application = Rack::Session::Pool.new(application, session_config.options) + when :memcached + application = Rack::Session::Memcache.new(application, session_config.options) + else + application = Rack::Session::Cookie.new(application) + end # set all static resource paths self.class.static_routes.each do |path| application = Rack::Static.new(application, path) end @@ -107,12 +124,17 @@ def find_router_for(request) match = Page.subclasses.values.find { |page| page.router && page.router.matches?(request) } match ? match.router : DefaultRouter.new(:application => self) end - # implements the rack specification + # Rack call interface. def call(env) + dup.call!(env) + end + + # implements the rack specification + def call!(env) response = Rack::Response.new request = Rack::Request.new(env) Application.logger.debug "request received with url_root of #{request.script_name}" if request.script_name @@ -652,10 +674,9 @@ def render unless @page.class.format == :eruby @parser.parse(@page.class.parsed_template.to_html) else - puts "eruby context is #{@eruby_context}" preprocessed = Erubis::PI::Eruby.new(@page.class.parsed_template.to_html, :trim => false).evaluate(@eruby_context) @parser.parse(preprocessed) end end \ No newline at end of file