[openstreetmap/openstreetmap-website] Use Config gem to read and validate configuration settings (#2170)

Andy Allan notifications at github.com
Wed Mar 6 16:05:40 UTC 2019


This is a partial solution to #2169 using the [Config](https://github.com/railsconfig/config/) gem. It features:

* validating particular settings, and what format those settings should be, at boot time
* supports our existing config file structure (through some hackery), including special settings for tests
* works with existing application.yml files
* supports environment variable overrides (but see below)

However:

* It needs to work in two stages, using initializers/settings.rb to revisit the setup with our custom application.yml file. This isn't ideal, since running everything in initializers/config.rb would mean settings are processed at the [before_configuration](https://github.com/railsconfig/config/blob/3a28b646a69c16f87d6539b4e1c4189aecb34c84/lib/config/integrations/rails/railtie.rb#L21) stage, whereas settings.rb runs later.
* It still requires our preinitializer, in order that the constants required in `application.rb` are available. `application.rb` runs before the code in `initializers/settings.rb`
* Although it supports environment variables, it can't support the same naming convention as we currently use. For example, we currently support e.g. 'OSM_API_TIMEOUT=foo'. If we set the prefix to 'OSM' and the env_separator to '_', then that parses to 'Settings.api.timeout' instead of 'Settings.api_timeout'. I'm really not sure if this is a big deal or not.
* There's a corner case involving environment variables that look-like-numbers-but-aren't. For example, with `env OSM.api_version=0.7 bundle exec ...`, the 0.7 gets converted to a float and then fails the string validation.

If we can ignore environment variables (can we?) then this still is an improvement on what we have already, due to the validations. However, it could be even better if there are other refactorings that can remove the need for the preinitializer code entirely, and I welcome any feedback.

You can view, comment on, or merge this pull request online at:

  https://github.com/openstreetmap/openstreetmap-website/pull/2170

-- Commit Summary --

  * Use Config gem to read and validate configuration settings

-- File Changes --

    M .gitignore (4)
    M Gemfile (1)
    M Gemfile.lock (34)
    M app/views/api/capabilities/show.builder (24)
    A config/initializers/config.rb (48)
    A config/initializers/settings.rb (27)

-- Patch Links --

https://github.com/openstreetmap/openstreetmap-website/pull/2170.patch
https://github.com/openstreetmap/openstreetmap-website/pull/2170.diff

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/2170
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20190306/de7cc460/attachment.html>


More information about the rails-dev mailing list