Use multiple public directories in Sinatra

My sinatra app is contained in a gem. This means that assets (css / js) live in a gem. This application records on-the-fly generated images and serves them; they are currently writing and serving from a public directory.

I prefer not to write the generated images to gem dir, but to some kind of "cache server" of some kind within the framework of a web application that implements this gem.

The gem is set at /var/www/TE/shared/gems/ruby/1.8/gems/tubemp-0.6.0 , so the assets are, for example. /var/www/TE/shared/gems/ruby/1.8/gems/tubemp-0.6.0/lib/public/css/ .

Gem is deployed in a simple application rack at /var/www/TE/current/ , so I'd rather write and maintain thumbnails from /var/www/TE/current/public .

However, the parameter for custom public-dir allows you to set only one directory:

 set :public_folder, File.join(Dir.pwd, "public") 

Interrupts asset servicing; Dir.pwd is the Rack application directory. The public is now in the Rack-app directory, but this is not the place where the assets are located: they live under the β€œpublic” in stone.

 set :public_folder, File.join(gemdir, "public") 

Service break of generated sketches.

I could rewrite the application to serve either assets or thumbnails through Sinatra, but that seems pretty overhead.

Is this the only way? Or is there a way to give Sinatra two or more public dirs to serve its static elements from?

+6
source share
1 answer

I think there may be several options, but here, as I got a small application for serving static files from two places, an extension and a shared folder:

Catalog layout

 root/ app.rb public/images/foo.jpg lib/sinatra/ gemapp.rb gemapp/public/images/bar.jpg 

Expansion

 # lib/sinatra/gemapp.rb module Sinatra module GemApp def self.registered(app) app.set :gem_images, File.expand_path( "gemapp/public/images", File.dirname(__FILE__)) app.get "/images/:file" do |file| send_file File.join( settings.gem_images, file) end app.get "/gemapp" do "running" end end end register GemApp end 

Main application

 require 'sinatra' require_relative "./lib/sinatra/gemapp.rb" get "/" do "home" end 

It served great for files.

+3
source

Source: https://habr.com/ru/post/944030/


All Articles