Guest User

Untitled

a guest
May 15th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.61 KB | None | 0 0
  1. require 'bundler/setup'
  2. require 'sinatra/base'
  3.  
  4. # The project root directory
  5. $root = ::File.dirname(__FILE__)
  6.  
  7. class SinatraStaticServer < Sinatra::Base  
  8.  
  9.   get(/.+/) do
  10.     send_sinatra_file(request.path) {404}
  11.   end
  12.  
  13.   not_found do
  14.     send_sinatra_file('404.html') {"Sorry, I cannot find #{request.path}"}
  15.   end
  16.  
  17.   def send_sinatra_file(path, &missing_file_block)
  18.     file_path = File.join(File.dirname(__FILE__), 'public',  path)
  19.     file_path = File.join(file_path, 'index.html') unless file_path =~ /\.[a-z]+$/i  
  20.     File.exist?(file_path) ? send_file(file_path) : missing_file_block.call
  21.   end
  22.  
  23. end
Add Comment
Please, Sign In to add comment