Skip to content

Instantly share code, notes, and snippets.

@rkh
Created May 30, 2011 16:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkh/999120 to your computer and use it in GitHub Desktop.
Save rkh/999120 to your computer and use it in GitHub Desktop.
Sinatra Stylesheet Expires
# avoid path traversal attacks and speed up look-up
set :css_files, {}
Dir.glob("#{settings.public}/css/*.css") { |file| settings.css_files[File.basename(file, '.css')] = file }
get "/css/:name.css" do |name|
expires 60*60*24*356*3
send_file setting.css_files[name]
end
@dropwhile
Copy link

I think you can do this in one line if desired too.

set :css_files, Dir.glob("#{settings.public}/css/*.css").inject({}) { |css,file| css[File.basename(file, '.css')] = file; css }

@rkh
Copy link
Author

rkh commented May 31, 2011

Yes, I intentionally avoided that to not confuse ppl new to Ruby.

@dropwhile
Copy link

doh!

@gustaflindqvist
Copy link

Thanks rkh and also cactus! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment