Class: Brut::Framework::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/framework/config.rb

Overview

Holds configuration for the framework and your app. In general, you should not interact with this class, however it's source code is a good reference for what is configured by default by Brut.

Instance Method Summary collapse

Instance Method Details

#configure!Object

Configures all defaults. In general, this attempts to be lazy in setting things up, so calling this should not attempt to make a connection to your database.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/brut/framework/config.rb', line 12

def configure!
  Brut.container do |c|
    # Brut Stuff that should not be changed

    c.store_ensured_path(
      "tmp_dir",
      "Temporary directory where ephemeral files can do"
    ) do |project_root|
      project_root / "tmp"
    end

    c.store(
      "project_env",
      Brut::Framework::ProjectEnvironment,
      "The environment of the running app, e.g. dev/test/prod",
      Brut::Framework::ProjectEnvironment.new(ENV["RACK_ENV"])
    )

    c.store_ensured_path(
      "log_dir",
      "Path where log files may be written"
    ) do |project_root|
      project_root / "logs"
    end

    c.store_ensured_path(
      "public_root_dir",
      "Path to the root of all public files"
    ) do |project_root|
      project_root / "app" / "public"
    end

    c.store_ensured_path(
      "images_root_dir",
      "Path to the root of all images"
    ) do |public_root_dir|
      public_root_dir / "static" / "images"
    end

    c.store_ensured_path(
      "css_bundle_output_dir",
      "Path where bundled CSS is written for use in web pages"
    ) do |public_root_dir|
      public_root_dir / "css"
    end

    c.store_ensured_path(
      "js_bundle_output_dir",
      "Path where bundled JS is written for use in web pages"
    ) do |public_root_dir|
      public_root_dir / "js"
    end

    c.store(
      "database_url",
      String,
      "URL to the primary database - generally avoid this and use sequel_db_handle"
    ) do
      ENV.fetch("DATABASE_URL")
    end

    c.store(
      "sequel_db_handle",
      Object,
      "Handle to the database",
    ) do |database_url|
      Sequel.connect(database_url)
    end

    c.store_ensured_path(
      "app_src_dir",
      "Path to root of where all the app's source files are"
    ) do |project_root|
      project_root / "app" / "src"
    end

    c.store_ensured_path(
      "app_specs_dir",
      "Path to root of where all the app's specs/tests are"
    ) do |project_root|
      project_root / "specs"
    end

    c.store_ensured_path(
      "e2e_specs_dir",
      "Path to the root of all end-to-end tests"
    ) do |app_specs_dir|
      app_specs_dir / "e2e"
    end

    c.store_ensured_path(
      "js_specs_dir",
      "Path to root of where all JS-based specs/tests are",
    ) do |app_specs_dir|
      app_specs_dir / "front_end" / "js"
    end

    c.store_ensured_path(
      "front_end_src_dir",
      "Path to the root of the front end layer for the app"
    ) do |app_src_dir|
      app_src_dir / "front_end"
    end

    c.store_ensured_path(
      "components_src_dir",
      "Path to where components classes and templates are stored"
    ) do |front_end_src_dir|
      front_end_src_dir / "components"
    end

    c.store_ensured_path(
      "components_specs_dir",
      "Path to where tests of components classes are stored",
    ) do |app_specs_dir|
      app_specs_dir / "front_end" / "components"
    end

    c.store_ensured_path(
      "forms_src_dir",
      "Path to where form classes are stored"
    ) do |front_end_src_dir|
      front_end_src_dir / "forms"
    end

    c.store_ensured_path(
      "handlers_src_dir",
      "Path to where handlers are stored"
    ) do |front_end_src_dir|
      front_end_src_dir / "handlers"
    end

    c.store_ensured_path(
      "handlers_specs_dir",
      "Path to where tests of handler classes are stored",
    ) do |app_specs_dir|
      app_specs_dir / "front_end" / "handlers"
    end

    c.store_ensured_path(
      "svgs_src_dir",
      "Path to where svgs are stored"
    ) do |front_end_src_dir|
      front_end_src_dir / "svgs"
    end

    c.store_ensured_path(
      "images_src_dir",
      "Path to where images are stored"
    ) do |front_end_src_dir|
      front_end_src_dir / "images"
    end

    c.store_required_path(
      "pages_src_dir",
      "Path to where page classes and templates are stored"
    ) do |front_end_src_dir|
      front_end_src_dir / "pages"
    end

    c.store_required_path(
      "pages_specs_dir",
      "Path to where tests of page classes are stored",
    ) do |app_specs_dir|
      app_specs_dir / "front_end" / "pages"
    end

    c.store_required_path(
      "layouts_src_dir",
      "Path to where layout classes and templates are stored"
    ) do |front_end_src_dir|
      front_end_src_dir / "layouts"
    end

    c.store_required_path(
      "js_src_dir",
      "Path to where JS files are",
    ) do |front_end_src_dir|
      front_end_src_dir / "js"
    end

    c.store_ensured_path(
      "back_end_src_dir",
      "Path to the root of the back end layer for the app"
    ) do |app_src_dir|
      app_src_dir / "back_end"
    end

    c.store_ensured_path(
      "data_models_src_dir",
      "Path to the root of all data modeling",
    ) do |back_end_src_dir|
      back_end_src_dir / "data_models"
    end

    c.store_ensured_path(
      "migrations_dir",
      "Path to the DB migrations",
    ) do |data_models_src_dir|
      data_models_src_dir / "migrations"
    end

    c.store_ensured_path(
      "db_seeds_dir",
      "Path to the seed data for the DB",
    ) do |data_models_src_dir|
      data_models_src_dir / "seed"
    end

    c.store_ensured_path(
      "config_dir",
      "Path to where configuration files are stores"
    ) do |project_root|
      project_root / "app" / "config"
    end

    c.store_ensured_path(
      "i18n_locales_dir",
      "Path to where I18N locale files are stored"
    ) do |config_dir|
      config_dir / "i18n"
    end

    c.store(
      "asset_metadata_file",
      Pathname,
      "Path to the asset metadata file, used to manage hashed asset names"
    ) do |config_dir|
      config_dir / "asset_metadata.json"
    end

    c.store_required_path(
      "brut_internal_dir",
      "Location to where the Brut gem is installed."
    ) do
      (Pathname(__FILE__).dirname / ".." / ".." / "..").expand_path
    end

    c.store(
      "svg_locator",
      "Brut::FrontEnd::InlineSvgLocator",
      "Object to use to locate SVGs"
    ) do |svgs_src_dir|
      Brut::FrontEnd::InlineSvgLocator.new(paths: svgs_src_dir)
    end

    c.store(
      "asset_path_resolver",
      "Brut::FrontEnd::AssetPathResolver",
      "Object to use to resolve logical asset paths to actual asset paths"
    ) do ||
      Brut::FrontEnd::AssetPathResolver.new(metadata_file: )
    end

    c.store(
      "routing",
      "Brut::FrontEnd::Routing",
      "Routing for all registered routes of this app",
      Brut::FrontEnd::Routing.new
    )

    c.store(
      "instrumentation",
      Brut::Instrumentation::OpenTelemetry,
      "Interface for recording instrumentable events and subscribing to them",
      Brut::Instrumentation::OpenTelemetry.new
    )

    # App can override

    c.store(
      "external_id_prefix",
      String,
      "String to use as a prefix for external ids in tables using the external_id feature. Nil means the feature is disabled",
      nil,
      allow_app_override: true,
      allow_nil: true,
    )

    c.store(
      "debug_zeitwerk?",
      :boolean,
      "If true, Zeitwerk's loading will be logged for debugging purposes. Do not enable this in production",
      false,
      allow_app_override: true,
    )

    c.store(
      "session_class",
      Class,
      "Class to use when wrapping the Rack session",
      Brut::FrontEnd::Session,
      allow_app_override: true,
    )

    c.store(
      "flash_class",
      Class,
      "Class to use to represent the Flash",
      Brut::FrontEnd::Flash,
      allow_app_override: true,
    )

    c.store(
      "semantic_logger_appenders",
      { Hash => "if only one appender is needed", Array => "to configure multiple appenders" },
      "List of appenders to be configured for SemanticLogger",
      allow_app_override: true
    ) do |project_env,log_dir|
      appenders = if project_env.development?
                    [
                      { formatter: :color, io: $stdout },
                      { file_name: (log_dir / "development.log").to_s },
                    ]
                  end
      if appenders.nil?
        appenders = { file_name: (log_dir / "#{project_env}.log").to_s }
      end
      if appenders.nil?
        appenders = { io: $stdout }
      end
      appenders
    end

    c.store(
      "eager_load_classes?",
      :boolean,
      "If true, classes are eagerly loaded upon startup",
      true,
      allow_app_override: true
    )

    c.store(
      "auto_reload_classes?",
      :boolean,
      "If true, classes are reloaded with each request. Useful only really for development",
      allow_app_override: true
    ) do |project_env|
      no_reload_in_dev = ENV["BRUT_NO_RELOAD_IN_DEV"] == "true"
      if project_env.development?
        !no_reload_in_dev
      else
        false
      end
    end

    c.store(
      "log_level",
      String,
      "Log level to control how much logging is happening",
      allow_app_override: true,
    ) do
      ENV["LOG_LEVEL"] || "debug"
    end

    c.store(
      "csp_class",
      Class,
      "Route Hook to use for setting the Content-Security-Policy header",
      allow_app_override: true,
      allow_nil: true,
    ) do |project_env|
      if project_env.development?
        Brut::FrontEnd::RouteHooks::CSPNoInlineScripts
      else
        Brut::FrontEnd::RouteHooks::CSPNoInlineStylesOrScripts
      end
    end

    c.store(
      "csp_reporting_class",
      Class,
      "Route Hook to use for setting the Content-Security-Policy-Report-Only header",
      Brut::FrontEnd::RouteHooks::CSPNoInlineStylesOrScripts::ReportOnly,
      allow_app_override: true,
      allow_nil: true,
    )

    Brut.container.store(
      "fallback_host",
      URI,
      "Hostname to use in situations where the request is not available",
      nil,
      allow_app_override: true,
      allow_nil: true
    )

    c.store(
      "local_hostname",
      String,
      "If present, this is an additional host on which your app responds locally. Useful if you have local domain names set up for dev",
      nil,
      allow_app_override: true,
      allow_nil: true
    )


    c.store(
      "permitted_hosts",
      Array,
      "An array of hostnames or IPAddr objects representing which hosts this app will respond to",
    ) do |local_hostname,project_env|
      if project_env.production?
        []
      else
        [
          local_hostname,
          "localhost",
          ".localhost",
          ".test",
          IPAddr.new("0.0.0.0/0"),
          IPAddr.new("::/0"),
        ].compact
      end
    end

  end
end