runtime_config 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58dcecc281bda9d8d52d275e0e1fc5f4df94ac14
4
+ data.tar.gz: fc338520a97a7331942645e19a864ff3e4c5f51e
5
+ SHA512:
6
+ metadata.gz: 89d4ef80414aa18f57efd1b51129a9765419945fec1ec1de4bdac5b49c3b1658dc9f0c793ffadfd599e0da754fdf5a0f06c245838507eb0a5abf85a6a669b6db
7
+ data.tar.gz: 9acb0d7089ed1117e161d38b0a0fabca65a0ada2865bac0c5c09e11959bb1a8292b6dc7b20a009c78ff538ddd2cd3de7b6ebc33a9baabca1e9bae13f048c4260
@@ -0,0 +1,41 @@
1
+ # Runtime Conf Tool [![Gem Version](https://badge.fury.io/rb/runtime_config.svg)](https://badge.fury.io/rb/runtime_config)
2
+
3
+ A middleware to change configuration parameters at runtime for Rails 5.
4
+
5
+ ## Installation and Usage
6
+
7
+ - Add this to your `Gemfile`:
8
+
9
+ `gem 'runtime_config'`
10
+
11
+ - Add to `config/environments/development.rb` (_path_ option is not mandatory, '/dev' is the default value):
12
+
13
+ `config.middleware.use RuntimeConfig::Middleware, path: '/some_path'`
14
+
15
+ - Open the path (or the one set in the option): **/dev**
16
+
17
+ ## Features
18
+
19
+ - Change log level
20
+ - Filter log lines using a RegExp
21
+ - Enable/disable catching errors
22
+ - Eneble/disable verbose query logs
23
+ - Toggle cache
24
+ - Clear cache
25
+ - Restart server
26
+
27
+ ## Preview
28
+
29
+ ![screenshot](screenshot.png)
30
+
31
+ ## Do you like it? Star it!
32
+
33
+ If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
34
+
35
+ ## Contributors
36
+
37
+ - [Mattia Roccoberton](http://blocknot.es) - creator, maintainer
38
+
39
+ ## License
40
+
41
+ [MIT](LICENSE.txt)
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'runtime_config/conf_param'
4
+ require 'runtime_config/middleware'
@@ -0,0 +1,72 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <title>Runtime Conf Tool</title>
5
+ <link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
6
+ <style>
7
+ .button { height: 2.2rem; line-height: 2.2rem; }
8
+ .mono { background: #ddd; font-family: monospace; }
9
+ .row { align-items: flex-end; }
10
+ #form_filter_logs { margin-bottom: 0; }
11
+ </style>
12
+ </head>
13
+ <body>
14
+ <div class="container">
15
+ <div class="row">
16
+ <div class="column">
17
+ <h3><a href="<%= @options[:path] %>">Runtime Conf Tool</a></h3>
18
+ </div>
19
+ </div>
20
+ <div class="row">
21
+ <div class="column">
22
+ Set log level to: <br/>
23
+ <%= RuntimeConfig::LOGGER_SEVERITY.map{ |level| '<a class="button" href="?log=' + level + '">' + level + '</a>' }.join(' ') %>
24
+ </div>
25
+ </div>
26
+ <div class="row">
27
+ <div class="column column-50">
28
+ <a class="button" href="?restart">Restart server</a>
29
+ <a class="button" href="?cache">Toggle cache (*)</a>
30
+ <a class="button" href="?cache_clear=1">Clear cache (*)</a>
31
+ </div>
32
+ <div class="column">
33
+ Catch errors (404, 500, etc.):<br/>
34
+ <a class="button" href="?catch_errors=1">ON (*)</a>
35
+ <a class="button button-outline" href="?catch_errors=0">OFF (*)</a>
36
+ </div>
37
+ <div class="column">
38
+ Verbose query logs:<br/>
39
+ <a class="button" href="?verbose_query_logs=1">ON (*)</a>
40
+ <a class="button button-outline" href="?verbose_query_logs=0">OFF (*)</a>
41
+ </div>
42
+ </div>
43
+ <div class="row">
44
+ <div class="column column-80">
45
+ Filter logs (using a Regexp, ex. <span class="mono">app/views</span>) (*): <br/>
46
+ <form method="get" id="form_filter_logs">
47
+ <input type="text" name="filter_logs" />
48
+ </form>
49
+ </div>
50
+ <div class="column">
51
+ <input type="button" value="Filter" onclick="Javascript: document.querySelector('#form_filter_logs').submit()" />
52
+ </div>
53
+ </div>
54
+ <div class="row">
55
+ <div class="column">
56
+ <em>(*) restarts the server</em><br/>
57
+ <pre><code><% if @actions && @actions.any? %>
58
+ <%= @actions.join("\n") %>
59
+ <% else %>
60
+ Rails.logger.level: <b><%= RuntimeConfig::LOGGER_SEVERITY[Rails.logger.level] %></b>
61
+ Rails.application.config.active_record.verbose_query_logs: <b><%= Rails.application.config.active_record.verbose_query_logs %></b>
62
+ Rails.application.config.consider_all_requests_local: <b><%= Rails.application.config.consider_all_requests_local %></b>
63
+
64
+ Rails.cache: <b><%= Rails.application.config.cache_store == :null_store ? '-' : Rails.cache.inspect %></b>
65
+ Rails.application.config.cache_store: <b><%= Rails.application.config.cache_store %></b>
66
+ Rails.application.config.action_controller.perform_caching: <b><%= Rails.application.config.action_controller.perform_caching %></b>
67
+ <% end %></code></pre>
68
+ </div>
69
+ </div>
70
+ </div>
71
+ </body>
72
+ </html>
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeConfig
4
+ class ConfParam
5
+ attr_reader :exec, :conf, :opt, :parent
6
+
7
+ def initialize(opt, parent, conf, exec = nil)
8
+ @opt = opt
9
+ @parent = parent
10
+ @conf = conf
11
+ @exec = exec
12
+ end
13
+
14
+ def set(value)
15
+ @parent.send("#{@conf}=", value) if @parent
16
+ @exec.call(value) if @exec
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeConfig
4
+ LOGGER_SEVERITY = %w[debug info warn error fatal unknown].freeze # https://github.com/ruby/ruby/blob/trunk/lib/logger.rb
5
+
6
+ class Middleware
7
+ def initialize(app, options = {})
8
+ @app = app
9
+ @params = {
10
+ assets_logs: ConfParam.new(:assets_logs, Rails.configuration.assets, :quiet),
11
+ catch_errors: ConfParam.new(:catch_errors, Rails.configuration, :consider_all_requests_local),
12
+ filter_logs: ConfParam.new(:filter_logs, nil, :filter_logs, ->(value) {
13
+ regexp = Regexp.new value, Regexp::IGNORECASE
14
+ Rails.logger = ActiveSupport::Logger.new STDOUT
15
+ Rails.logger.formatter = proc do |_severity, _time, _progname, msg|
16
+ "#{msg}\n" if msg =~ regexp
17
+ end
18
+ }),
19
+ verbose_query_logs: ConfParam.new(:verbose_query_logs, Rails.configuration.active_record, :verbose_query_logs),
20
+ }
21
+ @options = (options || {}).symbolize_keys
22
+ @options[:path] ||= '/dev'
23
+
24
+ Rails.configuration.after_initialize do
25
+ params_load.each do |k, v|
26
+ @params[k.to_sym].set v
27
+ end
28
+ params_reset
29
+ end
30
+ end
31
+
32
+ def call(env)
33
+ req = Rack::Request.new(env)
34
+ return @app.call(env) unless req.path == @options[:path]
35
+
36
+ restart = false
37
+ @actions = []
38
+ if LOGGER_SEVERITY.include?((req.params['log'] || '').downcase)
39
+ level = "Logger::#{req.params['log'].upcase}"
40
+ Rails.logger.level = level.constantize
41
+ @actions.push "Logger level set to: #{level}"
42
+ end
43
+ if req.params.include? 'cache'
44
+ Rails.application.load_tasks
45
+ Rake::Task['dev:cache'].invoke
46
+ @actions.push 'Invoked dev:cache'
47
+ end
48
+ # if req.params.include? 'assets_logs' # NOTE: not working
49
+ # # Rails.configuration.assets.quiet = false
50
+ # params_save(@params[:assets_logs], req.params['assets_logs'] == '1')
51
+ # @actions.push "assets_logs: #{req.params['assets_logs'] == '1'}"
52
+ # restart = true
53
+ # end
54
+ if req.params.include? 'catch_errors'
55
+ params_save(@params[:catch_errors], req.params['catch_errors'] == '1')
56
+ @actions.push "catch_errors: #{req.params['catch_errors'] == '1'}"
57
+ restart = true
58
+ end
59
+ if req.params.include? 'cache_clear'
60
+ Rails.cache.clear
61
+ @actions.push "cache_clear: #{req.params['cache_clear'] == '1'}"
62
+ restart = true
63
+ end
64
+ if req.params.include? 'filter_logs'
65
+ params_save(@params[:filter_logs], req.params['filter_logs'].strip)
66
+ @actions.push "filter logs: #{req.params['filter_logs'].strip}"
67
+ restart = true
68
+ end
69
+ if req.params.include? 'verbose_query_logs'
70
+ params_save(@params[:verbose_query_logs], req.params['verbose_query_logs'] == '1')
71
+ @actions.push "verbose_query_logs: #{req.params['verbose_query_logs'] == '1'}"
72
+ restart = true
73
+ end
74
+ restart = true if req.params.include? 'restart'
75
+ if restart
76
+ restart_server
77
+ @actions.push 'Restarting server'
78
+ end
79
+
80
+ [
81
+ 200,
82
+ { 'Content-Type' => 'text/html' },
83
+ [ERB.new(
84
+ File.read(
85
+ File.join(File.dirname(__FILE__), 'conf.html.erb')
86
+ )
87
+ ).result(binding)]
88
+ ]
89
+ end
90
+
91
+ private
92
+
93
+ def params_load
94
+ JSON.parse Rails.root.join('tmp', '_runtime_config.txt').read
95
+ rescue StandardError
96
+ {}
97
+ end
98
+
99
+ def params_reset
100
+ Rails.root.join('tmp', '_runtime_config.txt').unlink
101
+ rescue StandardError
102
+ nil
103
+ end
104
+
105
+ def params_save(param, value)
106
+ Rails.root.join('tmp', '_runtime_config.txt').open('w') do |f|
107
+ f.puts({ param.opt => value }.to_json)
108
+ end
109
+ end
110
+
111
+ def restart_server
112
+ FileUtils.touch Rails.root.join('tmp', 'restart.txt').to_s
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeConfig
4
+ VERSION = '0.2.0'
5
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: runtime_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Mattia Roccoberton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ description:
34
+ email:
35
+ - mat@blocknot.es
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - README.md
41
+ - lib/runtime_config.rb
42
+ - lib/runtime_config/conf.html.erb
43
+ - lib/runtime_config/conf_param.rb
44
+ - lib/runtime_config/middleware.rb
45
+ - lib/runtime_config/version.rb
46
+ homepage: https://github.com/blocknotes/runtime_config
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.5.2.3
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: A middleware to change configuration parameters at runtime for Rails 5
70
+ test_files: []