Skip to content

Instantly share code, notes, and snippets.

@careo
Created January 27, 2010 17:46
Show Gist options
  • Select an option

  • Save careo/288039 to your computer and use it in GitHub Desktop.

Select an option

Save careo/288039 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra/async'
require 'memcached'
cache = Memcached.new("localhost:11211")
cache.set "test:Action:/delay/30", "NOT delayed for 30 seconds"
class AsyncTest < Sinatra::Base
register Sinatra::Async
enable :show_exceptions
aget '/' do
body "hello async"
end
aget '/ssi' do
body <<-BODY
<!--# include virtual="/delay/2" -->
<!--# include virtual="/delay/5" -->
<!--# include virtual="/delay/30" -->
BODY
end
aget '/delay/:n' do |n|
EM.add_timer(n.to_i) { body { "delayed for #{n} seconds" } }
end
aget '/raise' do
raise 'boom'
end
end
run AsyncTest.new
worker_processes 1;
events {
worker_connections 1024;
}
http {
# define upstream
upstream sinatra {
server 127.0.0.1:4567;
}
server {
ssi on;
listen 8000;
# All dynamic requests will go here
location / {
default_type text/html;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
# All POST requests go to sinatra directly
if ($request_method = POST) {
proxy_pass http://sinatra;
break;
}
# setup memcached
set $memcached_key "test:Action:$uri";
memcached_pass localhost:11211;
proxy_intercept_errors on;
# fallback if not in cache
error_page 404 502 = /sinatra$uri;
}
location /sinatra/ {
# This means, that we can't get to this location from outside - only by internal redirect
internal;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
# Pass request to sinatr
proxy_pass http://sinatra/;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment