I am using Ruby on Rails 3 and I am developing API for my applications.
I have a web client app that make HTTP requests to a web server app. However the web application handles incoming request in the controller and responds like this:
respond_to do |format|
format.html {redirect_to @account}
...
format.json {
render :json => @account.to开发者_运维百科_json, :status => 200
}
format.xml {
render :xml => @account.to_xml, :status => 200
}
end
At this time I don't use Rack middlewares because it is very "convenient" to respond using respond_to
, and you don't have to change anything more in your application other than the controller. Anyway, I know that using middleware is faster in responding than the above approach, but I should implement responses for every HTTP request, maybe intercepting them URI.
What do you advice about? It is better "convenience" and "slowness" (like the code above) or "complexity" and "fastest"?
Premature optimization is the root of all evil. If the code is fast enough for your purposes using the design that you prefer, keep using it. If you find that it's not fast enough, then you can look at options for speeding it up.
精彩评论