Created
October 17, 2016 08:37
-
-
Save suhanlee/f2a80a00131517d959a470dd8be5a313 to your computer and use it in GitHub Desktop.
Sinatra local directory server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
$pwd = ENV['PWD'] | |
configure do | |
set :server, :puma | |
set :root, File.dirname(__FILE__) | |
set :port, 7777 | |
end | |
get '/' do | |
@files = Dir.entries($pwd) | |
erb :index | |
end | |
get '/*' do | |
files = params['splat'].join("") | |
if Dir.exists?(files) | |
@dir = "/" + files | |
@files = Dir.entries($pwd + '/' + files) | |
erb :index | |
else | |
send_file files | |
end | |
end | |
__END__ | |
@@ index | |
<% @files.each do |file| %> | |
<a href="<%= @dir %>/<%=file %>"><%=file %></a><br> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment