goliath 0.9.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of goliath might be problematic. Click here for more details.

Files changed (84) hide show
  1. data/.gitignore +15 -0
  2. data/.rspec +2 -0
  3. data/.yardopts +2 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE +66 -0
  6. data/README.md +86 -0
  7. data/Rakefile +18 -0
  8. data/examples/activerecord/config/srv.rb +7 -0
  9. data/examples/activerecord/srv.rb +37 -0
  10. data/examples/async_upload.rb +34 -0
  11. data/examples/conf_test.rb +27 -0
  12. data/examples/config/conf_test.rb +12 -0
  13. data/examples/config/echo.rb +1 -0
  14. data/examples/config/http_log.rb +7 -0
  15. data/examples/config/shared.rb +5 -0
  16. data/examples/custom_server.rb +57 -0
  17. data/examples/echo.rb +37 -0
  18. data/examples/gziped.rb +40 -0
  19. data/examples/hello_world.rb +10 -0
  20. data/examples/http_log.rb +85 -0
  21. data/examples/rack_routes.rb +44 -0
  22. data/examples/stream.rb +37 -0
  23. data/examples/valid.rb +19 -0
  24. data/goliath.gemspec +41 -0
  25. data/lib/goliath.rb +38 -0
  26. data/lib/goliath/api.rb +165 -0
  27. data/lib/goliath/application.rb +90 -0
  28. data/lib/goliath/connection.rb +94 -0
  29. data/lib/goliath/constants.rb +51 -0
  30. data/lib/goliath/env.rb +92 -0
  31. data/lib/goliath/goliath.rb +49 -0
  32. data/lib/goliath/headers.rb +37 -0
  33. data/lib/goliath/http_status_codes.rb +44 -0
  34. data/lib/goliath/plugins/latency.rb +33 -0
  35. data/lib/goliath/rack/default_mime_type.rb +30 -0
  36. data/lib/goliath/rack/default_response_format.rb +33 -0
  37. data/lib/goliath/rack/formatters/html.rb +90 -0
  38. data/lib/goliath/rack/formatters/json.rb +42 -0
  39. data/lib/goliath/rack/formatters/xml.rb +90 -0
  40. data/lib/goliath/rack/heartbeat.rb +23 -0
  41. data/lib/goliath/rack/jsonp.rb +38 -0
  42. data/lib/goliath/rack/params.rb +30 -0
  43. data/lib/goliath/rack/render.rb +66 -0
  44. data/lib/goliath/rack/tracer.rb +31 -0
  45. data/lib/goliath/rack/validation/boolean_value.rb +59 -0
  46. data/lib/goliath/rack/validation/default_params.rb +46 -0
  47. data/lib/goliath/rack/validation/numeric_range.rb +59 -0
  48. data/lib/goliath/rack/validation/request_method.rb +33 -0
  49. data/lib/goliath/rack/validation/required_param.rb +54 -0
  50. data/lib/goliath/rack/validation/required_value.rb +58 -0
  51. data/lib/goliath/rack/validation_error.rb +38 -0
  52. data/lib/goliath/request.rb +199 -0
  53. data/lib/goliath/response.rb +93 -0
  54. data/lib/goliath/runner.rb +236 -0
  55. data/lib/goliath/server.rb +149 -0
  56. data/lib/goliath/test_helper.rb +118 -0
  57. data/lib/goliath/version.rb +4 -0
  58. data/spec/integration/async_request_processing.rb +23 -0
  59. data/spec/integration/echo_spec.rb +27 -0
  60. data/spec/integration/keepalive_spec.rb +28 -0
  61. data/spec/integration/pipelining_spec.rb +43 -0
  62. data/spec/integration/valid_spec.rb +24 -0
  63. data/spec/spec_helper.rb +6 -0
  64. data/spec/unit/connection_spec.rb +59 -0
  65. data/spec/unit/env_spec.rb +55 -0
  66. data/spec/unit/headers_spec.rb +53 -0
  67. data/spec/unit/rack/default_mime_type_spec.rb +34 -0
  68. data/spec/unit/rack/formatters/json_spec.rb +54 -0
  69. data/spec/unit/rack/formatters/xml_spec.rb +66 -0
  70. data/spec/unit/rack/heartbeat_spec.rb +47 -0
  71. data/spec/unit/rack/params_spec.rb +94 -0
  72. data/spec/unit/rack/render_spec.rb +87 -0
  73. data/spec/unit/rack/validation/boolean_value_spec.rb +54 -0
  74. data/spec/unit/rack/validation/default_params_spec.rb +71 -0
  75. data/spec/unit/rack/validation/numeric_range_spec.rb +96 -0
  76. data/spec/unit/rack/validation/request_method_spec.rb +47 -0
  77. data/spec/unit/rack/validation/required_param_spec.rb +92 -0
  78. data/spec/unit/rack/validation/required_value_spec.rb +99 -0
  79. data/spec/unit/rack/validation_error_spec.rb +40 -0
  80. data/spec/unit/request_spec.rb +59 -0
  81. data/spec/unit/response_spec.rb +35 -0
  82. data/spec/unit/runner_spec.rb +129 -0
  83. data/spec/unit/server_spec.rb +137 -0
  84. metadata +409 -0
