hanami 0.0.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +214 -0
- data/FEATURES.md +156 -0
- data/LICENSE.md +22 -0
- data/README.md +80 -15
- data/bin/hanami +5 -0
- data/hanami.gemspec +27 -12
- data/lib/hanami.rb +78 -2
- data/lib/hanami/action/csrf_protection.rb +167 -0
- data/lib/hanami/action/routing_helpers.rb +40 -0
- data/lib/hanami/application.rb +244 -0
- data/lib/hanami/application_name.rb +101 -0
- data/lib/hanami/cli.rb +119 -0
- data/lib/hanami/cli_sub_commands/assets.rb +29 -0
- data/lib/hanami/cli_sub_commands/db.rb +124 -0
- data/lib/hanami/cli_sub_commands/destroy.rb +102 -0
- data/lib/hanami/cli_sub_commands/generate.rb +127 -0
- data/lib/hanami/commands/assets/precompile.rb +35 -0
- data/lib/hanami/commands/console.rb +90 -0
- data/lib/hanami/commands/db/abstract.rb +19 -0
- data/lib/hanami/commands/db/apply.rb +14 -0
- data/lib/hanami/commands/db/console.rb +50 -0
- data/lib/hanami/commands/db/create.rb +14 -0
- data/lib/hanami/commands/db/drop.rb +14 -0
- data/lib/hanami/commands/db/migrate.rb +19 -0
- data/lib/hanami/commands/db/prepare.rb +14 -0
- data/lib/hanami/commands/db/version.rb +14 -0
- data/lib/hanami/commands/generate/abstract.rb +63 -0
- data/lib/hanami/commands/generate/action.rb +262 -0
- data/lib/hanami/commands/generate/app.rb +116 -0
- data/lib/hanami/commands/generate/mailer.rb +118 -0
- data/lib/hanami/commands/generate/migration.rb +63 -0
- data/lib/hanami/commands/generate/model.rb +96 -0
- data/lib/hanami/commands/new/abstract.rb +128 -0
- data/lib/hanami/commands/new/app.rb +116 -0
- data/lib/hanami/commands/new/container.rb +102 -0
- data/lib/hanami/commands/routes.rb +41 -0
- data/lib/hanami/commands/server.rb +79 -0
- data/lib/hanami/config/configure.rb +17 -0
- data/lib/hanami/config/cookies.rb +68 -0
- data/lib/hanami/config/framework_configuration.rb +42 -0
- data/lib/hanami/config/load_paths.rb +27 -0
- data/lib/hanami/config/mapper.rb +36 -0
- data/lib/hanami/config/mapping.rb +12 -0
- data/lib/hanami/config/routes.rb +16 -0
- data/lib/hanami/config/security.rb +58 -0
- data/lib/hanami/config/sessions.rb +97 -0
- data/lib/hanami/configuration.rb +1728 -0
- data/lib/hanami/container.rb +59 -0
- data/lib/hanami/environment.rb +485 -0
- data/lib/hanami/frameworks.rb +14 -0
- data/lib/hanami/generators/action/action.rb.tt +8 -0
- data/lib/hanami/generators/action/action_spec.minitest.tt +12 -0
- data/lib/hanami/generators/action/action_spec.rspec.tt +11 -0
- data/lib/hanami/generators/action/action_without_view.rb.tt +9 -0
- data/lib/hanami/generators/action/template.tt +0 -0
- data/lib/hanami/generators/action/view.rb.tt +5 -0
- data/lib/hanami/generators/action/view_spec.minitest.tt +13 -0
- data/lib/hanami/generators/action/view_spec.rspec.tt +12 -0
- data/lib/hanami/generators/app/.gitkeep.tt +1 -0
- data/lib/hanami/generators/app/application.rb.tt +273 -0
- data/lib/hanami/generators/app/config/initializers/.gitkeep +0 -0
- data/lib/hanami/generators/app/config/routes.rb.tt +2 -0
- data/lib/hanami/generators/app/favicon.ico +0 -0
- data/lib/hanami/generators/app/templates/application.html.erb.tt +10 -0
- data/lib/hanami/generators/app/views/application_layout.rb.tt +7 -0
- data/lib/hanami/generators/application/app/.env.development.tt +4 -0
- data/lib/hanami/generators/application/app/.env.test.tt +4 -0
- data/lib/hanami/generators/application/app/.env.tt +1 -0
- data/lib/hanami/generators/application/app/.gitignore +0 -0
- data/lib/hanami/generators/application/app/.gitkeep +1 -0
- data/lib/hanami/generators/application/app/Gemfile.tt +37 -0
- data/lib/hanami/generators/application/app/Rakefile.minitest.tt +11 -0
- data/lib/hanami/generators/application/app/Rakefile.rspec.tt +6 -0
- data/lib/hanami/generators/application/app/apps/.gitkeep.tt +1 -0
- data/lib/hanami/generators/application/app/capybara.rb.rspec.tt +8 -0
- data/lib/hanami/generators/application/app/config.ru.tt +3 -0
- data/lib/hanami/generators/application/app/config/application.rb.tt +270 -0
- data/lib/hanami/generators/application/app/config/environment.rb.tt +5 -0
- data/lib/hanami/generators/application/app/config/initializers/.gitkeep +0 -0
- data/lib/hanami/generators/application/app/config/routes.rb.tt +2 -0
- data/lib/hanami/generators/application/app/db/.gitkeep +1 -0
- data/lib/hanami/generators/application/app/favicon.ico +0 -0
- data/lib/hanami/generators/application/app/features_helper.rb.minitest.tt +11 -0
- data/lib/hanami/generators/application/app/features_helper.rb.rspec.tt +12 -0
- data/lib/hanami/generators/application/app/gitignore.tt +2 -0
- data/lib/hanami/generators/application/app/gitignore_with_db.tt +4 -0
- data/lib/hanami/generators/application/app/hanamirc.tt +3 -0
- data/lib/hanami/generators/application/app/lib/app_name.rb.tt +59 -0
- data/lib/hanami/generators/application/app/lib/chirp/entities/.gitkeep +1 -0
- data/lib/hanami/generators/application/app/lib/chirp/repositories/.gitkeep +1 -0
- data/lib/hanami/generators/application/app/lib/config/mapping.rb.tt +7 -0
- data/lib/hanami/generators/application/app/rspec.rspec.tt +2 -0
- data/lib/hanami/generators/application/app/schema.sql.tt +0 -0
- data/lib/hanami/generators/application/app/spec_helper.rb.minitest.tt +7 -0
- data/lib/hanami/generators/application/app/spec_helper.rb.rspec.tt +104 -0
- data/lib/hanami/generators/application/app/templates/application.html.erb.tt +10 -0
- data/lib/hanami/generators/application/app/views/application_layout.rb.tt +7 -0
- data/lib/hanami/generators/application/container/.env.development.tt +3 -0
- data/lib/hanami/generators/application/container/.env.test.tt +3 -0
- data/lib/hanami/generators/application/container/.env.tt +1 -0
- data/lib/hanami/generators/application/container/.gitignore +0 -0
- data/lib/hanami/generators/application/container/.gitkeep +1 -0
- data/lib/hanami/generators/application/container/Gemfile.tt +36 -0
- data/lib/hanami/generators/application/container/Rakefile.minitest.tt +11 -0
- data/lib/hanami/generators/application/container/Rakefile.rspec.tt +6 -0
- data/lib/hanami/generators/application/container/capybara.rb.rspec.tt +8 -0
- data/lib/hanami/generators/application/container/config.ru.tt +3 -0
- data/lib/hanami/generators/application/container/config/environment.rb.tt +7 -0
- data/lib/hanami/generators/application/container/config/initializers/.gitkeep +0 -0
- data/lib/hanami/generators/application/container/db/.gitkeep +1 -0
- data/lib/hanami/generators/application/container/features_helper.rb.minitest.tt +11 -0
- data/lib/hanami/generators/application/container/features_helper.rb.rspec.tt +12 -0
- data/lib/hanami/generators/application/container/gitignore.tt +2 -0
- data/lib/hanami/generators/application/container/gitignore_with_db.tt +4 -0
- data/lib/hanami/generators/application/container/hanamirc.tt +3 -0
- data/lib/hanami/generators/application/container/lib/app_name.rb.tt +60 -0
- data/lib/hanami/generators/application/container/lib/chirp/entities/.gitkeep +1 -0
- data/lib/hanami/generators/application/container/lib/chirp/mailers/.gitkeep +0 -0
- data/lib/hanami/generators/application/container/lib/chirp/mailers/templates/.gitkeep +0 -0
- data/lib/hanami/generators/application/container/lib/chirp/repositories/.gitkeep +1 -0
- data/lib/hanami/generators/application/container/lib/config/mapping.rb.tt +7 -0
- data/lib/hanami/generators/application/container/rspec.rspec.tt +2 -0
- data/lib/hanami/generators/application/container/schema.sql.tt +0 -0
- data/lib/hanami/generators/application/container/spec_helper.rb.minitest.tt +7 -0
- data/lib/hanami/generators/application/container/spec_helper.rb.rspec.tt +104 -0
- data/lib/hanami/generators/database_config.rb +99 -0
- data/lib/hanami/generators/generatable.rb +51 -0
- data/lib/hanami/generators/generator.rb +35 -0
- data/lib/hanami/generators/mailer/mailer.rb.tt +7 -0
- data/lib/hanami/generators/mailer/mailer_spec.rb.tt +7 -0
- data/lib/hanami/generators/mailer/template.html.tt +0 -0
- data/lib/hanami/generators/mailer/template.txt.tt +0 -0
- data/lib/hanami/generators/migration/migration.rb.tt +4 -0
- data/lib/hanami/generators/model/entity.rb.tt +3 -0
- data/lib/hanami/generators/model/entity_spec.minitest.tt +5 -0
- data/lib/hanami/generators/model/entity_spec.rspec.tt +3 -0
- data/lib/hanami/generators/model/repository.rb.tt +3 -0
- data/lib/hanami/generators/model/repository_spec.minitest.tt +5 -0
- data/lib/hanami/generators/model/repository_spec.rspec.tt +3 -0
- data/lib/hanami/generators/test_framework.rb +42 -0
- data/lib/hanami/hanamirc.rb +152 -0
- data/lib/hanami/loader.rb +258 -0
- data/lib/hanami/mailer/glue.rb +68 -0
- data/lib/hanami/middleware.rb +143 -0
- data/lib/hanami/rake_helper.rb +68 -0
- data/lib/hanami/rake_tasks.rb +2 -0
- data/lib/hanami/rendering_policy.rb +77 -0
- data/lib/hanami/repositories/car_repository.rb +3 -0
- data/lib/hanami/repositories/name_repository.rb +3 -0
- data/lib/hanami/root.rb +7 -0
- data/lib/hanami/routes.rb +151 -0
- data/lib/hanami/routing/default.rb +25 -0
- data/lib/hanami/setup.rb +3 -0
- data/lib/hanami/static.rb +77 -0
- data/lib/hanami/templates/default.html.erb +9 -0
- data/lib/hanami/templates/welcome.html.erb +52 -0
- data/lib/hanami/version.rb +4 -1
- data/lib/hanami/views/default.rb +34 -0
- data/lib/hanami/views/default_template_finder.rb +20 -0
- data/lib/hanami/views/null_view.rb +17 -0
- data/lib/hanami/welcome.rb +40 -0
- metadata +357 -16
- data/.gitignore +0 -9
- data/Gemfile +0 -4
- data/Rakefile +0 -2
- data/bin/console +0 -14
- data/bin/setup +0 -8
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module Hanami
|
|
2
|
+
module Commands
|
|
3
|
+
# Display application/container routes.
|
|
4
|
+
#
|
|
5
|
+
# It is run with:
|
|
6
|
+
#
|
|
7
|
+
# `bundle exec hanami routes`
|
|
8
|
+
#
|
|
9
|
+
# @since 0.1.0
|
|
10
|
+
# @api private
|
|
11
|
+
class Routes
|
|
12
|
+
# @param options [Hash] Environment's options
|
|
13
|
+
#
|
|
14
|
+
# @since 0.1.0
|
|
15
|
+
# @see Hanami::Environment#initialize
|
|
16
|
+
def initialize(options)
|
|
17
|
+
@environment = Hanami::Environment.new(options)
|
|
18
|
+
@environment.require_application_environment
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Display to STDOUT application routes
|
|
22
|
+
#
|
|
23
|
+
# @since 0.1.0
|
|
24
|
+
def start
|
|
25
|
+
puts app.routes.inspector.to_s
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
# @since 0.1.0
|
|
31
|
+
# @api private
|
|
32
|
+
def app
|
|
33
|
+
if @environment.container?
|
|
34
|
+
Hanami::Container.new
|
|
35
|
+
else
|
|
36
|
+
Hanami::Application.applications.first.new
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
require 'rack'
|
|
2
|
+
|
|
3
|
+
module Hanami
|
|
4
|
+
module Commands
|
|
5
|
+
# Rack compatible server.
|
|
6
|
+
#
|
|
7
|
+
# It is run with:
|
|
8
|
+
#
|
|
9
|
+
# `bundle exec hanami server`
|
|
10
|
+
#
|
|
11
|
+
# It runs the application, by using the server specified in your `Gemfile`
|
|
12
|
+
# (eg. Puma or Unicorn).
|
|
13
|
+
#
|
|
14
|
+
# It enables code reloading by default.
|
|
15
|
+
# This feature is implemented via process fork and requires `shotgun` gem.
|
|
16
|
+
#
|
|
17
|
+
# @since 0.1.0
|
|
18
|
+
# @api private
|
|
19
|
+
class Server < ::Rack::Server
|
|
20
|
+
attr_reader :options
|
|
21
|
+
|
|
22
|
+
# @param options [Hash] Environment's options
|
|
23
|
+
#
|
|
24
|
+
# @since 0.1.0
|
|
25
|
+
# @see Hanami::Environment#initialize
|
|
26
|
+
def initialize(options)
|
|
27
|
+
@_env = Hanami::Environment.new(options)
|
|
28
|
+
@options = _extract_options(@_env)
|
|
29
|
+
|
|
30
|
+
if code_reloading?
|
|
31
|
+
require 'shotgun'
|
|
32
|
+
@app = Shotgun::Loader.new(@_env.rackup.to_s)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Primarily this removes the ::Rack::Chunked middleware
|
|
37
|
+
# which is the cause of Safari content-length bugs.
|
|
38
|
+
#
|
|
39
|
+
# @since 0.1.0
|
|
40
|
+
def middleware
|
|
41
|
+
mw = Hash.new { |e, m| e[m] = [] }
|
|
42
|
+
mw["deployment"].concat([::Rack::ContentLength, ::Rack::CommonLogger])
|
|
43
|
+
mw["development"].concat(mw["deployment"] + [::Rack::ShowExceptions, ::Rack::Lint])
|
|
44
|
+
mw
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Kickstart shotgun preloader if code reloading is supported
|
|
48
|
+
#
|
|
49
|
+
# @since 0.1.0
|
|
50
|
+
def start
|
|
51
|
+
if code_reloading?
|
|
52
|
+
Shotgun.enable_copy_on_write
|
|
53
|
+
Shotgun.preload
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
super
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
# @since 0.1.0
|
|
62
|
+
# @api private
|
|
63
|
+
def _extract_options(env)
|
|
64
|
+
env.to_options.merge(
|
|
65
|
+
config: env.rackup.to_s,
|
|
66
|
+
Host: env.host,
|
|
67
|
+
Port: env.port,
|
|
68
|
+
AccessLog: []
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# @since 0.1.0
|
|
73
|
+
# @api private
|
|
74
|
+
def code_reloading?
|
|
75
|
+
@_env.code_reloading?
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'hanami/config/mapper'
|
|
2
|
+
|
|
3
|
+
module Hanami
|
|
4
|
+
module Config
|
|
5
|
+
# Define configuration of application of
|
|
6
|
+
# a specific environment
|
|
7
|
+
#
|
|
8
|
+
# @since 0.2.0
|
|
9
|
+
# @api private
|
|
10
|
+
class Configure < Mapper
|
|
11
|
+
private
|
|
12
|
+
def error_message
|
|
13
|
+
'You must specify a block or a file for configuration definition'
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
module Hanami
|
|
2
|
+
module Config
|
|
3
|
+
# Cookies configuration
|
|
4
|
+
#
|
|
5
|
+
# @since 0.3.0
|
|
6
|
+
# @api private
|
|
7
|
+
class Cookies
|
|
8
|
+
|
|
9
|
+
# Return the routes for this application
|
|
10
|
+
#
|
|
11
|
+
# @return [Hash] options for cookies
|
|
12
|
+
#
|
|
13
|
+
# @since 0.3.0
|
|
14
|
+
# @api private
|
|
15
|
+
attr_reader :default_options
|
|
16
|
+
|
|
17
|
+
# Cookies configuration
|
|
18
|
+
#
|
|
19
|
+
# httponly option enabled by default.
|
|
20
|
+
# Prevent attackers to steal cookies via JavaScript,
|
|
21
|
+
# Eg. alert(document.cookie) will fail
|
|
22
|
+
#
|
|
23
|
+
# @param options [Hash, TrueClass, FalseClass] optional cookies options
|
|
24
|
+
# @param configuration [Hanami::Configuration] the application configuration
|
|
25
|
+
#
|
|
26
|
+
# @since 0.3.0
|
|
27
|
+
# @api private
|
|
28
|
+
#
|
|
29
|
+
# @see https://github.com/rack/rack/blob/master/lib/rack/utils.rb #set_cookie_header!
|
|
30
|
+
# @see https://www.owasp.org/index.php/HttpOnly
|
|
31
|
+
#
|
|
32
|
+
# @example Enable cookies with boolean
|
|
33
|
+
# module Web
|
|
34
|
+
# class Application < Hanami::Application
|
|
35
|
+
# configure do
|
|
36
|
+
# # ...
|
|
37
|
+
# cookies true
|
|
38
|
+
# end
|
|
39
|
+
# end
|
|
40
|
+
# end
|
|
41
|
+
#
|
|
42
|
+
# @example Enable cookies with options
|
|
43
|
+
# module Web
|
|
44
|
+
# class Application < Hanami::Application
|
|
45
|
+
# configure do
|
|
46
|
+
# # ...
|
|
47
|
+
# cookies max_age: 300
|
|
48
|
+
# end
|
|
49
|
+
# end
|
|
50
|
+
# end
|
|
51
|
+
def initialize(configuration, options = {})
|
|
52
|
+
@options = options
|
|
53
|
+
@default_options = { httponly: true, secure: configuration.ssl? }
|
|
54
|
+
@default_options.merge!(options) if options.is_a?(::Hash)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Return if cookies are enabled
|
|
58
|
+
#
|
|
59
|
+
# @return [TrueClass, FalseClass] enabled cookies
|
|
60
|
+
#
|
|
61
|
+
# @since 0.3.0
|
|
62
|
+
# @api private
|
|
63
|
+
def enabled?
|
|
64
|
+
@options.respond_to?(:empty?) ? !@options.empty? : !!@options
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module Hanami
|
|
2
|
+
module Config
|
|
3
|
+
# Collects all the settings for a given framework configuration and then
|
|
4
|
+
# forwards them when the application is loaded.
|
|
5
|
+
#
|
|
6
|
+
# @since 0.2.0
|
|
7
|
+
# @api private
|
|
8
|
+
class FrameworkConfiguration < BasicObject
|
|
9
|
+
# @since 0.2.0
|
|
10
|
+
# @api private
|
|
11
|
+
def initialize(&blk)
|
|
12
|
+
@blocks = [blk || ::Proc.new { }]
|
|
13
|
+
@settings = []
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# @since 0.2.0
|
|
17
|
+
# @api private
|
|
18
|
+
def __apply(configuration)
|
|
19
|
+
@blocks.compact.each do |blk|
|
|
20
|
+
configuration.instance_eval(&blk)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
@settings.each do |(m, args, blk)|
|
|
24
|
+
configuration.public_send(m, *args, &blk)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @since 0.6.0
|
|
29
|
+
# @api private
|
|
30
|
+
def __add(&blk)
|
|
31
|
+
@blocks << blk
|
|
32
|
+
self
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @since 0.2.0
|
|
36
|
+
# @api private
|
|
37
|
+
def method_missing(m, *args, &blk)
|
|
38
|
+
@settings.push([m, args, blk])
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'hanami/utils/load_paths'
|
|
2
|
+
|
|
3
|
+
module Hanami
|
|
4
|
+
module Config
|
|
5
|
+
# Define the load paths where the application should load
|
|
6
|
+
#
|
|
7
|
+
# @since 0.1.0
|
|
8
|
+
# @api private
|
|
9
|
+
class LoadPaths < Utils::LoadPaths
|
|
10
|
+
PATTERN = '**/*.rb'.freeze
|
|
11
|
+
|
|
12
|
+
def load!(root)
|
|
13
|
+
@root = root
|
|
14
|
+
|
|
15
|
+
each do |path|
|
|
16
|
+
Dir.glob(path.join(PATTERN)).each { |file| require file }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
protected
|
|
21
|
+
def realpath(path)
|
|
22
|
+
@root.join(path).realpath
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'hanami/utils/kernel'
|
|
2
|
+
|
|
3
|
+
module Hanami
|
|
4
|
+
module Config
|
|
5
|
+
# Define a mapping for Hanami::Model
|
|
6
|
+
#
|
|
7
|
+
# @since 0.1.0
|
|
8
|
+
# @api private
|
|
9
|
+
class Mapper
|
|
10
|
+
EXTNAME = '.rb'
|
|
11
|
+
|
|
12
|
+
def initialize(root, path, &blk)
|
|
13
|
+
@path, @blk = path, blk
|
|
14
|
+
@path = root.join(path) if root && path
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def to_proc
|
|
18
|
+
return @blk if @blk
|
|
19
|
+
|
|
20
|
+
code = realpath.read
|
|
21
|
+
Proc.new { eval(code) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
def realpath
|
|
26
|
+
Utils::Kernel.Pathname("#{ @path }#{ EXTNAME }").realpath
|
|
27
|
+
rescue Errno::ENOENT
|
|
28
|
+
raise ArgumentError, error_message
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def error_message
|
|
32
|
+
'You must specify a block or a file.'
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'hanami/config/mapper'
|
|
2
|
+
|
|
3
|
+
module Hanami
|
|
4
|
+
module Config
|
|
5
|
+
# Defines a route set
|
|
6
|
+
#
|
|
7
|
+
# @since 0.1.0
|
|
8
|
+
# @api private
|
|
9
|
+
class Routes < Mapper
|
|
10
|
+
private
|
|
11
|
+
def error_message
|
|
12
|
+
'You must specify a block or a file for routes definitions.'
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module Hanami
|
|
2
|
+
module Config
|
|
3
|
+
# Security policies are stored here.
|
|
4
|
+
#
|
|
5
|
+
# @since 0.3.0
|
|
6
|
+
class Security
|
|
7
|
+
# @since 0.3.0
|
|
8
|
+
# @api private
|
|
9
|
+
#
|
|
10
|
+
# @see Hanami::Loader#_configure_controller_framework!
|
|
11
|
+
X_FRAME_OPTIONS_HEADER = 'X-Frame-Options'.freeze
|
|
12
|
+
|
|
13
|
+
# @since 0.3.0
|
|
14
|
+
# @api private
|
|
15
|
+
#
|
|
16
|
+
# @see Hanami::Loader#_configure_controller_framework!
|
|
17
|
+
CONTENT_SECURITY_POLICY_HEADER = 'Content-Security-Policy'.freeze
|
|
18
|
+
|
|
19
|
+
# X-Frame-Options headers' value
|
|
20
|
+
#
|
|
21
|
+
# @overload x_frame_options(value)
|
|
22
|
+
# Sets the given value
|
|
23
|
+
# @param value [String] for X-Frame-Options header.
|
|
24
|
+
#
|
|
25
|
+
# @overload x_frame_options
|
|
26
|
+
# Gets the value
|
|
27
|
+
# @return [String] X-Frame-Options header's value
|
|
28
|
+
#
|
|
29
|
+
# @since 0.3.0
|
|
30
|
+
def x_frame_options(value = nil)
|
|
31
|
+
if value.nil?
|
|
32
|
+
@x_frame_options
|
|
33
|
+
else
|
|
34
|
+
@x_frame_options = value
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Content-Policy-Security headers' value
|
|
39
|
+
#
|
|
40
|
+
# @overload content_security_policy(value)
|
|
41
|
+
# Sets the given value
|
|
42
|
+
# @param value [String] for Content-Security-Policy header.
|
|
43
|
+
#
|
|
44
|
+
# @overload content_security_policy
|
|
45
|
+
# Gets the value
|
|
46
|
+
# @return [String] Content-Security-Policy header's value
|
|
47
|
+
#
|
|
48
|
+
# @since 0.3.0
|
|
49
|
+
def content_security_policy(value = nil)
|
|
50
|
+
if value.nil?
|
|
51
|
+
@content_security_policy
|
|
52
|
+
else
|
|
53
|
+
@content_security_policy = value
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
require 'ipaddr'
|
|
2
|
+
require 'hanami/utils/string'
|
|
3
|
+
|
|
4
|
+
module Hanami
|
|
5
|
+
module Config
|
|
6
|
+
# Sessions configuration
|
|
7
|
+
#
|
|
8
|
+
# @since 0.2.0
|
|
9
|
+
# @api private
|
|
10
|
+
class Sessions
|
|
11
|
+
|
|
12
|
+
# Ruby namespace for Rack session adapters
|
|
13
|
+
#
|
|
14
|
+
# @since 0.2.0
|
|
15
|
+
# @api private
|
|
16
|
+
RACK_NAMESPACE = 'Rack::Session::%s'.freeze
|
|
17
|
+
|
|
18
|
+
# Localhost string for detecting localhost host configuration
|
|
19
|
+
#
|
|
20
|
+
# @since 0.2.0
|
|
21
|
+
# @api private
|
|
22
|
+
BLACKLISTED_DOMAINS = %w(localhost).freeze
|
|
23
|
+
|
|
24
|
+
# HTTP sessions configuration
|
|
25
|
+
#
|
|
26
|
+
# @param adapter [Symbol,String,Class] the session adapter
|
|
27
|
+
# @param options [Hash] the optional session options
|
|
28
|
+
# @param configuration [Hanami::Configuration] the application configuration
|
|
29
|
+
#
|
|
30
|
+
# @since 0.2.0
|
|
31
|
+
# @api private
|
|
32
|
+
#
|
|
33
|
+
# @see http://www.rubydoc.info/github/rack/rack/Rack/Session/Abstract/ID
|
|
34
|
+
def initialize(adapter = nil, options = {}, configuration = nil)
|
|
35
|
+
@adapter = adapter
|
|
36
|
+
@options = options
|
|
37
|
+
@configuration = configuration
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Check if the sessions are enabled
|
|
41
|
+
#
|
|
42
|
+
# @return [FalseClass,TrueClass] the result of the check
|
|
43
|
+
#
|
|
44
|
+
# @since 0.2.0
|
|
45
|
+
# @api private
|
|
46
|
+
def enabled?
|
|
47
|
+
!!@adapter
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Returns the Rack middleware and the options
|
|
51
|
+
#
|
|
52
|
+
# @return [Array] Rack middleware and options
|
|
53
|
+
#
|
|
54
|
+
# @since 0.2.0
|
|
55
|
+
# @api private
|
|
56
|
+
def middleware
|
|
57
|
+
middleware = case @adapter
|
|
58
|
+
when Symbol
|
|
59
|
+
RACK_NAMESPACE % Utils::String.new(@adapter).classify
|
|
60
|
+
else
|
|
61
|
+
@adapter
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
[middleware, options]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
# @since 0.2.0
|
|
70
|
+
# @api private
|
|
71
|
+
def options
|
|
72
|
+
default_options.merge(@options)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# @since 0.2.0
|
|
76
|
+
# @api private
|
|
77
|
+
def default_options
|
|
78
|
+
if @configuration
|
|
79
|
+
{ domain: domain, secure: @configuration.ssl? }
|
|
80
|
+
else
|
|
81
|
+
{}
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def domain
|
|
86
|
+
domain = @configuration.host
|
|
87
|
+
if !BLACKLISTED_DOMAINS.include?(domain) && !ip_address?(domain)
|
|
88
|
+
domain
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def ip_address?(string)
|
|
93
|
+
!!IPAddr.new(string) rescue false
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|