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,59 @@
|
|
|
1
|
+
require 'thread'
|
|
2
|
+
require 'rack/builder'
|
|
3
|
+
require 'hanami/router'
|
|
4
|
+
|
|
5
|
+
module Hanami
|
|
6
|
+
class Container
|
|
7
|
+
class Router < ::Hanami::Router
|
|
8
|
+
def mount(app, options)
|
|
9
|
+
app = app.new(path_prefix: options.fetch(:at)) if hanami_app?(app)
|
|
10
|
+
super(app, options)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def hanami_app?(app)
|
|
16
|
+
app.ancestors.include? Hanami::Application
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
attr_reader :routes
|
|
21
|
+
|
|
22
|
+
def self.configure(options = {}, &blk)
|
|
23
|
+
Mutex.new.synchronize do
|
|
24
|
+
@@options = options
|
|
25
|
+
@@configuration = blk
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def initialize
|
|
30
|
+
Mutex.new.synchronize do
|
|
31
|
+
assert_configuration_presence!
|
|
32
|
+
prepare_middleware_stack!
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def call(env)
|
|
37
|
+
@builder.call(env)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
def assert_configuration_presence!
|
|
42
|
+
unless self.class.class_variable_defined?(:@@configuration)
|
|
43
|
+
raise ArgumentError.new("#{ self.class } doesn't have any application mounted.")
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def prepare_middleware_stack!
|
|
48
|
+
@builder = ::Rack::Builder.new
|
|
49
|
+
@routes = Router.new(&@@configuration)
|
|
50
|
+
|
|
51
|
+
if Hanami.environment.serve_static_assets?
|
|
52
|
+
require 'hanami/static'
|
|
53
|
+
@builder.use Hanami::Static
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
@builder.run @routes
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
require 'thread'
|
|
2
|
+
require 'pathname'
|
|
3
|
+
require 'dotenv'
|
|
4
|
+
require 'hanami/utils'
|
|
5
|
+
require 'hanami/utils/hash'
|
|
6
|
+
require 'hanami/hanamirc'
|
|
7
|
+
|
|
8
|
+
module Hanami
|
|
9
|
+
# Define and expose information about the Hanami environment.
|
|
10
|
+
#
|
|
11
|
+
# @since 0.1.0
|
|
12
|
+
# @api private
|
|
13
|
+
class Environment
|
|
14
|
+
# Standard Rack ENV key
|
|
15
|
+
#
|
|
16
|
+
# @since 0.1.0
|
|
17
|
+
# @api private
|
|
18
|
+
RACK_ENV = 'RACK_ENV'.freeze
|
|
19
|
+
|
|
20
|
+
# Standard Hanami ENV key
|
|
21
|
+
#
|
|
22
|
+
# @since 0.1.0
|
|
23
|
+
# @api private
|
|
24
|
+
HANAMI_ENV = 'HANAMI_ENV'.freeze
|
|
25
|
+
|
|
26
|
+
# Default Hanami environment
|
|
27
|
+
#
|
|
28
|
+
# @since 0.1.0
|
|
29
|
+
# @api private
|
|
30
|
+
DEFAULT_ENV = 'development'.freeze
|
|
31
|
+
|
|
32
|
+
# Production environment
|
|
33
|
+
#
|
|
34
|
+
# @since 0.6.0
|
|
35
|
+
# @api private
|
|
36
|
+
PRODUCTION_ENV = 'production'.freeze
|
|
37
|
+
|
|
38
|
+
# Rack production environment (aka deployment)
|
|
39
|
+
#
|
|
40
|
+
# @since 0.6.0
|
|
41
|
+
# @api private
|
|
42
|
+
RACK_ENV_DEPLOYMENT = 'deployment'.freeze
|
|
43
|
+
|
|
44
|
+
# Default `.env` file name
|
|
45
|
+
#
|
|
46
|
+
# @since 0.2.0
|
|
47
|
+
# @api private
|
|
48
|
+
DEFAULT_DOTENV = '.env'.freeze
|
|
49
|
+
|
|
50
|
+
# Default `.env` per environment file name
|
|
51
|
+
#
|
|
52
|
+
# @since 0.2.0
|
|
53
|
+
# @api private
|
|
54
|
+
DEFAULT_DOTENV_ENV = '.env.%s'.freeze
|
|
55
|
+
|
|
56
|
+
# Default configuration directory under application root
|
|
57
|
+
#
|
|
58
|
+
# @since 0.2.0
|
|
59
|
+
# @api private
|
|
60
|
+
DEFAULT_CONFIG = 'config'.freeze
|
|
61
|
+
|
|
62
|
+
# Standard Hanami host ENV key
|
|
63
|
+
#
|
|
64
|
+
# @since 0.1.0
|
|
65
|
+
# @api private
|
|
66
|
+
HANAMI_HOST = 'HANAMI_HOST'.freeze
|
|
67
|
+
|
|
68
|
+
# Default HTTP host
|
|
69
|
+
#
|
|
70
|
+
# @since 0.1.0
|
|
71
|
+
# @api private
|
|
72
|
+
DEFAULT_HOST = 'localhost'.freeze
|
|
73
|
+
|
|
74
|
+
# Default IP address listen
|
|
75
|
+
#
|
|
76
|
+
# @since 0.1.0
|
|
77
|
+
# @api private
|
|
78
|
+
LISTEN_ALL_HOST = '0.0.0.0'.freeze
|
|
79
|
+
|
|
80
|
+
# Standard Hanami port ENV key
|
|
81
|
+
#
|
|
82
|
+
# @since 0.1.0
|
|
83
|
+
# @api private
|
|
84
|
+
HANAMI_PORT = 'HANAMI_PORT'.freeze
|
|
85
|
+
|
|
86
|
+
# Default Hanami HTTP port
|
|
87
|
+
#
|
|
88
|
+
# @since 0.1.0
|
|
89
|
+
# @api private
|
|
90
|
+
DEFAULT_PORT = 2300
|
|
91
|
+
|
|
92
|
+
# Default Rack configuration file
|
|
93
|
+
#
|
|
94
|
+
# @since 0.2.0
|
|
95
|
+
# @api private
|
|
96
|
+
DEFAULT_RACKUP = 'config.ru'.freeze
|
|
97
|
+
|
|
98
|
+
# Default environment configuration file
|
|
99
|
+
#
|
|
100
|
+
# @since 0.2.0
|
|
101
|
+
# @api private
|
|
102
|
+
DEFAULT_ENVIRONMENT_CONFIG = 'environment'.freeze
|
|
103
|
+
|
|
104
|
+
# Code reloading per environment
|
|
105
|
+
#
|
|
106
|
+
# @since 0.2.0
|
|
107
|
+
# @api private
|
|
108
|
+
CODE_RELOADING = { 'development' => true }.freeze
|
|
109
|
+
|
|
110
|
+
# @since 0.4.0
|
|
111
|
+
# @api private
|
|
112
|
+
CONTAINER = 'container'.freeze
|
|
113
|
+
|
|
114
|
+
# @since 0.4.0
|
|
115
|
+
# @api private
|
|
116
|
+
CONTAINER_PATH = 'apps'.freeze
|
|
117
|
+
|
|
118
|
+
# @since 0.4.0
|
|
119
|
+
# @api private
|
|
120
|
+
APPLICATION = 'app'.freeze
|
|
121
|
+
|
|
122
|
+
# @since 0.4.0
|
|
123
|
+
# @api private
|
|
124
|
+
APPLICATION_PATH = 'app'.freeze
|
|
125
|
+
|
|
126
|
+
# @since 0.4.0
|
|
127
|
+
# @api private
|
|
128
|
+
SERVE_STATIC_ASSETS = 'SERVE_STATIC_ASSETS'.freeze
|
|
129
|
+
|
|
130
|
+
# @since 0.4.0
|
|
131
|
+
# @api private
|
|
132
|
+
SERVE_STATIC_ASSETS_ENABLED = 'true'.freeze
|
|
133
|
+
|
|
134
|
+
# Initialize a Hanami environment
|
|
135
|
+
#
|
|
136
|
+
# It accepts an optional set of configurations from the CLI commands.
|
|
137
|
+
# Those settings override the defaults defined by this object.
|
|
138
|
+
#
|
|
139
|
+
# When initialized, it sets standard `ENV` variables for Rack and Hanami,
|
|
140
|
+
# such as `RACK_ENV` and `HANAMI_ENV`.
|
|
141
|
+
#
|
|
142
|
+
# It also evaluates configuration from `.env` and `.env.<environment>`
|
|
143
|
+
# located under the config directory. All the settings in those files will
|
|
144
|
+
# be exported as `ENV` variables.
|
|
145
|
+
#
|
|
146
|
+
# The format of those `.env` files is compatible with `dotenv` and `foreman`
|
|
147
|
+
# gems.
|
|
148
|
+
#
|
|
149
|
+
# @param options [Hash] override default options for various environment
|
|
150
|
+
# attributes
|
|
151
|
+
#
|
|
152
|
+
# @return [Hanami::Environment] the environment
|
|
153
|
+
#
|
|
154
|
+
# @see Hanami::Commands::Console
|
|
155
|
+
# @see Hanami::Commands::Routes
|
|
156
|
+
# @see Hanami::Commands::Server
|
|
157
|
+
# @see Hanami::Environment#config
|
|
158
|
+
#
|
|
159
|
+
# @example Define ENV variables from .env
|
|
160
|
+
#
|
|
161
|
+
# # % tree .
|
|
162
|
+
# # .
|
|
163
|
+
# # # ...
|
|
164
|
+
# # ├── .env
|
|
165
|
+
# # └── .env.development
|
|
166
|
+
#
|
|
167
|
+
# # % cat .env
|
|
168
|
+
# # FOO="bar"
|
|
169
|
+
# # XYZ="yes"
|
|
170
|
+
#
|
|
171
|
+
# # % cat .env.development
|
|
172
|
+
# # FOO="ok"
|
|
173
|
+
#
|
|
174
|
+
# require 'hanami/environment'
|
|
175
|
+
#
|
|
176
|
+
# env = Hanami::Environment.new
|
|
177
|
+
# env.environment # => "development"
|
|
178
|
+
#
|
|
179
|
+
# # Framework defined ENV vars
|
|
180
|
+
# ENV['HANAMI_ENV'] # => "development"
|
|
181
|
+
# ENV['RACK_ENV'] # => "development"
|
|
182
|
+
#
|
|
183
|
+
# ENV['HANAMI_HOST'] # => "localhost"
|
|
184
|
+
# ENV['HANAMI_PORT'] # => "2300"
|
|
185
|
+
#
|
|
186
|
+
# # User defined ENV vars
|
|
187
|
+
# ENV['FOO'] # => "ok"
|
|
188
|
+
# ENV['XYZ'] # => "yes"
|
|
189
|
+
#
|
|
190
|
+
# # Hanami::Environment evaluates `.env` first as master configuration.
|
|
191
|
+
# # Then it evaluates `.env.development` because the current environment
|
|
192
|
+
# # is "development". The settings defined in this last file override
|
|
193
|
+
# # the one defined in the parent (eg `FOO` is overwritten). All the
|
|
194
|
+
# # other settings (eg `XYZ`) will be left untouched.
|
|
195
|
+
def initialize(options = {})
|
|
196
|
+
@options = Hanami::Hanamirc.new(root).options
|
|
197
|
+
@options.merge! Utils::Hash.new(options.clone).symbolize!
|
|
198
|
+
@mutex = Mutex.new
|
|
199
|
+
@mutex.synchronize { set_env_vars! }
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# The current environment
|
|
203
|
+
#
|
|
204
|
+
# In order to decide the value, it looks up to the following `ENV` vars:
|
|
205
|
+
#
|
|
206
|
+
# * HANAMI_ENV
|
|
207
|
+
# * RACK_ENV
|
|
208
|
+
#
|
|
209
|
+
# If those are missing it falls back to the defalt one: `"development"`.
|
|
210
|
+
#
|
|
211
|
+
# Rack environment `"deployment"` is translated to Hanami `"production"`.
|
|
212
|
+
#
|
|
213
|
+
# @return [String] the current environment
|
|
214
|
+
#
|
|
215
|
+
# @since 0.1.0
|
|
216
|
+
#
|
|
217
|
+
# @see Hanami::Environment::DEFAULT_ENV
|
|
218
|
+
def environment
|
|
219
|
+
@environment ||= ENV[HANAMI_ENV] || rack_env || DEFAULT_ENV
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# @since 0.3.1
|
|
223
|
+
#
|
|
224
|
+
# @see Hanami.env?(name)
|
|
225
|
+
def environment?(*names)
|
|
226
|
+
names.map(&:to_s).include?(environment)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# A set of Bundler groups
|
|
230
|
+
#
|
|
231
|
+
# @return [Array] A set of groups
|
|
232
|
+
#
|
|
233
|
+
# @since 0.2.0
|
|
234
|
+
# @api private
|
|
235
|
+
#
|
|
236
|
+
# @see http://bundler.io/v1.7/groups.html
|
|
237
|
+
def bundler_groups
|
|
238
|
+
[:default, environment]
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Application's root
|
|
242
|
+
#
|
|
243
|
+
# It defaults to the current working directory.
|
|
244
|
+
# Hanami assumes that all the commands are executed from there.
|
|
245
|
+
#
|
|
246
|
+
# @return [Pathname] application's root
|
|
247
|
+
#
|
|
248
|
+
# @since 0.2.0
|
|
249
|
+
def root
|
|
250
|
+
@root ||= Pathname.new(Dir.pwd)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# Application's config directory
|
|
254
|
+
#
|
|
255
|
+
# It's the application where all the configurations are stored.
|
|
256
|
+
#
|
|
257
|
+
# In order to decide the value, it looks up the following sources:
|
|
258
|
+
#
|
|
259
|
+
# * CLI option `config`
|
|
260
|
+
#
|
|
261
|
+
# If those are missing it falls back to the default one: `"config/"`.
|
|
262
|
+
#
|
|
263
|
+
# When a relative path is given via CLI option, it assumes to be located
|
|
264
|
+
# under application's root. If absolute path, it will be used as it is.
|
|
265
|
+
#
|
|
266
|
+
# @return [Pathname] the config directory
|
|
267
|
+
#
|
|
268
|
+
# @since 0.2.0
|
|
269
|
+
#
|
|
270
|
+
# @see Hanami::Environment::DEFAULT_CONFIG
|
|
271
|
+
# @see Hanami::Environment#root
|
|
272
|
+
def config
|
|
273
|
+
@config ||= root.join(@options.fetch(:config) { DEFAULT_CONFIG })
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# The HTTP host name
|
|
277
|
+
#
|
|
278
|
+
# In order to decide the value, it looks up the following sources:
|
|
279
|
+
#
|
|
280
|
+
# * CLI option `host`
|
|
281
|
+
# * HANAMI_HOST ENV var
|
|
282
|
+
#
|
|
283
|
+
# If those are missing it falls back to the following defaults:
|
|
284
|
+
#
|
|
285
|
+
# * `"localhost"` for development
|
|
286
|
+
# * `"0.0.0.0"` for all the other environments
|
|
287
|
+
#
|
|
288
|
+
# @return [String] the HTTP host name
|
|
289
|
+
#
|
|
290
|
+
# @since 0.1.0
|
|
291
|
+
#
|
|
292
|
+
# @see Hanami::Environment::DEFAULT_HOST
|
|
293
|
+
# @see Hanami::Environment::LISTEN_ALL_HOST
|
|
294
|
+
def host
|
|
295
|
+
@host ||= @options.fetch(:host) {
|
|
296
|
+
ENV[HANAMI_HOST] || default_host
|
|
297
|
+
}
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
# The HTTP port
|
|
301
|
+
#
|
|
302
|
+
# In order to decide the value, it looks up the following sources:
|
|
303
|
+
#
|
|
304
|
+
# * CLI option `port`
|
|
305
|
+
# * HANAMI_PORT ENV var
|
|
306
|
+
#
|
|
307
|
+
# If those are missing it falls back to the default one: `2300`.
|
|
308
|
+
#
|
|
309
|
+
# @return [Integer] the default port
|
|
310
|
+
#
|
|
311
|
+
# @since 0.1.0
|
|
312
|
+
#
|
|
313
|
+
# @see Hanami::Environment::DEFAULT_PORT
|
|
314
|
+
def port
|
|
315
|
+
@port ||= @options.fetch(:port) { ENV[HANAMI_PORT] || DEFAULT_PORT }.to_i
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# Path to the Rack configuration file
|
|
319
|
+
#
|
|
320
|
+
# In order to decide the value, it looks up the following sources:
|
|
321
|
+
#
|
|
322
|
+
# * CLI option `rackup`
|
|
323
|
+
#
|
|
324
|
+
# If those are missing it falls back to the default one: `"config.ru"`.
|
|
325
|
+
#
|
|
326
|
+
# When a relative path is given via CLI option, it assumes to be located
|
|
327
|
+
# under application's root. If absolute path, it will be used as it is.
|
|
328
|
+
#
|
|
329
|
+
# @return [Pathname] path to the Rack configuration file
|
|
330
|
+
#
|
|
331
|
+
# @since 0.2.0
|
|
332
|
+
def rackup
|
|
333
|
+
root.join(@options.fetch(:rackup) { DEFAULT_RACKUP })
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
# Path to environment configuration file.
|
|
337
|
+
#
|
|
338
|
+
# In order to decide the value, it looks up the following sources:
|
|
339
|
+
#
|
|
340
|
+
# * CLI option `environment`
|
|
341
|
+
#
|
|
342
|
+
# If those are missing it falls back to the default one:
|
|
343
|
+
# `"config/environment.rb"`.
|
|
344
|
+
#
|
|
345
|
+
# When a relative path is given via CLI option, it assumes to be located
|
|
346
|
+
# under application's root. If absolute path, it will be used as it is.
|
|
347
|
+
#
|
|
348
|
+
# @return [Pathname] path to applications
|
|
349
|
+
#
|
|
350
|
+
# @since 0.1.0
|
|
351
|
+
#
|
|
352
|
+
# @see Hanami::Environment::DEFAULT_ENVIRONMENT_CONFIG
|
|
353
|
+
def env_config
|
|
354
|
+
root.join(@options.fetch(:environment) { config.join(DEFAULT_ENVIRONMENT_CONFIG) })
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# Require application environment
|
|
358
|
+
#
|
|
359
|
+
# Eg <tt>require "config/environment"</tt>.
|
|
360
|
+
#
|
|
361
|
+
# @since 0.4.0
|
|
362
|
+
# @api private
|
|
363
|
+
def require_application_environment
|
|
364
|
+
require env_config.to_s
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
# Determine if activate code reloading for the current environment while
|
|
368
|
+
# running the server.
|
|
369
|
+
#
|
|
370
|
+
# In order to decide the value, it looks up the following sources:
|
|
371
|
+
#
|
|
372
|
+
# * CLI option `code_reloading`
|
|
373
|
+
#
|
|
374
|
+
# If those are missing it falls back to the following defaults:
|
|
375
|
+
#
|
|
376
|
+
# * true for development
|
|
377
|
+
# * false for all the other environments
|
|
378
|
+
#
|
|
379
|
+
# @return [TrueClass,FalseClass] the result of the check
|
|
380
|
+
#
|
|
381
|
+
# @since 0.2.0
|
|
382
|
+
#
|
|
383
|
+
# @see Hanami::Commands::Server
|
|
384
|
+
# @see Hanami::Environment::CODE_RELOADING
|
|
385
|
+
def code_reloading?
|
|
386
|
+
# JRuby doesn't implement fork that's why shotgun cannot be used.
|
|
387
|
+
if Utils.jruby?
|
|
388
|
+
puts "JRuby doesn't support code reloading."
|
|
389
|
+
false
|
|
390
|
+
else
|
|
391
|
+
@options.fetch(:code_reloading) { !!CODE_RELOADING[environment] }
|
|
392
|
+
end
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# @since 0.4.0
|
|
396
|
+
# @api private
|
|
397
|
+
def architecture
|
|
398
|
+
@options.fetch(:architecture) {
|
|
399
|
+
puts "Cannot recognize Hanami architecture, please check `.hanamirc'"
|
|
400
|
+
exit 1
|
|
401
|
+
}
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
# @since 0.4.0
|
|
405
|
+
# @api private
|
|
406
|
+
def container?
|
|
407
|
+
architecture == CONTAINER
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
# @since 0.6.0
|
|
411
|
+
# @api private
|
|
412
|
+
def serve_static_assets?
|
|
413
|
+
SERVE_STATIC_ASSETS_ENABLED == ENV[SERVE_STATIC_ASSETS]
|
|
414
|
+
end
|
|
415
|
+
|
|
416
|
+
# @since 0.4.0
|
|
417
|
+
# @api private
|
|
418
|
+
def apps_path
|
|
419
|
+
@options.fetch(:path) {
|
|
420
|
+
case architecture
|
|
421
|
+
when CONTAINER then CONTAINER_PATH
|
|
422
|
+
when APPLICATION then APPLICATION_PATH
|
|
423
|
+
end
|
|
424
|
+
}
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
# Serialize the most relevant settings into a Hash
|
|
428
|
+
#
|
|
429
|
+
# @return [Hanami::Utils::Hash]
|
|
430
|
+
#
|
|
431
|
+
# @since 0.1.0
|
|
432
|
+
# @api private
|
|
433
|
+
def to_options
|
|
434
|
+
@options.merge(
|
|
435
|
+
environment: environment,
|
|
436
|
+
env_config: env_config,
|
|
437
|
+
apps_path: apps_path,
|
|
438
|
+
rackup: rackup,
|
|
439
|
+
host: host,
|
|
440
|
+
port: port
|
|
441
|
+
)
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
private
|
|
445
|
+
|
|
446
|
+
# @since 0.1.0
|
|
447
|
+
# @api private
|
|
448
|
+
def set_env_vars!
|
|
449
|
+
set_application_env_vars!
|
|
450
|
+
set_hanami_env_vars!
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
# @since 0.2.0
|
|
454
|
+
# @api private
|
|
455
|
+
def set_hanami_env_vars!
|
|
456
|
+
ENV[HANAMI_ENV] = ENV[RACK_ENV] = environment
|
|
457
|
+
ENV[HANAMI_HOST] = host
|
|
458
|
+
ENV[HANAMI_PORT] = port.to_s
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
# @since 0.2.0
|
|
462
|
+
# @api private
|
|
463
|
+
def set_application_env_vars!
|
|
464
|
+
Dotenv.load root.join(DEFAULT_DOTENV)
|
|
465
|
+
Dotenv.overload root.join(DEFAULT_DOTENV_ENV % environment)
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
# @since 0.1.0
|
|
469
|
+
# @api private
|
|
470
|
+
def default_host
|
|
471
|
+
environment == DEFAULT_ENV ? DEFAULT_HOST : LISTEN_ALL_HOST
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
# @since 0.6.0
|
|
475
|
+
# @api private
|
|
476
|
+
def rack_env
|
|
477
|
+
case ENV[RACK_ENV]
|
|
478
|
+
when RACK_ENV_DEPLOYMENT
|
|
479
|
+
PRODUCTION_ENV
|
|
480
|
+
else
|
|
481
|
+
ENV[RACK_ENV]
|
|
482
|
+
end
|
|
483
|
+
end
|
|
484
|
+
end
|
|
485
|
+
end
|