@@ -0,0 +1,15 @@
1
+ Makefile
2
+ mkmf.log
3
+ *.o
4
+ *.bundle
5
+ doc/
6
+ pkg/
7
+ examples/log
8
+ *.swp
9
+ Gemfile.lock
10
+ .yardoc
11
+ .livereload
12
+ *.watchr
13
+ *.rbc
14
+ .rvmrc
15
+ _site
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,2 @@
1
+ --main README.md
2
+ --no-private
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,66 @@
1
+ Copyright (c) 2011 PostRank Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
+
20
+
21
+
22
+ Portions of this code are from the thin project (https://github.com/macournoyer/thin) and under the following license:
23
+ Copyright (c) 2008 Marc-Andre Cournoyer
24
+
25
+ Permission is hereby granted, free of charge, to any person obtaining a copy
26
+ of this software and associated documentation files (the "Software"), to
27
+ deal in the Software without restriction, including without limitation the
28
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
29
+ sell copies of the Software, and to permit persons to whom the Software is
30
+ furnished to do so, subject to the following conditions:
31
+
32
+ The above copyright notice and this permission notice shall be included in
33
+ all copies or substantial portions of the Software.
34
+
35
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
38
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
39
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41
+
42
+
43
+
44
+ Portions of this code are from the Sinatra project (https://github.com/bmizerany/sinatra) and under the following license:
45
+ Copyright (c) 2007, 2008, 2009 Blake Mizerany
46
+
47
+ Permission is hereby granted, free of charge, to any person
48
+ obtaining a copy of this software and associated documentation
49
+ files (the "Software"), to deal in the Software without
50
+ restriction, including without limitation the rights to use,
51
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
52
+ copies of the Software, and to permit persons to whom the
53
+ Software is furnished to do so, subject to the following
54
+ conditions:
55
+
56
+ The above copyright notice and this permission notice shall be
57
+ included in all copies or substantial portions of the Software.
58
+
59
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
60
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
61
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
62
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
63
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
64
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
65
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
66
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ # Goliath
2
+
3
+ Goliath is an open source version of the non-blocking (asynchronous) Ruby web server framework powering PostRank. It is a lightweight framework designed to meet the following goals: bare metal performance, Rack API and middleware support, simple configuration, fully asynchronous processing, and readable and maintainable code (read: no callbacks).
4
+
5
+ The framework is powered by an EventMachine reactor, a high-performance HTTP parser and Ruby 1.9 runtime. The one major advantage Goliath has over other asynchronous frameworks is the fact that by leveraging Ruby fibers introduced in Ruby 1.9+, it can untangle the complicated callback-based code into a format we are all familiar and comfortable with: linear execution, which leads to more maintainable and readable code.
6
+
7
+ Each HTTP request within Goliath is executed in its own Ruby fiber and all asynchronous I/O operations can transparently suspend and later resume the processing without requiring the developer to write any additional code. Both request processing and response processing can be done in fully asynchronous fashion: streaming uploads, firehose API's, request/response, and so on.
8
+
9
+ ## Installation & Prerequisites
10
+
11
+ * Install Ruby 1.9 (via RVM or natively)
12
+
13
+ $> gem install rvm
14
+ $> rvm install 1.9.2
15
+ $> rvm use 1.9.2
16
+
17
+ * Install Goliath:
18
+
19
+ $> gem install goliath
20
+
21
+ ## Getting Started: Hello World
22
+
23
+ require 'goliath'
24
+
25
+ class Hello < Goliath::API
26
+ # reload code on every request in dev environment
27
+ use ::Rack::Reloader, 0 if Goliath.dev?
28
+
29
+ def response(env)
30
+ [200, {}, "Hello World"]
31
+ end
32
+ end
33
+
34
+ > ruby hello.rb -sv
35
+ > [97570:INFO] 2011-02-15 00:33:51 :: Starting server on 0.0.0.0:9000 in development mode. Watch out for stones.
36
+
37
+ See examples directory for more, hands-on examples of building Goliath powered web-services.
38
+
39
+ ## Performance: MRI, JRuby, Rubinius
40
+
41
+ Goliath is not tied to a single Ruby runtime - it is able to run on MRI Ruby, JRuby and Rubinius today. Depending on which platform you are working with, you will see different performance characteristics. At the moment, we recommend MRI Ruby 1.9.2 as the best performing VM: a roundtrip through the full Goliath stack on MRI 1.9.2p136 takes ~0.33ms (~3000 req/s).
42
+
43
+ JRuby performance (with 1.9 mode enabled) is currently much worse than MRI Ruby 1.9.2, due to the fact that all JRuby fibers are mapped to native Java threads. However, there is [very promising](http://classparser.blogspot.com/2010/04/jruby-coroutines-really-fast.html), existing work that promises to make JRuby fibers even faster than those of MRI Ruby. Once this functionality is built into JRuby ([JRUBY-5461](http://jira.codehaus.org/browse/JRUBY-5461)), JRuby may well take the performance crown. At the moment, without the MLVM support, a request through the full Goliath stack takes ~6ms (166 req/s).
44
+
45
+ Rubinius + Goliath performance is tough to pin down - there is a lot of room for optimization within the Rubinius VM. Currently, requests can take as little as 0.2ms and later spike to 50ms+. Stay tuned!
46
+
47
+ Goliath has been in production at PostRank for over a year, serving a sustained 500 requests/s for internal and external applications. Many of the Goliath processes have been running for months at a time (read: no memory leaks) and have served hundreds of gigabytes of data without restarts. To scale up and provide failover and redundancy, our individual Goliath servers at PostRank are usually deployed behind a reverse proxy (such as HAProxy).
48
+
49
+ ## FAQ
50
+
51
+ * How does Goliath compare to other Ruby async app-servers like Thin?
52
+ * They are similar (both use Eventmachine reactor), but also very different. Goliath is able to run on different Ruby platforms (see above), uses a different HTTP parser, supports HTTP keepalive & pipelining, and offers a fully asynchronous API for both request and response processing.
53
+
54
+ * How does Goliath compare to Mongrel, Passenger, Unicorn?
55
+ * Mongrel is a threaded web-server, and both Passenger and Unicorn fork an entire VM to isolate each request from each other. By contrast, Goliath builds a single instance of the Rack app and runs all requests in parallel through a single VM, which leads to a much smaller memory footprint and less overhead.
56
+
57
+ * How do I deploy Goliath in production?
58
+ * We recommend deploying Goliath behind a reverse proxy such as HAProxy, Nginx or equivalent. Using one of the above, you can easily run multiple instances of the same application and load balance between then within the reverse proxy.
59
+
60
+ ## Guides
61
+
62
+ * [Server Options](https://github.com/postrank-labs/goliath/wiki/Server)
63
+ * [Middleware](https://github.com/postrank-labs/goliath/wiki/Middleware)
64
+ * [Configuration](https://github.com/postrank-labs/goliath/wiki/Configuration)
65
+ * [Plugins](https://github.com/postrank-labs/goliath/wiki/Plugins)
66
+
67
+ ### Hands-on applications:
68
+
69
+ * [Asynchronous HTTP, MySQL, etc](https://github.com/postrank-labs/goliath/wiki/Asynchronous-Processing)
70
+ * [Response streaming with Goliath](https://github.com/postrank-labs/goliath/wiki/Streaming)
71
+ * [Examples](https://github.com/postrank-labs/goliath/tree/master/examples)
72
+
73
+ ## Coverage
74
+
75
+ * [Goliath: Non-blocking, Ruby 1.9 Web Server](http://www.igvita.com/2011/03/08/goliath-non-blocking-ruby-19-web-server)
76
+ * [Stage left: Enter Goliath - HTTP Proxy + MongoDB](http://everburning.com/news/stage-left-enter-goliath/)
77
+
78
+ ## Discussion and Support
79
+
80
+ * [Source](https://github.com/postrank-labs/goliath)
81
+ * [Issues](https://github.com/postrank-labs/goliath/issues)
82
+ * [Mailing List](http://groups.google.com/group/goliath-io)
83
+
84
+ ## License & Acknowledgments
85
+
86
+ Goliath is distributed under the MIT license, for full details please see the LICENSE file.
@@ -0,0 +1,18 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'yard'
5
+ require 'rspec/core/rake_task'
6
+
7
+ task :default => [:spec]
8
+
9
+ desc "run spec tests"
10
+ RSpec::Core::RakeTask.new('spec') do |t|
11
+ t.pattern = 'spec/**/*_spec.rb'
12
+ end
13
+
14
+ desc 'Generate documentation'
15
+ YARD::Rake::YardocTask.new do |t|
16
+ t.files = ['lib/**/*.rb', '-', 'LICENSE']
17
+ t.options = ['--main', 'README.md', '--no-private']
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'mysql2'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => 'em_mysql2',
4
+ :database => 'goliath_test',
5
+ :username => 'goliath',
6
+ :password => 'goliath',
7
+ :host => 'localhost')
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # gem install activerecord
4
+ # gem install mysql2
5
+
6
+ # create database goliath_test
7
+ # create user 'goliath'@'localhost' identified by 'goliath'
8
+ # grant all on goliath_test.* to 'goliath'@'localhost'
9
+ # create table users (id int not null auto_increment primary key, name varchar(255), email varchar(255));
10
+ # insert into users (name, email) values ('dan', 'dj2@everyburning.com'), ('Ilya', 'ilya@igvita.com');
11
+
12
+ $: << "../../lib" << "./lib"
13
+
14
+ require 'goliath'
15
+ require 'active_record'
16
+ require 'yajl'
17
+
18
+ class User < ActiveRecord::Base
19
+ end
20
+
21
+ class Srv < Goliath::API
22
+ use ::Rack::Reloader, 0 if Goliath.dev?
23
+
24
+ use Goliath::Rack::Params
25
+ use Goliath::Rack::DefaultMimeType
26
+ use Goliath::Rack::Formatters::JSON
27
+ use Goliath::Rack::Render
28
+ use Goliath::Rack::ValidationError
29
+
30
+ use Goliath::Rack::Validation::RequiredParam, {:key => 'id', :type => 'ID'}
31
+ use Goliath::Rack::Validation::NumericRange, {:key => 'id', :min => 1}
32
+
33
+ def response(env)
34
+ User.find_by_sql("SELECT SLEEP(10)")
35
+ [200, {}, User.find(params['id'])]
36
+ end
37
+ end
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ $:<< '../lib' << 'lib'
3
+
4
+ require 'goliath'
5
+ require 'yajl'
6
+
7
+ class AsyncUpload < Goliath::API
8
+
9
+ # reload code on every request in dev environment
10
+ use ::Rack::Reloader, 0 if Goliath.dev?
11
+
12
+ use Goliath::Rack::Params # parse & merge query and body parameters
13
+ use Goliath::Rack::DefaultMimeType # cleanup accepted media types
14
+ use Goliath::Rack::Formatters::JSON # JSON output formatter
15
+ use Goliath::Rack::Render # auto-negotiate response format
16
+
17
+ def on_headers(env, headers)
18
+ env.logger.info 'received headers: ' + headers.inspect
19
+ env['async-headers'] = headers
20
+ end
21
+
22
+ def on_body(env, data)
23
+ env.logger.info 'received data: ' + data
24
+ (env['async-body'] ||= '') << data
25
+ end
26
+
27
+ def on_close(env)
28
+ env.logger.info 'closing connection'
29
+ end
30
+
31
+ def response(env)
32
+ [200, {}, {body: env['async-body'], head: env['async-headers']}]
33
+ end
34
+ end
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ $:<< '../lib' << 'lib'
3
+
4
+ #
5
+ # Example of using "config/conf_test.rb" during server initialization
6
+ # - options parser allows us to extend the command line parameters for API
7
+ # - config file shows loading of environment specific, global, and shared variables
8
+ #
9
+
10
+ require 'goliath'
11
+
12
+ class ConfTest < Goliath::API
13
+
14
+ use Goliath::Rack::Params
15
+ use Goliath::Rack::DefaultMimeType
16
+ use Goliath::Rack::Formatters::JSON
17
+ use Goliath::Rack::Render
18
+
19
+ def options_parser(opts, options)
20
+ options[:test] = 0
21
+ opts.on('-t', '--test NUM', "The test number") { |val| options[:test] = val.to_i }
22
+ end
23
+
24
+ def response(env)
25
+ [200, {}, {:response => env.config}]
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ config['global'] = 'my global'
2
+ config['test'] = options[:test]
3
+
4
+ import('shared')
5
+
6
+ environment :production do
7
+ config['prod'] = 'my prod'
8
+ end
9
+
10
+ environment :development do
11
+ config['dev'] = 'my dev'
12
+ end
@@ -0,0 +1 @@
1
+ import('shared')
@@ -0,0 +1,7 @@
1
+ config['forwarder'] = 'http://localhost:8080'
2
+
3
+ config['mongo'] = EventMachine::Synchrony::ConnectionPool.new(size: 20) do
4
+ # Need to deal with this just never connecting ... ?
5
+ conn = EM::Mongo::Connection.new('localhost', 27017, 1, {:reconnect_in => 1})
6
+ conn.db('http_log').collection('aggregators')
7
+ end
@@ -0,0 +1,5 @@
1
+ config['shared'] = 'shared'
2
+
3
+ environment :development do
4
+ # shared development only configuration
5
+ end
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ $:<< '../lib' << 'lib'
3
+
4
+ require 'goliath/api'
5
+ require 'goliath/runner'
6
+
7
+ # Example demonstrating how to use a custom rack builder with the
8
+ # Goliath server and mixing Goliath APIs with normal Rack end points.
9
+ #
10
+ # Note, that the same routing behavior is supported by Goliath, loost at
11
+ # the rack_routes.rb example to see how to define custom routes.
12
+
13
+ # Our custom Goliath API
14
+ class HelloWorld < Goliath::API
15
+ def response(env)
16
+ [200, {}, "hello world!"]
17
+ end
18
+ end
19
+
20
+ # Another Goliath API
21
+ class Bonjour < Goliath::API
22
+ def response(env)
23
+ [200, {}, "bonjour!"]
24
+ end
25
+ end
26
+
27
+ # Rack builder acting as a router
28
+ router = Rack::Builder.new do
29
+ # Rack end point
30
+ map '/version' do
31
+ use ::Rack::ContentLength
32
+ run Proc.new {|env| [200, {"Content-Type" => "text/html"}, ["Version 0.1"]] }
33
+ end
34
+
35
+ # map the /hello_world uri to our Goliath API
36
+ map "/hello_world" do
37
+ use ::Rack::ContentLength
38
+ run HelloWorld.new
39
+ end
40
+
41
+ # map the /bonjour uri to our other Goliath API
42
+ map "/bonjour" do
43
+ use ::Rack::ContentLength
44
+ run Bonjour.new
45
+ end
46
+
47
+ # catch the root route and return a 404
48
+ map "/" do
49
+ use ::Rack::ContentLength
50
+ run Proc.new {|env| [404, {"Content-Type" => "text/html"}, ["Try /version /hello_world or /bonjour"]] }
51
+ end
52
+ end
53
+
54
+ runner = Goliath::Runner.new(ARGV, nil)
55
+ runner.app = router
56
+ runner.run
57
+
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ $:<< '../lib' << 'lib'
3
+
4
+ require 'goliath'
5
+ require 'goliath/plugins/latency'
6
+
7
+ # Goliath uses multi-jon, so pick your favorite JSON serializer
8
+ # require 'json'
9
+ require 'yajl'
10
+
11
+ class Echo < Goliath::API
12
+
13
+ # reload code on every request in dev environment
14
+ use ::Rack::Reloader, 0 if Goliath.dev?
15
+
16
+ use Goliath::Rack::Params # parse & merge query and body parameters
17
+ use Goliath::Rack::DefaultMimeType # cleanup accepted media types
18
+ use Goliath::Rack::Formatters::JSON # JSON output formatter
19
+ use Goliath::Rack::Render # auto-negotiate response format
20
+ use Goliath::Rack::Heartbeat # respond to /status with 200, OK (monitoring, etc)
21
+ use Goliath::Rack::ValidationError # catch and render validation errors
22
+
23
+ use Goliath::Rack::Validation::RequestMethod, %w(GET) # allow GET requests only
24
+ use Goliath::Rack::Validation::RequiredParam, {:key => 'echo'} # must provide ?echo= query or body param
25
+
26
+ plugin Goliath::Plugin::Latency # output reactor latency every second
27
+
28
+ def process_request
29
+ logger.info "Processing request"
30
+
31
+ {response: env.params['echo']}
32
+ end
33
+
34
+ def response(env)
35
+ [200, {}, process_request]
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+ $:<< '../lib' << 'lib'
3
+
4
+ #
5
+ # Example of using the rack/deflater middleware to automatically GZIP your response
6
+ # if the client indicated that it will accept gzipped data.
7
+ #
8
+ # Note that we're also using Rack::Rewrite to alter incoming request prior to it
9
+ # being parsed by the Rack::Params middleware. This allows us to transparently
10
+ # rewrite incoming requests before any processing is done on it.
11
+ #
12
+ # curl -s -H "Accept-Encoding: gzip,deflate" -H "Connection: close" localhost:9000?gziped=test | gunzip
13
+ #
14
+
15
+ require 'rack/deflater'
16
+ require 'rack/rewrite'
17
+ require 'goliath'
18
+ require 'yajl'
19
+
20
+ class Gziped < Goliath::API
21
+ # if client requested, compress the response
22
+ use ::Rack::Deflater
23
+
24
+ # example of using rack rewriting to rewrite the param gziped to echo
25
+ use ::Rack::Rewrite do
26
+ rewrite %r{^(.*?)\??gziped=(.*)$}, lambda { |match, env| "#{match[1]}?echo=#{match[2]}" }
27
+ end
28
+
29
+ use Goliath::Rack::Params # parse & merge query and body parameters
30
+ use Goliath::Rack::Formatters::JSON # JSON output formatter
31
+ use Goliath::Rack::Render # auto-negotiate response format
32
+ use Goliath::Rack::ValidationError # catch and render validation errors
33
+
34
+ use Goliath::Rack::Validation::RequestMethod, %w(GET) # allow GET requests only
35
+ use Goliath::Rack::Validation::RequiredParam, {:key => 'echo'} # must provide ?echo= query or body param
36
+
37
+ def response(env)
38
+ [200, {}, {response: env.params['echo']}]
39
+ end
40
+ end