Permalink
Browse files

Merge branch 'upstream-master' into clean_up_some_docs

  • Loading branch information...
2 parents a25cc12 + d7456bb commit 106df2d966cea34ab40c581369f1cc2d3e7ba05f @mixonic mixonic committed Jul 9, 2012
View
@@ -36,11 +36,12 @@ structure.
|---- vendor/
|------- stylesheets/
|------- javascripts/
+|---- external/
|- config/
|---- settings.yml
|---- application.rb
|- Rakefile
-|- name_of_your_application.rb
+|- application.rb
|- config.ru
```
@@ -51,7 +52,7 @@ directory structure to create a module system. For example,
`app/javascripts/views/my_view.js` would create a minispade module
named: `#{application_name}/views/my_view`.
-`app/stylsheets` contains all the css/scss/sass files. You can create
+`app/stylesheets` contains all the css/scss/sass files. You can create
your own subdirectory structure if you want.
`app/images` all the images.
@@ -73,7 +74,7 @@ files will be turned into minispade modules. Example:
`Rakefile` defines rake tasks
-`name_of_your_application.rb` defines your application
+`application.rb` defines your application
`config.ru` rack up!
@@ -95,7 +96,7 @@ Now setup the other files:
```
touch Gemfile
-touch name_of_your_application.rb
+touch application.rb
touch Rakefile
touch config.ru
```
@@ -129,33 +130,20 @@ require 'iridium'
class Todos < Iridium::Application
end
-
-# Tell the server where to locate the files
-
-Todos.root = File.dirname __FILE__
```
Now, tell Rack to run a new Todo app.
```ruby
# config.ru
-
-require './todos'
-
+require ::File.expand_path('application', __FILE__)
run Todos
```
-Now, create a rake file so you can compile assets at deploy time
+Now, create a `Rakefile` so you can compile assets at deploy time
```ruby
-
-namespace :assets do
- task :precompile do
- ENV['RACK_ENV'] = 'production'
- require './todos'
- Todos.new.compile_assets
- end
-end
+require 'iridium/tasks'
```
Now you can start the development server like this:
@@ -179,9 +167,11 @@ bundle exec rake assets:precompile # compile all assets in public/
## Configuration
-`config/application.yml` contains all configuration values. `server` is
-the only required key. All other keys are translated to method names.
-For example, this config file:
+`config/application.yml` contains all configuration values.
+All other keys are translated to method names available as
+`ApplicationName.config`
+
+Here is an example config file:
```yml
development:
@@ -209,48 +199,54 @@ required if they exist.
## Configuration
-You can hook into the Rack builder process at the beginning. Here's an
+You can hook into the Rack builder process at the beginning. The
+interface is inspired by the Rails middleware interface. Here's an
exmaple:
```ruby
-Todo.configure do |rack, config|
- rack.use MyCustomMiddleWare.new config.value
+Todos.configure do
+ middleware.use MyCustomMiddleware, 'foo', 'bar', :options => :accepted
end
```
## API Proxy
-The server also includes a simple proxy for your API. Configure the `server`
-value in `application.yml` first. All requests `/api` are proxied to the
-API server. For example, if you request `/api/todos` and server is set
-to `api.example.com`, the resulting request would be:
-`http://api.example.com/todos`.
+You can configure any number of proxies to other API's. You can use the
+proxy to hide access keys and/or avoid CORs problems. Here is an
+example:
+
+```ruby
+# config/application.rb
+
+Todos.configure do
+ config.proxy '/radium', 'http://api.example.com'
+ config.proxy '/twitter', 'http://api.twitter.com'
+ config.proxy '/fb', 'http://horrible-api.facebook.com'
+end
## Development
The pipeline is recompiled before each request in development mode.
## Extras
-I've included a simple middleware you can use to add headers to
-requests. I've included this because our API uses headers to
-authenticate keys. This way I can keep the API key hidden from the
-public and proxy it to the API through this rack app.
-
-Here's an example:
-
-```yml
-development:
- server: "http://api.example.com"
- developer_key: "foo"
- user_api_key: "bar"
-```
+Iridium contains some basic middleware that make it easy to authenticate
+to external APIs
```ruby
# config/enviroment.rb
-Todos.configure do |rack, config|
- rack.use Iridium::Middleware::AddHeader.new 'X-Application-Auth-Token', config.developer_key
+Todos.configure do
+ # Add a header: commonly used to authentication/oauth keys
+ # :if option can be specified to only send the header for certain requests
+ middleware.use Iridium::Middleware::AddHeader.new('X-Application-Auth-Token', config.developer_key, :if => /\/api/)
+
+ # Can add a cookies if you need them
+ middleware.use Iridium::Middleware::Addcookie.new('user_api_key', config.api_key')
+
+ # These middleware calls have shortcut methods as well
+ middleware.add_header 'Foo', 'bar', :if => /\/api/
+ middleware.add_cookie 'Foo', 'bar'
end
```
@@ -260,7 +256,7 @@ Applications built using Iridium can be deployed to heroku out of
the box. Applications will be **compiled and minified** at deploy time.
```
-heroku create --stack cedar
+heroku create
git push master heroku
heroku open
```
View
@@ -18,7 +18,7 @@
require 'iridium/middleware/rack_lint_compatibility'
require 'iridium/middleware/pipeline_reset'
-require 'iridium/middleware/static_assets'
+require 'iridium/middleware/caching'
require 'iridium/middleware/add_header'
require 'iridium/middleware/add_cookie'
@@ -31,10 +31,31 @@
require 'iridium/rack'
module Iridium
+ class << self
+ def application
+ @application
+ end
+
+ def application=(app)
+ @application = app
+ end
+ end
+
class Application
include Configuration
include Pipeline
include Rack
+ include Singleton
+
+ class << self
+ def inherited(base)
+ raise "You cannot have more than one Iridium::Application" if Iridium.application
+ super
+ root_path = File.dirname caller.first.match(/(.+):\d+/)[1]
+ base.root = root_path
+ Iridium.application = base.instance
+ end
+ end
def production?
env == 'production'
View
@@ -11,7 +11,7 @@ def initialize
@middleware_stack = MiddlewareStack.new
@proxies = {}
- @cache_control = "public"
+ @cache_control = "max-age=0, private, must-revalidate"
@cache = {
:metastore => Dalli::Client.new,
@@ -0,0 +1,31 @@
+require 'digest/md5'
+
+module Iridium
+ module Middleware
+ class Caching
+ def initialize(app, root, cache_control)
+ @app, @root, @cache_control = app, root, cache_control
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+
+ if asset? env
+ headers['Last-Modified'] = File.new(asset_path(env)).mtime.httpdate
+ headers['Cache-Control'] = @cache_control
+ end
+
+ [status, headers, body]
+ end
+
+ private
+ def asset_path(env)
+ @root.join(env['PATH_INFO'].gsub(/^\//, '')).to_s
+ end
+
+ def asset?(env)
+ File.exists?(asset_path(env))
+ end
+ end
+ end
+end
@@ -1,46 +0,0 @@
-require 'digest/md5'
-
-module Iridium
- module Middleware
- class StaticAssets
- STATIC_EXTENSIONS = %w(js css jpeg jpg png html)
-
- def initialize(app, root, cache_control)
- @app, @root, @cache_control = app, root, cache_control
- end
-
- def call(env)
- status, headers, body = @app.call(env)
-
- if static_asset? env
- headers['Last-Modified'] = File.new(asset_path(env)).mtime.httpdate
-
- if body
- text = ""
-
- body.each do |part|
- text << part
- end
-
- headers['ETag'] = %Q("#{Digest::MD5.hexdigest(text)}")
- end
-
- headers['Cache-Control'] = @cache_control
- end
-
- [status, headers, body]
- end
-
- private
- def asset_path(env)
- @root.join('site', env['PATH_INFO'].gsub(/^\//, '')).to_s
- end
-
- def static_asset?(env)
- STATIC_EXTENSIONS.select do |ext|
- env['PATH_INFO'] =~ %r{#{ext}$}
- end.first && File.exists?(asset_path(env))
- end
- end
- end
-end
View
@@ -23,33 +23,29 @@ def generate_output(inputs, output)
end
module ClassMethods
- def call(env)
- new.call(env)
- end
-
- def compile_assets
- new.compile_assets
+ def compile
+ instance.compile
end
end
def app_path
- "#{root}/app"
+ root.join 'app'
end
def site_path
- "#{root}/site"
+ root.join 'site'
end
def tmp_path
- "#{root}/tmp"
+ root.join 'tmp'
end
def reset
FileUtils.rm_rf site_path
FileUtils.rm_rf tmp_path
end
- def compile_assets
+ def compile
reset
project.invoke
end
View
@@ -12,7 +12,7 @@ def proxy(url, to)
module ClassMethods
def call(env)
- new.app.call(env)
+ instance.app.call(env)
end
end
@@ -27,7 +27,7 @@ def app
if config.perform_caching
builder.use ::Rack::Cache, config.cache
builder.use ::Rack::ConditionalGet
- builder.use Middleware::StaticAssets, config.root, config.cache_control
+ builder.use Middleware::Caching, server.site_path, config.cache_control
end
config.middleware.each do |middleware|
@@ -48,7 +48,7 @@ def app
builder.use Rake::Pipeline::Middleware, pipeline
end
- builder.run ::Rack::Directory.new root.join('site')
+ builder.run ::Rack::Directory.new server.site_path
builder.to_app
end
View
@@ -0,0 +1,8 @@
+namespace :assets do
+ desc "Compiles assets for production use"
+ task :precompile do
+ ENV['RACK_ENV'] = 'production'
+ require File.expand_path("application", Dir.pwd)
+ Iridium.application.compile
+ end
+end
View
@@ -0,0 +1 @@
+class TestApp < Iridium::Application ; end
Oops, something went wrong.

0 comments on commit 106df2d

Please sign in to comment.