Last active
March 2, 2017 15:59
-
-
Save adamluzsi/badf3ac5d40db335b45972aca4b30cd8 to your computer and use it in GitHub Desktop.
rack-app example for main page
This file contains 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 'json' | |
require 'rack/app' | |
class MyApp < Rack::App | |
headers 'Access-Control-Allow-Origin' => '*', | |
'Access-Control-Expose-Headers' => 'X-My-Custom-Header, X-Another-Custom-Header' | |
serializer do |obj| | |
if obj.is_a?(String) | |
obj | |
else | |
JSON.dump(obj) | |
end | |
end | |
error StandardError, NoMethodError do |ex| | |
{ error: ex.message } | |
end | |
get '/bad/endpoint' do | |
no_method_error_here | |
end | |
desc 'hello world endpoint' | |
validate_params do | |
required 'words', class: Array, of: String, | |
desc: 'words that will be joined with space', | |
example: %w(dog cat) | |
required 'to', class: String, | |
desc: 'the subject of the conversation' | |
end | |
get '/validated' do | |
return "Hello #{validated_params['to']}: #{validated_params['words'].join(' ')}" | |
end | |
get '/' do | |
{ hello: 'world' } | |
end | |
mount MediaFileServer, to: "/assets" | |
mount Uploader, to: '/upload' | |
end | |
# for more check out how-to | |
run MyApp |
This file contains 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
class MediaFileServer < Rack::App | |
serve_files_from '/folder/from/project/root', to: '/files' | |
get '/' do | |
serve_file 'custom_file_path_to_stream_back' | |
end | |
end |
This file contains 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 'fileutils' | |
class Uploader < Rack::App | |
post '/to_stream' do | |
payload_stream do |string_chunk| | |
# do some work | |
end | |
end | |
post '/upload_file' do | |
file_path = Rack::App::Utils.pwd('/upliads', params['user_id'], params['file_name']) | |
FileUtils.mkdir_p(file_path) | |
payload_to_file(file_path) | |
end | |
post '/memory_buffered_payload' do | |
payload #> request payload string | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment