Skip to content

Instantly share code, notes, and snippets.

@mvasin
Created January 18, 2018 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvasin/63ec16c9be08355703583848ddc18a9b to your computer and use it in GitHub Desktop.
Save mvasin/63ec16c9be08355703583848ddc18a9b to your computer and use it in GitHub Desktop.
A simple rack app for educational purposes: upload a file and have it presented in the browser
class App
def self.call env
new(env).call
end
def initialize env
@request = Rack::Request.new env
@response = Rack::Response.new
end
def set_response
case @request.request_method
when 'GET'
@response.set_header 'Content-Type', 'text/html'
@response.write '<form method="post" enctype="multipart/form-data">'
@response.write '<input type="file" name="myfile"></input><input type="submit"></input>'
when 'POST'
@response.set_header 'Content-Type', Rack::Mime.mime_type(File.extname @request.params['myfile'][:tempfile])
@response.write @request.params['myfile'][:tempfile].read
else
raise 'Method unknown!'
end
end
def call
set_response
@response.finish
end
end
run App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment