1

次のような HTML ドキュメントが S3 に保存されているとします。

次のルートをマッピングして、Rack (できれば Sinatra) アプリケーションでこれらを提供したいと思います。

get "/posts/:id" do
 render "http://alan.aws-s3-bla-bla.com/posts/#{params[:id]}.html"
end

get "/posts/:posts_id/comments/:comments_id" do
 render "http://alan.aws-s3-bla-bla.com/posts/#{params[:posts_id]}/comments/#{params[:comments_id}.html"
end

これは良い考えですか?どうすればいいですか?

4

1 に答える 1

1

ファイルを取得する間、明らかに待機が発生するため、ファイルをキャッシュするか、etags などを設定して、それを支援することができます。HTMLをローカルまたはリモートに保存する価値があるかどうかは、待機する時間とアクセス頻度、サイズなどに依存すると思います。あなただけがそのビットを解決することができます。

ブロックの最後の式が自動的にレンダリングされる文字列の場合render、ファイルを文字列として開いている限り、呼び出す必要はありません。

外部ファイルを取得して一時ファイルに入れる方法は次のとおりです。

require 'faraday'
require 'faraday_middleware'
#require 'faraday/adapter/typhoeus' # see https://github.com/typhoeus/typhoeus/issues/226#issuecomment-9919517 if you get a problem with the requiring
require 'typhoeus/adapters/faraday'

configure do
  Faraday.default_connection = Faraday::Connection.new( 
    :headers => { :accept =>  'text/plain', # maybe this is wrong
    :user_agent => "Sinatra via Faraday"}
  ) do |conn|
    conn.use Faraday::Adapter::Typhoeus
  end
end

helpers do
  def grab_external_html( url )
    response = Faraday.get url # you'll need to supply this variable somehow, your choice
    filename = url # perhaps change this a bit
    tempfile = Tempfile.open(filename, 'wb') { |fp| fp.write(response.body) }
  end
end

get "/posts/:whatever/" do
  tempfile = grab_external_html whatever # surely you'd do a bit more here…
  tempfile.read
end

これはうまくいくかもしれません。その一時ファイルを閉じることも考えたいかもしれませんが、ガベージ コレクターと OSがそれを処理する必要があります。

于 2013-01-12T20:29:35.950 に答える