Эх сурвалжийг харах

Add Hanami framework to Ruby (#2764)

* Add Hanami framework to Ruby

* Fix tests

* Remove test and dev files

* Fix deprecation warnings for Sequel 5.0
Jester 8 жил өмнө
parent
commit
e0a2c8ebac
38 өөрчлөгдсөн 937 нэмэгдсэн , 1 устгасан
  1. 0 1
      .gitignore
  2. 1 0
      .travis.yml
  3. 56 0
      frameworks/Ruby/hanami/.gitignore
  4. 4 0
      frameworks/Ruby/hanami/.hanamirc
  5. 15 0
      frameworks/Ruby/hanami/Gemfile
  6. 12 0
      frameworks/Ruby/hanami/Rakefile
  7. 327 0
      frameworks/Ruby/hanami/apps/web/application.rb
  8. 21 0
      frameworks/Ruby/hanami/apps/web/config/controller.rb
  9. 14 0
      frameworks/Ruby/hanami/apps/web/config/routes.rb
  10. 1 0
      frameworks/Ruby/hanami/apps/web/controllers/.gitkeep
  11. 9 0
      frameworks/Ruby/hanami/apps/web/controllers/hello_world/db.rb
  12. 13 0
      frameworks/Ruby/hanami/apps/web/controllers/hello_world/fortune.rb
  13. 9 0
      frameworks/Ruby/hanami/apps/web/controllers/hello_world/json.rb
  14. 10 0
      frameworks/Ruby/hanami/apps/web/controllers/hello_world/plaintext.rb
  15. 17 0
      frameworks/Ruby/hanami/apps/web/controllers/hello_world/query.rb
  16. 19 0
      frameworks/Ruby/hanami/apps/web/controllers/hello_world/update.rb
  17. 9 0
      frameworks/Ruby/hanami/apps/web/templates/application.html.erb
  18. 12 0
      frameworks/Ruby/hanami/apps/web/templates/hello_world/fortune.html.erb
  19. 7 0
      frameworks/Ruby/hanami/apps/web/views/application_layout.rb
  20. 5 0
      frameworks/Ruby/hanami/apps/web/views/hello_world/fortune.rb
  21. 74 0
      frameworks/Ruby/hanami/benchmark_config.json
  22. 3 0
      frameworks/Ruby/hanami/config.ru
  23. 2 0
      frameworks/Ruby/hanami/config/boot.rb
  24. 28 0
      frameworks/Ruby/hanami/config/environment.rb
  25. 1 0
      frameworks/Ruby/hanami/config/initializers/.gitkeep
  26. 158 0
      frameworks/Ruby/hanami/config/nginx.conf
  27. 10 0
      frameworks/Ruby/hanami/config/thin.yml
  28. 53 0
      frameworks/Ruby/hanami/config/unicorn.rb
  29. 2 0
      frameworks/Ruby/hanami/lib/hello_world.rb
  30. 1 0
      frameworks/Ruby/hanami/lib/hello_world/entities/.gitkeep
  31. 6 0
      frameworks/Ruby/hanami/lib/hello_world/entities/fortune.rb
  32. 2 0
      frameworks/Ruby/hanami/lib/hello_world/entities/world.rb
  33. 1 0
      frameworks/Ruby/hanami/lib/hello_world/repositories/.gitkeep
  34. 3 0
      frameworks/Ruby/hanami/lib/hello_world/repositories/fortune_repository.rb
  35. 7 0
      frameworks/Ruby/hanami/lib/hello_world/repositories/world_repository.rb
  36. 7 0
      frameworks/Ruby/hanami/run_mri_puma.sh
  37. 7 0
      frameworks/Ruby/hanami/run_mri_thin.sh
  38. 11 0
      frameworks/Ruby/hanami/run_mri_unicorn.sh

+ 0 - 1
.gitignore

@@ -4,7 +4,6 @@ installs
 node_modules/
 
 # Added for pedestal framework test
-lib/
 .lein-deps-sum
 
 # eclipse

+ 1 - 0
.travis.yml

@@ -168,6 +168,7 @@ env:
     - "TESTDIR=Python/wheezyweb"
     - "TESTDIR=Python/wsgi"
     - "TESTDIR=Ruby/grape"
+    - "TESTDIR=Ruby/hanami"
     - "TESTDIR=Ruby/ngx_mruby"
     - "TESTDIR=Ruby/padrino"
     - "TESTDIR=Ruby/rack"

+ 56 - 0
frameworks/Ruby/hanami/.gitignore

@@ -0,0 +1,56 @@
+/public/assets*
+/tmp
+
+# Except the .gitkeep
+!.gitkeep
+
+### Ruby ###
+*.gem
+*.rbc
+/.config
+/coverage/
+/InstalledFiles
+/pkg/
+/spec/reports/
+/spec/examples.txt
+/test/tmp/
+/test/version_tmp/
+/tmp/
+
+# Used by dotenv library to load environment variables.
+# .env
+
+## Specific to RubyMotion:
+.dat*
+.repl_history
+build/
+*.bridgesupport
+build-iPhoneOS/
+build-iPhoneSimulator/
+
+## Specific to RubyMotion (use of CocoaPods):
+#
+# We recommend against adding the Pods directory to your .gitignore. However
+# you should judge for yourself, the pros and cons are mentioned at:
+# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
+#
+# vendor/Pods/
+
+## Documentation cache and generated files:
+/.yardoc/
+/_yardoc/
+/doc/
+/rdoc/
+
+## Environment normalization:
+/.bundle/
+/vendor/bundle
+
+# for a library or gem, you might want to ignore these files since the code is
+# intended to run in multiple environments; otherwise, check them in:
+# Gemfile.lock
+# .ruby-version
+# .ruby-gemset
+
+# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
+.rvmrc

+ 4 - 0
frameworks/Ruby/hanami/.hanamirc

@@ -0,0 +1,4 @@
+project=hello_world
+architecture=container
+test=minitest
+template=erb

+ 15 - 0
frameworks/Ruby/hanami/Gemfile

@@ -0,0 +1,15 @@
+source 'https://rubygems.org'
+
+gem 'rake'
+gem 'hanami',       '~> 1.0'
+gem 'hanami-model', '~> 1.0'
+
+gem 'mysql2'
+
+gem 'rom-sql', git: 'https://github.com/rom-rb/rom-sql'
+
+group :production do
+  gem 'puma'
+  gem 'unicorn'
+  gem 'thin'
+end

+ 12 - 0
frameworks/Ruby/hanami/Rakefile

@@ -0,0 +1,12 @@
+require 'rake'
+require 'hanami/rake_tasks'
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+  t.pattern = 'spec/**/*_spec.rb'
+  t.libs    << 'spec'
+  t.warning = false
+end
+
+task default: :test
+task spec: :test

+ 327 - 0
frameworks/Ruby/hanami/apps/web/application.rb

@@ -0,0 +1,327 @@
+require 'hanami/helpers'
+require 'hanami/assets'
+require_relative 'config/controller'
+
+module Web
+  class Application < Hanami::Application
+    configure do
+      ##
+      # BASIC
+      #
+
+      # Define the root path of this application.
+      # All paths specified in this configuration are relative to path below.
+      #
+      root __dir__
+
+      # Relative load paths where this application will recursively load the
+      # code.
+      #
+      # When you add new directories, remember to add them here.
+      #
+      load_paths << [
+        'controllers',
+        'views'
+      ]
+
+      # Handle exceptions with HTTP statuses (true) or don't catch them (false).
+      # Defaults to true.
+      # See: http://www.rubydoc.info/gems/hanami-controller/#Exceptions_management
+      #
+      # handle_exceptions true
+
+      ##
+      # HTTP
+      #
+
+      # Routes definitions for this application
+      # See: http://www.rubydoc.info/gems/hanami-router#Usage
+      #
+      routes 'config/routes'
+
+      # URI scheme used by the routing system to generate absolute URLs
+      # Defaults to "http"
+      #
+      # scheme 'https'
+
+      # URI host used by the routing system to generate absolute URLs
+      # Defaults to "localhost"
+      #
+      # host 'example.org'
+
+      # URI port used by the routing system to generate absolute URLs
+      # Argument: An object coercible to integer, defaults to 80 if the scheme
+      # is http and 443 if it's https
+      #
+      # This should only be configured if app listens to non-standard ports
+      #
+      # port 443
+
+      # Enable cookies
+      # Argument: boolean to toggle the feature
+      #           A Hash with options
+      #
+      # Options:
+      #   :domain   - The domain (String - nil by default, not required)
+      #   :path     - Restrict cookies to a relative URI
+      #               (String - nil by default)
+      #   :max_age  - Cookies expiration expressed in seconds
+      #               (Integer - nil by default)
+      #   :secure   - Restrict cookies to secure connections
+      #               (Boolean - Automatically true when using HTTPS)
+      #               See #scheme and #ssl?
+      #   :httponly - Prevent JavaScript access (Boolean - true by default)
+      #
+      # cookies true
+      # or
+      # cookies max_age: 300
+
+      # Enable sessions
+      # Argument: Symbol the Rack session adapter
+      #           A Hash with options
+      #
+      # See: http://www.rubydoc.info/gems/rack/Rack/Session/Cookie
+      #
+      # sessions :cookie, secret: ENV['WEB_SESSIONS_SECRET']
+
+      # Configure Rack middleware for this application
+      #
+      # middleware.use Rack::Protection
+
+      # Default format for the requests that don't specify an HTTP_ACCEPT header
+      # Argument: A symbol representation of a mime type, defaults to :html
+      #
+      # default_request_format :html
+
+      # Default format for responses that don't consider the request format
+      # Argument: A symbol representation of a mime type, defaults to :html
+      #
+      default_response_format :json
+
+      # HTTP Body parsers
+      # Parse non GET responses body for a specific mime type
+      # Argument: Symbol, which represent the format of the mime type
+      #             (only `:json` is supported)
+      #           Object, the parser
+      #
+      # body_parsers :json
+
+      # When it's true and the router receives a non-encrypted request (http),
+      # it redirects to the secure equivalent (https). Disabled by default.
+      #
+      # force_ssl true
+
+      ##
+      # TEMPLATES
+      #
+
+      # The layout to be used by all views
+      #
+      layout :application # It will load Web::Views::ApplicationLayout
+
+      # The relative path to templates
+      #
+      templates 'templates'
+
+      ##
+      # ASSETS
+      #
+      assets do
+        # JavaScript compressor
+        #
+        # Supported engines:
+        #
+        #   * :builtin
+        #   * :uglifier
+        #   * :yui
+        #   * :closure
+        #
+        # See: http://hanamirb.org/guides/assets/compressors
+        #
+        # In order to skip JavaScript compression comment the following line
+        # javascript_compressor :builtin
+
+        # Stylesheet compressor
+        #
+        # Supported engines:
+        #
+        #   * :builtin
+        #   * :yui
+        #   * :sass
+        #
+        # See: http://hanamirb.org/guides/assets/compressors
+        #
+        # In order to skip stylesheet compression comment the following line
+        # stylesheet_compressor :builtin
+
+        # Specify sources for assets
+        #
+        sources << [
+          'assets'
+        ]
+      end
+
+      ##
+      # SECURITY
+      #
+
+      # X-Frame-Options is a HTTP header supported by modern browsers.
+      # It determines if a web page can or cannot be included via <frame> and
+      # <iframe> tags by untrusted domains.
+      #
+      # Web applications can send this header to prevent Clickjacking attacks.
+      #
+      # Read more at:
+      #
+      #   * https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options
+      #   * https://www.owasp.org/index.php/Clickjacking
+      #
+      security.x_frame_options 'DENY'
+
+      # X-Content-Type-Options prevents browsers from interpreting files as
+      # something else than declared by the content type in the HTTP headers.
+      #
+      # Read more at:
+      #
+      #   * https://www.owasp.org/index.php/OWASP_Secure_Headers_Project#X-Content-Type-Options
+      #   * https://msdn.microsoft.com/en-us/library/gg622941%28v=vs.85%29.aspx
+      #   * https://blogs.msdn.microsoft.com/ie/2008/09/02/ie8-security-part-vi-beta-2-update
+      #
+      security.x_content_type_options 'nosniff'
+
+      # X-XSS-Protection is a HTTP header to determine the behavior of the
+      # browser in case an XSS attack is detected.
+      #
+      # Read more at:
+      #
+      #   * https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)
+      #   * https://www.owasp.org/index.php/OWASP_Secure_Headers_Project#X-XSS-Protection
+      #
+      security.x_xss_protection '1; mode=block'
+
+      # Content-Security-Policy (CSP) is a HTTP header supported by modern
+      # browsers. It determines trusted sources of execution for dynamic
+      # contents (JavaScript) or other web related assets: stylesheets, images,
+      # fonts, plugins, etc.
+      #
+      # Web applications can send this header to mitigate Cross Site Scripting
+      # (XSS) attacks.
+      #
+      # The default value allows images, scripts, AJAX, fonts and CSS from the
+      # same origin, and does not allow any other resources to load (eg object,
+      # frame, media, etc).
+      #
+      # Inline JavaScript is NOT allowed. To enable it, please use:
+      # "script-src 'unsafe-inline'".
+      #
+      # Content Security Policy introduction:
+      #
+      #  * http://www.html5rocks.com/en/tutorials/security/content-security-policy/
+      #  * https://www.owasp.org/index.php/Content_Security_Policy
+      #  * https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29
+      #
+      # Inline and eval JavaScript risks:
+      #
+      #   * http://www.html5rocks.com/en/tutorials/security/content-security-policy/#inline-code-considered-harmful
+      #   * http://www.html5rocks.com/en/tutorials/security/content-security-policy/#eval-too
+      #
+      # Content Security Policy usage:
+      #
+      #  * http://content-security-policy.com/
+      #  * https://developer.mozilla.org/en-US/docs/Web/Security/CSP/Using_Content_Security_Policy
+      #
+      # Content Security Policy references:
+      #
+      #  * https://developer.mozilla.org/en-US/docs/Web/Security/CSP/CSP_policy_directives
+      #
+      security.content_security_policy %{
+        form-action 'self';
+        frame-ancestors 'self';
+        base-uri 'self';
+        default-src 'none';
+        script-src 'self';
+        connect-src 'self';
+        img-src 'self' https: data:;
+        style-src 'self' 'unsafe-inline' https:;
+        font-src 'self';
+        object-src 'none';
+        plugin-types application/pdf;
+        child-src 'self';
+        frame-src 'self';
+        media-src 'self'
+      }
+
+      ##
+      # FRAMEWORKS
+      #
+
+      # Configure the code that will yield each time Web::Action is included
+      # This is useful for sharing common functionality
+      #
+      # See: http://www.rubydoc.info/gems/hanami-controller#Configuration
+      controller.prepare do
+        # include MyAuthentication # included in all the actions
+        # before :authenticate!    # run an authentication before callback
+      end
+
+      # Configure the code that will yield each time Web::View is included
+      # This is useful for sharing common functionality
+      #
+      # See: http://www.rubydoc.info/gems/hanami-view#Configuration
+      view.prepare do
+        include Hanami::Helpers
+        include Web::Assets::Helpers
+      end
+    end
+
+    ##
+    # DEVELOPMENT
+    #
+    configure :development do
+      # Don't handle exceptions, render the stack trace
+      handle_exceptions false
+    end
+
+    ##
+    # TEST
+    #
+    configure :test do
+      # Don't handle exceptions, render the stack trace
+      handle_exceptions false
+    end
+
+    ##
+    # PRODUCTION
+    #
+    configure :production do
+      # scheme 'https'
+      # host   'example.org'
+      # port   443
+
+      assets do
+        # Don't compile static assets in production mode (eg. Sass, ES6)
+        #
+        # See: http://www.rubydoc.info/gems/hanami-assets#Configuration
+        compile false
+
+        # Use fingerprint file name for asset paths
+        #
+        # See: http://hanamirb.org/guides/assets/overview
+        fingerprint false
+
+        # Content Delivery Network (CDN)
+        #
+        # See: http://hanamirb.org/guides/assets/content-delivery-network
+        #
+        # scheme 'https'
+        # host   'cdn.example.org'
+        # port   443
+
+        # Subresource Integrity
+        #
+        # See: http://hanamirb.org/guides/assets/content-delivery-network/#subresource-integrity
+        subresource_integrity :sha256
+      end
+    end
+  end
+end

+ 21 - 0
frameworks/Ruby/hanami/apps/web/config/controller.rb

@@ -0,0 +1,21 @@
+require 'hanami/controller'
+
+SERVER_STRING =
+  if defined?(PhusionPassenger)
+    [
+      PhusionPassenger::SharedConstants::SERVER_TOKEN_NAME,
+      PhusionPassenger::VERSION_STRING
+    ].join('/').freeze
+  elsif defined?(Puma)
+    Puma::Const::PUMA_SERVER_STRING
+  elsif defined?(Unicorn)
+    Unicorn::HttpParser::DEFAULTS['SERVER_SOFTWARE']
+  end
+
+Hanami::Controller.configure do
+  default_charset('')
+  default_headers({
+    'Date'   => Time.now.httpdate,
+    'Server' => SERVER_STRING || 'WebServer'
+  })
+end

+ 14 - 0
frameworks/Ruby/hanami/apps/web/config/routes.rb

@@ -0,0 +1,14 @@
+# Configure your routes here
+# See: http://hanamirb.org/guides/routing/overview/
+#
+# Example:
+# get '/hello', to: ->(env) { [200, {}, ['Hello from Hanami!']] }
+get '/plaintext', to: 'hello_world#plaintext'
+get '/fortune',  to: 'hello_world#fortune'
+get '/update', to: 'hello_world#update'
+
+namespace 'hello_world' do
+  get '/json',    to: 'hello_world#json'
+  get '/db',      to: 'hello_world#db'
+  get '/query', to: 'hello_world#query'
+end

+ 1 - 0
frameworks/Ruby/hanami/apps/web/controllers/.gitkeep

@@ -0,0 +1 @@
+#

+ 9 - 0
frameworks/Ruby/hanami/apps/web/controllers/hello_world/db.rb

@@ -0,0 +1,9 @@
+module Web::Controllers::HelloWorld
+  class Db
+    include Web::Action
+
+    def call(params)
+      status 200, WorldRepository.new.find_random_entity.to_h.to_json
+    end
+  end
+end

+ 13 - 0
frameworks/Ruby/hanami/apps/web/controllers/hello_world/fortune.rb

@@ -0,0 +1,13 @@
+module Web::Controllers::HelloWorld
+  class Fortune
+    include Web::Action
+    expose :fortunes
+
+    def call(params)
+      self.headers.merge!({ 'Content-Type' => 'text/html; charset=utf-8' })
+      @fortunes = FortuneRepository.new.all
+      @fortunes << ::Fortune.new(id: 0, message: "Additional fortune added at request time.")
+      @fortunes = @fortunes.sort_by { |x| x.message }
+    end
+  end
+end

+ 9 - 0
frameworks/Ruby/hanami/apps/web/controllers/hello_world/json.rb

@@ -0,0 +1,9 @@
+module Web::Controllers::HelloWorld
+  class Json
+    include Web::Action
+
+    def call(params)
+      status 200, {:message => "Hello, World!"}.to_json
+    end
+  end
+end

+ 10 - 0
frameworks/Ruby/hanami/apps/web/controllers/hello_world/plaintext.rb

@@ -0,0 +1,10 @@
+module Web::Controllers::HelloWorld
+  class Plaintext
+    include Web::Action
+
+    def call(params)
+      self.format = :txt
+      status 200, 'Hello, World!'
+    end
+  end
+end

+ 17 - 0
frameworks/Ruby/hanami/apps/web/controllers/hello_world/query.rb

@@ -0,0 +1,17 @@
+module Web::Controllers::HelloWorld
+  class Query
+    include Web::Action
+
+    def call(params)
+      queries = params[:queries].to_i
+      queries = 1 if queries < 1
+      queries = 500 if queries > 500
+
+      repository = WorldRepository.new
+      results = (1..queries).map do
+        repository.find_random_entity.to_h
+      end
+      status 200, results.to_json
+    end
+  end
+end

+ 19 - 0
frameworks/Ruby/hanami/apps/web/controllers/hello_world/update.rb

@@ -0,0 +1,19 @@
+module Web::Controllers::HelloWorld
+  class Update
+    include Web::Action
+
+    def call(params)
+      queries = (params[:queries] || 1).to_i
+      queries = 1 if queries < 1
+      queries = 500 if queries > 500
+
+      repository = WorldRepository.new
+      worlds = (1..queries).map do
+        world = repository.find_random_entity
+        world = repository.update(world.id, randomNumber: Random.rand(10000) + 1)
+        world.to_h
+      end
+      status 200, worlds.to_json
+    end
+  end
+end

+ 9 - 0
frameworks/Ruby/hanami/apps/web/templates/application.html.erb

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>Fortunes</title>
+</head>
+<body>
+<%= yield %>
+</body>
+</html>

+ 12 - 0
frameworks/Ruby/hanami/apps/web/templates/hello_world/fortune.html.erb

@@ -0,0 +1,12 @@
+<table>
+<tr>
+<th>id</th>
+<th>message</th>
+</tr>
+<% fortunes.each do |fortune| %>
+<tr>
+<td><%= fortune.id %></td>
+<td><%= Rack::Utils.escape_html(fortune.message) %></td>
+</tr>
+<% end %>
+</table>

+ 7 - 0
frameworks/Ruby/hanami/apps/web/views/application_layout.rb

@@ -0,0 +1,7 @@
+module Web
+  module Views
+    class ApplicationLayout
+      include Web::Layout
+    end
+  end
+end

+ 5 - 0
frameworks/Ruby/hanami/apps/web/views/hello_world/fortune.rb

@@ -0,0 +1,5 @@
+module Web::Views::HelloWorld
+  class Fortune
+    include Web::View
+  end
+end

+ 74 - 0
frameworks/Ruby/hanami/benchmark_config.json

@@ -0,0 +1,74 @@
+{
+  "framework": "hanami",
+  "tests": [{
+    "default": {
+      "setup_file": "run_mri_puma",
+      "json_url": "/hello_world/json",
+      "db_url": "/hello_world/db",
+      "query_url": "/hello_world/query?queries=",
+      "fortune_url": "/fortune",
+      "update_url": "/update?queries=",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MySQL",
+      "framework": "hanami",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "Rack",
+      "webserver": "Puma",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "hanami-puma-mri",
+      "notes": "",
+      "versus": "rack-puma-mri"
+    },
+    "thin": {
+      "setup_file": "run_mri_thin",
+      "json_url": "/hello_world/json",
+      "db_url": "/hello_world/db",
+      "query_url": "/hello_world/query?queries=",
+      "fortune_url": "/fortune",
+      "update_url": "/update?queries=",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MySQL",
+      "framework": "hanami",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "Rack",
+      "webserver": "Thin",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "hanami-thin-mri",
+      "notes": "",
+      "versus": "rack-thin-mri"
+    },
+    "unicorn": {
+      "setup_file": "run_mri_unicorn",
+      "json_url": "/hello_world/json",
+      "db_url": "/hello_world/db",
+      "query_url": "/hello_world/query?queries=",
+      "fortune_url": "/fortune",
+      "update_url": "/update?queries=",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MySQL",
+      "framework": "hanami",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "Rack",
+      "webserver": "Unicorn",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "hanami-unicorn-mri",
+      "notes": "",
+      "versus": "rack-unicorn-mri"
+    }
+  }]
+}

+ 3 - 0
frameworks/Ruby/hanami/config.ru

@@ -0,0 +1,3 @@
+require './config/environment'
+
+run Hanami.app

+ 2 - 0
frameworks/Ruby/hanami/config/boot.rb

@@ -0,0 +1,2 @@
+require_relative './environment'
+Hanami.boot

+ 28 - 0
frameworks/Ruby/hanami/config/environment.rb

@@ -0,0 +1,28 @@
+require 'bundler/setup'
+require 'hanami/setup'
+require 'hanami/model'
+require_relative '../lib/hello_world'
+require_relative '../apps/web/application'
+
+Hanami.configure do
+  mount Web::Application, at: '/'
+
+  model do
+    ##
+    # Database adapter
+    #
+    # Available options:
+    #
+    #  * SQL adapter
+    #    adapter :sql, 'sqlite://db/hello_world_development.sqlite3'
+    #    adapter :sql, 'postgresql://localhost/hello_world_development'
+    #    adapter :sql, 'mysql://localhost/hello_world_development'
+    #
+
+    host = ENV['DB_HOST'] || 'localhost'
+    adapter :sql, "mysql2://benchmarkdbuser:benchmarkdbpass@#{host}/hello_world"
+  end
+
+  environment :production do
+  end
+end

+ 1 - 0
frameworks/Ruby/hanami/config/initializers/.gitkeep

@@ -0,0 +1 @@
+#

+ 158 - 0
frameworks/Ruby/hanami/config/nginx.conf

@@ -0,0 +1,158 @@
+# This is example contains the bare mininum to get nginx going with
+# Unicorn or Rainbows! servers.  Generally these configuration settings
+# are applicable to other HTTP application servers (and not just Ruby
+# ones), so if you have one working well for proxying another app
+# server, feel free to continue using it.
+#
+# The only setting we feel strongly about is the fail_timeout=0
+# directive in the "upstream" block.  max_fails=0 also has the same
+# effect as fail_timeout=0 for current versions of nginx and may be
+# used in its place.
+#
+# Users are strongly encouraged to refer to nginx documentation for more
+# details and search for other example configs.
+
+# you generally only need one nginx worker unless you're serving
+# large amounts of static files which require blocking disk reads
+worker_processes 8;
+
+# # drop privileges, root is needed on most systems for binding to port 80
+# # (or anything < 1024).  Capability-based security may be available for
+# # your system and worth checking out so you won't need to be root to
+# # start nginx to bind on 80
+# user nobody nogroup; # for systems with a "nogroup"
+#user nobody nobody; # for systems with "nobody" as a group instead
+
+# Feel free to change all paths to suite your needs here, of course
+pid /tmp/nginx.pid;
+#error_log /tmp/nginx.error.log;
+error_log stderr error;
+
+events {
+  worker_connections 4096; # increase if you have lots of clients
+  accept_mutex off; # "on" if nginx worker_processes > 1
+  use epoll; # enable for Linux 2.6+
+  # use kqueue; # enable for FreeBSD, OSX
+}
+
+http {
+  # nginx will find this file in the config directory set at nginx build time
+  include /usr/local/nginx/conf/mime.types;
+
+  # fallback in case we can't determine a type
+  default_type application/octet-stream;
+
+  # click tracking!
+  #access_log /tmp/nginx.access.log combined;
+  access_log off;
+
+  # you generally want to serve static files with nginx since neither
+  # Unicorn nor Rainbows! is optimized for it at the moment
+  sendfile on;
+
+  tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
+  tcp_nodelay off; # on may be better for some Comet/long-poll stuff
+
+  # we haven't checked to see if Rack::Deflate on the app server is
+  # faster or not than doing compression via nginx.  It's easier
+  # to configure it all in one place here for static files and also
+  # to disable gzip for clients who don't get gzip/deflate right.
+  # There are other gzip settings that may be needed used to deal with
+  # bad clients out there, see http://wiki.nginx.org/NginxHttpGzipModule
+  #gzip on;
+  #gzip_http_version 1.0;
+  #gzip_proxied any;
+  #gzip_min_length 500;
+  #gzip_disable "MSIE [1-6]\.";
+  #gzip_types text/plain text/html text/xml text/css
+  #           text/comma-separated-values
+  #           text/javascript application/x-javascript
+  #           application/atom+xml;
+
+  # this can be any application server, not just Unicorn/Rainbows!
+  upstream app_server {
+    # fail_timeout=0 means we always retry an upstream even if it failed
+    # to return a good HTTP response (in case the Unicorn master nukes a
+    # single worker for timing out).
+
+    # for UNIX domain socket setups:
+    server unix:/tmp/.sock fail_timeout=0;
+
+    # for TCP setups, point these to your backend servers
+    # server 192.168.0.7:8080 fail_timeout=0;
+    # server 192.168.0.8:8080 fail_timeout=0;
+    # server 192.168.0.9:8080 fail_timeout=0;
+  }
+
+  server {
+    # enable one of the following if you're on Linux or FreeBSD
+    listen 8080 default deferred; # for Linux
+    # listen 80 default accept_filter=httpready; # for FreeBSD
+
+    # If you have IPv6, you'll likely want to have two separate listeners.
+    # One on IPv4 only (the default), and another on IPv6 only instead
+    # of a single dual-stack listener.  A dual-stack listener will make
+    # for ugly IPv4 addresses in $remote_addr (e.g ":ffff:10.0.0.1"
+    # instead of just "10.0.0.1") and potentially trigger bugs in
+    # some software.
+    # listen [::]:80 ipv6only=on; # deferred or accept_filter recommended
+
+    client_max_body_size 4G;
+    server_name _;
+
+    # ~2 seconds is often enough for most folks to parse HTML/CSS and
+    # retrieve needed images/icons/frames, connections are cheap in
+    # nginx so increasing this is generally safe...
+    keepalive_timeout 10;
+
+    # path for static files
+    root /path/to/app/current/public;
+
+    # Prefer to serve static files directly from nginx to avoid unnecessary
+    # data copies from the application server.
+    #
+    # try_files directive appeared in in nginx 0.7.27 and has stabilized
+    # over time.  Older versions of nginx (e.g. 0.6.x) requires
+    # "if (!-f $request_filename)" which was less efficient:
+    # http://bogomips.org/unicorn.git/tree/examples/nginx.conf?id=v3.3.1#n127
+    try_files $uri/index.html $uri.html $uri @app;
+
+    location @app {
+      # an HTTP header important enough to have its own Wikipedia entry:
+      #   http://en.wikipedia.org/wiki/X-Forwarded-For
+      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+
+      # enable this if you forward HTTPS traffic to unicorn,
+      # this helps Rack set the proper URL scheme for doing redirects:
+      # proxy_set_header X-Forwarded-Proto $scheme;
+
+      # pass the Host: header from the client right along so redirects
+      # can be set properly within the Rack application
+      proxy_set_header Host $http_host;
+
+      # we don't want nginx trying to do something clever with
+      # redirects, we set the Host: header above already.
+      proxy_redirect off;
+
+      # set "proxy_buffering off" *only* for Rainbows! when doing
+      # Comet/long-poll/streaming.  It's also safe to set if you're using
+      # only serving fast clients with Unicorn + nginx, but not slow
+      # clients.  You normally want nginx to buffer responses to slow
+      # clients, even with Rails 3.1 streaming because otherwise a slow
+      # client can become a bottleneck of Unicorn.
+      #
+      # The Rack application may also set "X-Accel-Buffering (yes|no)"
+      # in the response headers do disable/enable buffering on a
+      # per-response basis.
+      # proxy_buffering off;
+
+      proxy_pass http://app_server;
+    }
+
+    # Rails error pages
+    error_page 500 502 503 504 /500.html;
+    location = /500.html {
+      root /path/to/app/current/public;
+    }
+  }
+}

+ 10 - 0
frameworks/Ruby/hanami/config/thin.yml

@@ -0,0 +1,10 @@
+--- 
+timeout: 30
+wait: 30
+max_conns: 1024
+max_persistent_conns: 512
+environment: production
+port: 8080
+servers: 8
+log: /tmp/thin.log
+quiet: true

+ 53 - 0
frameworks/Ruby/hanami/config/unicorn.rb

@@ -0,0 +1,53 @@
+worker_processes 8
+listen "/tmp/.sock", :backlog => 256
+
+preload_app true
+GC.respond_to?(:copy_on_write_friendly=) and
+  GC.copy_on_write_friendly = true
+
+  before_fork do |server, worker|
+    # the following is highly recomended for Rails + "preload_app true"
+    # as there's no need for the master process to hold a connection
+    defined?(ActiveRecord::Base) and
+      ActiveRecord::Base.connection.disconnect!
+
+    # The following is only recommended for memory/DB-constrained
+    # installations.  It is not needed if your system can house
+    # twice as many worker_processes as you have configured.
+    #
+    # # This allows a new master process to incrementally
+    # # phase out the old master process with SIGTTOU to avoid a
+    # # thundering herd (especially in the "preload_app false" case)
+    # # when doing a transparent upgrade.  The last worker spawned
+    # # will then kill off the old master process with a SIGQUIT.
+    # old_pid = "#{server.config[:pid]}.oldbin"
+    # if old_pid != server.pid
+    #   begin
+    #     sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
+    #     Process.kill(sig, File.read(old_pid).to_i)
+    #   rescue Errno::ENOENT, Errno::ESRCH
+    #   end
+    # end
+    #
+    # Throttle the master from forking too quickly by sleeping.  Due
+    # to the implementation of standard Unix signal handlers, this
+    # helps (but does not completely) prevent identical, repeated signals
+    # from being lost when the receiving process is busy.
+    # sleep 1
+  end
+
+  after_fork do |server, worker|
+    # per-process listener ports for debugging/admin/migrations
+    # addr = "127.0.0.1:#{9293 + worker.nr}"
+    # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
+
+    # the following is *required* for Rails + "preload_app true",
+    defined?(ActiveRecord::Base) and
+      ActiveRecord::Base.establish_connection
+
+    # if preload_app is true, then you may also want to check and
+    # restart any other shared sockets/descriptors such as Memcached,
+    # and Redis.  TokyoCabinet file handles are safe to reuse
+    # between any number of forked children (assuming your kernel
+    # correctly implements pread()/pwrite() system calls)
+  end

+ 2 - 0
frameworks/Ruby/hanami/lib/hello_world.rb

@@ -0,0 +1,2 @@
+module HelloWorld
+end

+ 1 - 0
frameworks/Ruby/hanami/lib/hello_world/entities/.gitkeep

@@ -0,0 +1 @@
+#

+ 6 - 0
frameworks/Ruby/hanami/lib/hello_world/entities/fortune.rb

@@ -0,0 +1,6 @@
+class Fortune < Hanami::Entity
+  attributes do
+    attribute :id,      Types::Int
+    attribute :message, Types::String
+  end
+end

+ 2 - 0
frameworks/Ruby/hanami/lib/hello_world/entities/world.rb

@@ -0,0 +1,2 @@
+class World < Hanami::Entity
+end

+ 1 - 0
frameworks/Ruby/hanami/lib/hello_world/repositories/.gitkeep

@@ -0,0 +1 @@
+#

+ 3 - 0
frameworks/Ruby/hanami/lib/hello_world/repositories/fortune_repository.rb

@@ -0,0 +1,3 @@
+class FortuneRepository < Hanami::Repository
+  self.relation = :fortune
+end

+ 7 - 0
frameworks/Ruby/hanami/lib/hello_world/repositories/world_repository.rb

@@ -0,0 +1,7 @@
+class WorldRepository < Hanami::Repository
+  self.relation = :world
+
+  def find_random_entity
+    find(Random.rand(10000) + 1)
+  end
+end

+ 7 - 0
frameworks/Ruby/hanami/run_mri_puma.sh

@@ -0,0 +1,7 @@
+#!/bin/bash
+
+fw_depends mysql rvm ruby-2.4
+
+rvm ruby-$MRI_VERSION do bundle install --jobs=4 --gemfile=$TROOT/Gemfile --path=vendor/bundle
+
+DB_HOST=${DBHOST} rvm ruby-$MRI_VERSION do bundle exec puma -t 8:32 -w 8 --preload -b tcp://0.0.0.0:8080 -e production &

+ 7 - 0
frameworks/Ruby/hanami/run_mri_thin.sh

@@ -0,0 +1,7 @@
+#!/bin/bash
+
+fw_depends mysql rvm ruby-2.4
+
+rvm ruby-$MRI_VERSION do bundle install --jobs=4 --gemfile=$TROOT/Gemfile --path=vendor/bundle
+
+DB_HOST=${DBHOST} rvm ruby-$MRI_VERSION do bundle exec thin start -C config/thin.yml &

+ 11 - 0
frameworks/Ruby/hanami/run_mri_unicorn.sh

@@ -0,0 +1,11 @@
+#!/bin/bash
+
+fw_depends mysql rvm ruby-2.4 nginx
+
+sed -i 's|/usr/local/nginx/|'"${IROOT}"'/nginx/|g' config/nginx.conf
+
+rvm ruby-$MRI_VERSION do bundle install --jobs=4 --gemfile=$TROOT/Gemfile --path=vendor/bundle
+
+nginx -c $TROOT/config/nginx.conf
+
+DB_HOST=${DBHOST} rvm ruby-$MRI_VERSION do bundle exec unicorn_rails -E production -c $TROOT/config/unicorn.rb &