Permalink
Browse files

Merge branch '0.8-dev'

  • Loading branch information...
2 parents a94d522 + e48d5d3 commit 3d0c1395bd52d49e82c4c0c46b826bd93610f474 @bryanp bryanp committed Aug 25, 2012
@@ -1,7 +1,8 @@
module Pakyow
class Application
class << self
- attr_accessor :routes_proc, :handlers_proc, :middleware_proc, :configurations
+ attr_accessor :core_proc, :middleware_proc, :configurations
+
# Sets the path to the application file so it can be reloaded later.
#
@@ -70,32 +71,22 @@ def configure(environment, &block)
self.configurations[environment] = block
end
- # Creates routes. Example:
- # routes { get '/' { # do something } }
+ # The block that stores routes, handlers, and hooks.
#
- def routes(&block)
- self.routes_proc = block
+ def core(&block)
+ self.core_proc = block
end
- # Creates handlers for later execution.
- # The handler can be created one of two ways:
- #
- # Define a controller/action handler with an associate response status:
- # handler(name, 404, :ApplicationController, :handle_404)
- #
- # Specify a block as a handler:
- # handler(name, 404) { # handle error }
+ # The block that stores presenter related things.
#
- # If a controller calls #invoke_handler!(name) then the
- # handler defined for that code will be invoked.
- #
- def handlers(&block)
- self.handlers_proc = block
+ def presenter(&block)
+ Configuration::Base.app.presenter.proc = block
end
def middleware(&block)
self.middleware_proc = block
end
+
protected
@@ -107,9 +98,11 @@ def prepare(args)
self.builder.use(Rack::MethodOverride)
self.builder.instance_eval(&self.middleware_proc) if self.middleware_proc
- self.builder.use(Pakyow::Static) if Configuration::Base.app.static
- self.builder.use(Pakyow::Logger) if Configuration::Base.app.log
- self.builder.use(Pakyow::Reloader) if Configuration::Base.app.auto_reload
+ self.builder.use(Pakyow::Middleware::NotFound)
+ self.builder.use(Pakyow::Middleware::Static) if Configuration::Base.app.static
+ self.builder.use(Pakyow::Middleware::Presenter) if Configuration::Base.app.presenter
+ self.builder.use(Pakyow::Middleware::Logger) if Configuration::Base.app.log
+ self.builder.use(Pakyow::Middleware::Reloader) if Configuration::Base.app.auto_reload
@prepared = true
@@ -128,9 +121,12 @@ def load_config(args)
end
def detect_handler
- ['thin', 'mongrel', 'webrick'].each do |server|
+ handlers = ['thin', 'mongrel', 'webrick']
+ handlers.unshift(Configuration::Base.server.handler) if Configuration::Base.server.handler
+
+ handlers.each do |handler|
begin
- return Rack::Handler.get(server)
+ return Rack::Handler.get(handler)
rescue LoadError
rescue NameError
end
@@ -151,7 +147,7 @@ def stop(server)
include Helpers
- attr_accessor :request, :response, :presenter, :route_store, :restful_routes, :handler_store
+ attr_accessor :request, :response, :presenter, :route_store, :restful_routes, :handler_store, :parser_store
def initialize
Pakyow.app = self
@@ -214,35 +210,8 @@ def call(env)
has_route = false
catch(:halt) {
route_block = prepare_route_block(self.request.path, self.request.method)
- has_route = true if route_block
-
- if self.presenter
- self.presenter.prepare_for_request(self.request)
- end
-
- has_route = trampoline(route_block)
-
- if self.presenter
- self.response.body = [self.presenter.content]
- end
-
- # 404 if no route matched and no views were found
- if !has_route && (!self.presenter || !self.presenter.presented?)
- Log.enter "[404] Not Found"
- handler404 = @handler_store[@handler_code_to_name[404]] if @handler_code_to_name[404]
- if handler404
- catch(:halt) {
- if self.presenter
- self.presenter.prepare_for_request(self.request)
- end
- trampoline(handler404)
- if self.presenter then
- self.response.body = [self.presenter.content]
- end
- }
- end
- self.response.status = 404
- end
+ @has_route = true if route_block
+ @has_route = trampoline(route_block)
} #end :halt catch block
# This needs to be in the 'return' position (last statement)
@@ -410,14 +379,36 @@ def handler(name, *args, &block)
@handler_code_to_name[code] = name
end
end
+
+ def parser(extensions, &block)
+ extensions = [extensions] unless extensions.is_a? Array
+
+ extensions.each do |e|
+ @parser_store[e] = block
+ end
+ end
+
+ def session
+ self.request.env['rack.session'] || {}
+ end
- #TODO: don't like this...
+ # This is NOT a useless method, it's a part of the external api
def reload
load_app
end
-
- def session
- self.request.env['rack.session'] || {}
+
+ def routed?
+ @has_route
+ end
+
+ def handle_404
+ handler404 = @handler_store[@handler_code_to_name[404]] if @handler_code_to_name[404]
+ if handler404
+ catch(:halt) {
+ trampoline(handler404)
+ }
+ end
+ self.response.status = 404
end
protected
@@ -575,25 +566,22 @@ def load_app(reload_app = true)
@loader = Loader.new unless @loader
@loader.load!(Configuration::Base.app.src_dir)
- load_handlers
- load_routes
+ self.load_core
- # Reload views
- if self.presenter
- self.presenter.load
- end
+ # Reload presenter
+ self.presenter.load if self.presenter
end
- def load_handlers
+ # Evaluates core_proc
+ #
+ def load_core
@handler_store = {}
- self.instance_eval(&self.class.handlers_proc) if self.class.handlers_proc
- end
-
- def load_routes
@route_store = RouteStore.new
- self.instance_eval(&self.class.routes_proc) if self.class.routes_proc
+
+ self.instance_eval(&self.class.core_proc) if self.class.core_proc
end
+
# Send the response and cleanup.
#
def finish!
@@ -5,12 +5,16 @@
require 'core/loader'
require 'core/application'
require 'core/route_store'
-require 'core/logger'
-require 'core/static'
-require 'core/reloader'
require 'core/cache'
require 'core/presenter_base'
+# middlewares
+require 'core/middleware/logger'
+require 'core/middleware/static'
+require 'core/middleware/reloader'
+require 'core/middleware/presenter'
+require 'core/middleware/not_found'
+
# utils
require 'utils/string'
require 'utils/hash'
@@ -2,7 +2,7 @@ module Pakyow
module Configuration
class Server
class << self
- attr_accessor :port, :host
+ attr_accessor :port, :host, :handler
# On what port does the application run?
def port
@@ -13,6 +13,11 @@ def port
def host
@host || '0.0.0.0'
end
+
+ # If set, adds a handler to try (e.g. puma)
+ def handler
+ @handler || nil
+ end
end
end
end
@@ -1,5 +1,4 @@
module Pakyow
-
# Handles the loading and reloading of a Pakyow application. If in development
# mode, files are automatically reloaded if modified.
class Loader
@@ -1,33 +0,0 @@
-module Pakyow
- class Logger
- def initialize(app)
- @app = app
- end
-
- def call(env)
- began_at = Time.now
-
- Log.enter "Processing #{env['PATH_INFO']} (#{env['REMOTE_ADDR']} at #{began_at}) [#{env['REQUEST_METHOD']}]"
-
- result = nil
-
- if error = catch(:error) {
- result = @app.call(env)
- nil
- }
- Log.enter "[500] #{error}\n"
- Log.enter error.backtrace.join("\n") + "\n\n"
-
- result = Pakyow.app.response.finish
- end
-
- ended_at = Time.now.to_f
- difference = ((ended_at - began_at.to_f) * 1000).to_f
-
- Log.enter "Completed in #{difference}ms | #{Pakyow.app.response.status} | [#{Pakyow.app.request.url}]"
- Log.enter
-
- result
- end
- end
-end
@@ -0,0 +1,35 @@
+module Pakyow
+ module Middleware
+ class Logger
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ began_at = Time.now
+
+ Log.enter "Processing #{env['PATH_INFO']} (#{env['REMOTE_ADDR']} at #{began_at}) [#{env['REQUEST_METHOD']}]"
+
+ result = nil
+
+ if error = catch(:error) {
+ result = @app.call(env)
+ nil
+ }
+ Log.enter "[500] #{error}\n"
+ Log.enter error.backtrace.join("\n") + "\n\n"
+
+ result = Pakyow.app.response.finish
+ end
+
+ ended_at = Time.now.to_f
+ difference = ((ended_at - began_at.to_f) * 1000).to_f
+
+ Log.enter "Completed in #{difference}ms | #{Pakyow.app.response.status} | [#{Pakyow.app.request.url}]"
+ Log.enter
+
+ result
+ end
+ end
+ end
+end
@@ -0,0 +1,27 @@
+module Pakyow
+ module Middleware
+ class NotFound
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ @app.call(env)
+
+ # 404 if no route matched and no views were found
+ if !Pakyow.app.routed? && (!Pakyow.app.presenter || !Pakyow.app.presenter.presented?)
+ Log.enter "[404] Not Found"
+ Pakyow.app.handle_404
+
+ if Pakyow.app.presenter
+ # consider moving to presenter middleware
+ Pakyow.app.presenter.prepare_for_request(Pakyow.app.request)
+ Pakyow.app.response.body = [Pakyow.app.presenter.content]
+ end
+ end
+
+ Pakyow.app.response.finish
+ end
+ end
+ end
+end
@@ -0,0 +1,24 @@
+module Pakyow
+ module Middleware
+ class Presenter
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ request = Request.new(env)
+
+ #TODO dry up with application (move to Request#new?)
+ base_route, ignore_format = StringUtils.split_at_last_dot(request.path)
+ request.working_path = base_route
+ request.working_method = request.method
+
+ Pakyow.app.presenter.prepare_for_request(request)
+ @app.call(env)
+
+ Pakyow.app.response.body = [Pakyow.app.presenter.content]
+ Pakyow.app.response.finish
+ end
+ end
+ end
+end
@@ -0,0 +1,14 @@
+module Pakyow
+ module Middleware
+ class Reloader
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ @app.reload
+ @app.call(env)
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 3d0c139

Please sign in to comment.