Class: Itsi::Server::Config::DSL
- Inherits:
-
Object
- Object
- Itsi::Server::Config::DSL
- Defined in:
- lib/itsi/server/config/dsl.rb
Instance Attribute Summary collapse
-
#accepts ⇒ Object
readonly
Returns the value of attribute accepts.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#content_types ⇒ Object
readonly
Returns the value of attribute content_types.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
-
#http_methods ⇒ Object
readonly
Returns the value of attribute http_methods.
-
#middleware ⇒ Object
readonly
Returns the value of attribute middleware.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#ports ⇒ Object
readonly
Returns the value of attribute ports.
-
#protocols ⇒ Object
readonly
Returns the value of attribute protocols.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
-
.evaluate(config = Itsi::Server::Config.config_file_path, &blk) ⇒ Object
rubocop:disable Metrics/MethodLength.
Instance Method Summary collapse
- #deep_stringify_keys(obj) ⇒ Object
- #effective_middleware ⇒ Object
- #errors ⇒ Object
- #file_server(**args) ⇒ Object
- #flatten_routes ⇒ Object
- #grpc_reflection(handlers) ⇒ Object
-
#initialize(parent = nil, routes: [], methods: [], protocols: [], hosts: [], ports: [], extensions: [], content_types: [], accepts: [], controller: self, &block) ⇒ DSL
constructor
rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity.
- #paths_from_parent ⇒ Object
Constructor Details
#initialize(parent = nil, routes: [], methods: [], protocols: [], hosts: [], ports: [], extensions: [], content_types: [], accepts: [], controller: self, &block) ⇒ DSL
rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity
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 |
# File 'lib/itsi/server/config/dsl.rb', line 27 def initialize( # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity parent = nil, routes: [], methods: [], protocols: [], hosts: [], ports: [], extensions: [], content_types: [], accepts: [], controller: self, &block ) @parent = parent @children = [] @middleware = {} @controller = controller @routes = Array(routes).flatten @http_methods = methods.map { |s| s.is_a?(Regexp) ? s : s.to_s } @protocols = protocols.map { |s| s.is_a?(Regexp) ? s : s.to_s } @hosts = hosts.map { |s| s.is_a?(Regexp) ? s : s.to_s } @ports = ports.map { |s| s.is_a?(Regexp) ? s : s.to_s } @extensions = extensions.map { |s| s.is_a?(Regexp) ? s : s.to_s } @content_types = content_types.map { |s| s.is_a?(Regexp) ? s : s.to_s } @accepts = accepts.map { |s| s.is_a?(Regexp) ? s : s.to_s } @options = { nested_locations: [], middleware_loader: lambda do @options[:nested_locations].each(&:call) @middleware[:app] ||= {} @middleware[:app][:app_proc] = @middleware[:app]&.[](:preloader)&.call || DEFAULT_APP[] [flatten_routes, Config.errors_to_error_lines(errors)] end } @errors = [] instance_exec(&block) end |
Instance Attribute Details
#accepts ⇒ Object (readonly)
Returns the value of attribute accepts.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def accepts @accepts end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def children @children end |
#content_types ⇒ Object (readonly)
Returns the value of attribute content_types.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def content_types @content_types end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def controller @controller end |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def extensions @extensions end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def hosts @hosts end |
#http_methods ⇒ Object (readonly)
Returns the value of attribute http_methods.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def http_methods @http_methods end |
#middleware ⇒ Object (readonly)
Returns the value of attribute middleware.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def middleware @middleware end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def parent @parent end |
#ports ⇒ Object (readonly)
Returns the value of attribute ports.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def ports @ports end |
#protocols ⇒ Object (readonly)
Returns the value of attribute protocols.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def protocols @protocols end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
9 10 11 |
# File 'lib/itsi/server/config/dsl.rb', line 9 def routes @routes end |
Class Method Details
.evaluate(config = Itsi::Server::Config.config_file_path, &blk) ⇒ Object
rubocop:disable Metrics/MethodLength
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/itsi/server/config/dsl.rb', line 12 def self.evaluate(config = Itsi::Server::Config.config_file_path, &blk) # rubocop:disable Metrics/MethodLength config = new(routes: ["/"]) do if blk instance_exec(&blk) else code = IO.read(config) instance_eval(code, config.to_s, 1) end location("*") {} end [config., config.errors] rescue Exception => e # rubocop:disable Lint/RescueException [{}, [[e, e.backtrace[0]]]] end |
Instance Method Details
#deep_stringify_keys(obj) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/itsi/server/config/dsl.rb', line 192 def deep_stringify_keys(obj) case obj when Hash obj.transform_keys!(&:to_s) obj.transform_values! { |v| deep_stringify_keys(v) } when Array obj.map { |v| deep_stringify_keys(v) } else obj end end |
#effective_middleware ⇒ Object
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 |
# File 'lib/itsi/server/config/dsl.rb', line 166 def effective_middleware chain = [] node = self while node if node.middleware[:app]&.[](:preloader) node.middleware[:app][:app_proc] = node.middleware[:app].delete(:preloader).call end chain << node node = node.parent end chain.reverse! merged = {} chain.each do |n| n.middleware.each do |k, v| merged[k] = if v[:combine] ([merged[k] || []] + [v]).flatten else v end end end deep_stringify_keys(merged) end |
#errors ⇒ Object
68 69 70 |
# File 'lib/itsi/server/config/dsl.rb', line 68 def errors @children.map(&:errors).flatten(1) + @errors end |
#file_server(**args) ⇒ Object
104 105 106 107 |
# File 'lib/itsi/server/config/dsl.rb', line 104 def file_server(**args) Itsi.log_info "Note: file_server is an alias for static_assets" static_assets(**args) end |
#flatten_routes ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/itsi/server/config/dsl.rb', line 109 def flatten_routes result = [] result.concat(@children.flat_map(&:flatten_routes)) = paths_from_parent if result << deep_stringify_keys( { route: Regexp.new("^#{}/?$"), methods: @http_methods.any? ? @http_methods : nil, protocols: @protocols.any? ? @protocols : nil, hosts: @hosts.any? ? @hosts : nil, ports: @ports.any? ? @ports : nil, extensions: @extensions.any? ? @extensions : nil, content_types: @content_types.any? ? @content_types : nil, accepts: @accepts.any? ? @accepts : nil, middleware: effective_middleware } ) end result end |
#grpc_reflection(handlers) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/itsi/server/config/dsl.rb', line 92 def grpc_reflection(handlers) @grpc_reflected_services ||= [] @grpc_reflected_services.concat(handlers) location("grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo", "grpc.reflection.v1.ServerReflection/ServerReflectionInfo") do @middleware[:app] = { preloader: lambda { Itsi::Server::GrpcInterface.reflection_for(handlers) }, request_type: "grpc" } end end |
#paths_from_parent ⇒ Object
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 |
# File 'lib/itsi/server/config/dsl.rb', line 131 def paths_from_parent return nil unless @routes.any? route_or_str = @routes.map do |seg| case seg when Regexp seg.source else parts = seg.split("/") parts.map do |part| case part when /^:([A-Za-z_]\w*)(?:\(([^)]*)\))?$/ param_name = Regexp.last_match(1) custom = Regexp.last_match(2) if custom && !custom.empty? "(?<#{param_name}>#{custom})" else "(?<#{param_name}>[^/]+)" end when /\*/ part.gsub(/\*/, ".*") else Regexp.escape(part) end end.join("/") end end.join("|") if parent && parent.paths_from_parent && parent.paths_from_parent != "(?:/)" "#{parent.paths_from_parent}#{route_or_str != "" ? "(?:#{route_or_str})" : ""}" else route_or_str = "/#{route_or_str}" unless route_or_str.start_with?("/") "(?:#{route_or_str})" end end |