rack

Instance_eval block not supplied? [duplicate]

孤街醉人 提交于 2019-11-30 09:56:58
问题 This question already has answers here : Code block passed to each works with brackets but not with 'do'-'end' (ruby) (3 answers) Closed 3 years ago . Does anybody know what's causing this error? I'm trying to make a basic rack application. App.rb => class Cherry class << self def app &block Cherry::Application.new &block end end class Application def initialize &block instance_eval &block end def print_start_message puts "Starting server" end def call env [200, {"Content-type" => "text/plain

Trigger Rack middleware on specific Rails routes

六月ゝ 毕业季﹏ 提交于 2019-11-30 08:24:58
问题 Is it possible to trigger Rack middleware only on specific Rails routes? For example, let's say I wanted to run a rate limiter middleware only on the api namespace. namespace :api do resources :users end 回答1: I've had good success with Rack::Throttle for rate limiting. Subclass one of the built-in throttle classes and overload the allowed? method. Your custom logic can check which controller is being accessed and apply the rate limit as needed. class ApiThrottle < Rack::Throttle::Hourly ## #

Where do you put your Rack middleware files and requires?

落爺英雄遲暮 提交于 2019-11-30 06:20:33
问题 I'm in the process of refactoring some logic built into a Rails application into middleware, and one annoyance I've run into is a seeming lack of convention for where to put them. Currently I've settled on app/middleware but I could just as easily move it to vendor/middleware or maybe vendor/plugins/middleware ... The biggest problem is having to require the individual files at the top of config/environment.rb require "app/middleware/system_message" require "app/middleware/rack_backstage" or

How can you block or filter IP addresses on Heroku?

江枫思渺然 提交于 2019-11-30 04:41:39
Is there a way to implement IP filtering or IP access rules much like I would with nginx/apache to restrict or block certain IPs on Heroku? Note: I know this can be done from within my application (Rails 3.2) very easily but I don't think this is the most efficient use of my resources on Heroku. Also, a Rack based solution would be better than implementing the filtering in Rails. I added 'rack-block' as Rack middleware. In config/initializers, add a new file: YourApp::Application.configure do config.middleware.insert_before(Rack::Lock, Rack::Block) do # Add your rules with the rack-block

mongoid query caching

谁都会走 提交于 2019-11-30 03:55:33
Rails' ActiveRecord has a feature called Query Caching (ActiveRecord::QueryCache) which saves the result of SQL query for the life-span of a request. While I'm not very familiar with the internals of the implementation, I think that it saves the query results somewhere in the Rack env, which is discarded in the end of the request. The Mongoid, unfortunately, doesn't currently provide such feature, and this is exacerbated by the fact, that some queries occur implicitly (references). I'm considering to implement this feature, and I'm curious, where and how Mongoid (or, perhaps, mongo driver?)

分布式存储原理:ElasticSearch

你离开我真会死。 提交于 2019-11-30 03:48:48
对于一个分布式存储系统来说, 数据是分散存储在多个节点上的 。如何让数据 均衡 的分布在不同节点上,来保证其高可用性? 所谓均衡,是指系统中每个节点的负载是均匀的,并且在发现有不均匀的情况或者有节点增加/删除时,能及时进行调整,保持均匀状态 。本文将探讨Elasticsearch的数据分布方法,文中所述的背景是Elasticsearch 5.5。   在Elasticsearch中,以Shard为最小的数据分配/迁移单位。数据到节点的映射分离为两层:一层是数据到Shard的映射(Route),另一层是Shard到节点的映射(Allocate)。   一方面,插入一条数据时, ES会根据指定的Key来计算应该落到哪个Shard上 。默认Key是自动分配的id,可以自定义,比如在我们的业务中采用CompanyID作为Key。 因为Primary Shard的个数是不允许改变的,所以同一个Key每次算出来的Shard是一样的,从而保证了准确定位 。 shard_num = hash(_routing) % num_primary_shards   另一方面,Master会为每个Shard分配相应的Data节点进行存储,并维护相关元信息。通过Route计算出来的Shard序号,在元信息中找到对应的存储节点,便可完成数据分布。Shard Allocate的映射关系并不是完全不变的

rails json response with gzip compression

一曲冷凌霜 提交于 2019-11-30 03:31:47
I have an api written in rails which on each request responds with a JSON response. The response could be huge, so i need to compress the JSON response using gzip. Wondering how to do this in rails controller? I have added the line use Rack::Deflater in config.ru Should I also be changing something in the line which renders JSON? render :json => response.to_json() Also, how do i check if the response is in gzip format or not..?? I did a curl request from terminal, I see only the normal plain JSON. My post Content Compression with Rack::Deflater describes a couple of ways to integrate Rack:

How do I use a Rack middleware only for certain paths?

孤者浪人 提交于 2019-11-30 02:25:22
I'd like to have MyMiddleware run in my Rack app, but only for certain paths. I was hoping to use Rack::Builder or at least Rack::URLMap , but I can't quite figure out how. This is what I thought would work, but doesn't: # in my rackup file or Rails environment.rb: map '/foo' do use MyMiddleware, { :some => 'options' } end Or, better yet, with a Regexp: map /^foo/ do use MyMiddleware, { :some => 'options' } end But map seems to demand an app at the end; it won't fall back on just passing control back to its parent. (The actual error is " undefined method 'each' for nil:NilClass " from when

How do I specify Origin Whitelist Options in Sinatra using Rack/Protection

时光怂恿深爱的人放手 提交于 2019-11-30 00:27:23
I have a web app, lets say http://web.example.com making a POST request to http://api.example.com . The api server is running the latest version of Sinatra with rack protection enabled. I am getting this error 'attack prevented by Rack::Protection::HttpOrigin'. I can do something like this: set :protection, :except => [:http_origin] but I feel like I am just ignoring the actual problem. I have tried to do this: use Rack::Protection::HttpOrigin, :origin_whitelist => ['http://web.example.com'] but I still get the warning. The request does not get rejected, but Sinatra clears my session see this

Any way to serve gzip assets from heroku?

帅比萌擦擦* 提交于 2019-11-30 00:08:49
I'm wondering if there is any way to get the Rails webserver (thin) to serve the *.gz files the asset pipeline creates. As I understand, those have a higher compression level than that of Rack::Deflater , which only works with serve_static_assets from within the rackup file and not in config.middleware . A less optimal solution might be to change the default compression level of Zlib which is what Rack::Deflater references. It should only need to gzip once, then it goes to Rack::Cache , then hopefully a CDN. A second less optimal solution might be a Rack::Rewrite . Mike The answer to your