config.ru 1.13 KB
Newer Older
Antonio Terceiro's avatar
Antonio Terceiro committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
require 'debci/api'

LISTING = <<EOF
<!DOCTYPE html>
<html>
  <body>
  <h1>Index of <%= request.path %></h1>
  <div><a href="..">..</a></div>
  <% Dir.chdir(@dir) do %>
    <% Dir.glob('*').each do |f| %>
      <% h = File.directory?(f) ? f + '/': f %>
      <div><a href="<%= h %>"><%= f %></a></div>
    <% end %>
  <% end %>
  </body>
</html>
EOF

class ServeStatic < Sinatra::Base
20 21 22 23 24 25 26 27 28 29 30

  def static!(*args)
    # XXX static! is a private method, so this could break at some point
    if request.path =~ /log\.gz$/
      headers['Content-Encoding'] = 'gzip'
      headers['Content-Type'] = 'text/plain; charset=utf-8'
    end
    super
  end


Antonio Terceiro's avatar
Antonio Terceiro committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  get '/*' do
    if request.path !~ %r{/$}
      return redirect(request.path + '/')
    end
    index = File.join(settings.public_folder, request.path, 'index.html')
    if File.exist?(index)
      send_file(index, type: 'text/html')
    else
      @dir = File.dirname(index)
      if File.directory?(@dir)
        erb LISTING
      else
        halt(404, "<h1>404 Not Found</h1>")
      end
    end
  end
end

app = Rack::Builder.new do
  run ServeStatic
  map '/api' do
    run Debci::API
  end
end

run app