Class: Otto

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Core::Configuration, Core::ErrorHandler, Core::FileSafety, Core::HelperRegistry, Core::LifecycleHooks, Core::MiddlewareManagement, Core::Router, Core::UriGenerator, MCP::Core, Privacy::Core, Security::Core
Defined in:
lib/otto.rb,
lib/otto/route.rb,
lib/otto/utils.rb,
lib/otto/errors.rb,
lib/otto/locale.rb,
lib/otto/static.rb,
lib/otto/privacy.rb,
lib/otto/request.rb,
lib/otto/version.rb,
lib/otto/env_keys.rb,
lib/otto/mcp/core.rb,
lib/otto/response.rb,
lib/otto/mcp/server.rb,
lib/otto/core/router.rb,
lib/otto/helpers/base.rb,
lib/otto/mcp/protocol.rb,
lib/otto/mcp/registry.rb,
lib/otto/privacy/core.rb,
lib/otto/design_system.rb,
lib/otto/locale/config.rb,
lib/otto/security/core.rb,
lib/otto/security/csrf.rb,
lib/otto/core/freezable.rb,
lib/otto/mcp/auth/token.rb,
lib/otto/privacy/config.rb,
lib/otto/route_handlers.rb,
lib/otto/logging_helpers.rb,
lib/otto/security/config.rb,
lib/otto/core/file_safety.rb,
lib/otto/mcp/route_parser.rb,
lib/otto/route_definition.rb,
lib/otto/locale/middleware.rb,
lib/otto/mcp/rate_limiting.rb,
lib/otto/response_handlers.rb,
lib/otto/core/configuration.rb,
lib/otto/core/error_handler.rb,
lib/otto/core/uri_generator.rb,
lib/otto/helpers/validation.rb,
lib/otto/privacy/ip_privacy.rb,
lib/otto/security/validator.rb,
lib/otto/route_handlers/base.rb,
lib/otto/core/helper_registry.rb,
lib/otto/core/lifecycle_hooks.rb,
lib/otto/privacy/geo_resolver.rb,
lib/otto/core/middleware_stack.rb,
lib/otto/mcp/schema_validation.rb,
lib/otto/route_handlers/lambda.rb,
lib/otto/security/configurator.rb,
lib/otto/security/rate_limiter.rb,
lib/otto/response_handlers/auto.rb,
lib/otto/response_handlers/base.rb,
lib/otto/response_handlers/json.rb,
lib/otto/response_handlers/view.rb,
lib/otto/route_handlers/factory.rb,
lib/otto/security/rate_limiting.rb,
lib/otto/security/authentication.rb,
lib/otto/response_handlers/default.rb,
lib/otto/response_handlers/factory.rb,
lib/otto/core/middleware_management.rb,
lib/otto/response_handlers/redirect.rb,
lib/otto/route_handlers/logic_class.rb,
lib/otto/route_handlers/class_method.rb,
lib/otto/privacy/redacted_fingerprint.rb,
lib/otto/security/authorization_error.rb,
lib/otto/route_handlers/instance_method.rb,
lib/otto/security/middleware/csrf_middleware.rb,
lib/otto/security/authentication/auth_failure.rb,
lib/otto/security/authentication/auth_strategy.rb,
lib/otto/security/authentication/strategy_result.rb,
lib/otto/security/middleware/ip_privacy_middleware.rb,
lib/otto/security/middleware/rate_limit_middleware.rb,
lib/otto/security/middleware/validation_middleware.rb,
lib/otto/security/authentication/route_auth_wrapper.rb,
lib/otto/security/authentication/strategies/role_strategy.rb,
lib/otto/security/authentication/strategies/noauth_strategy.rb,
lib/otto/security/authentication/strategies/api_key_strategy.rb,
lib/otto/security/authentication/strategies/session_strategy.rb,
lib/otto/security/authentication/strategies/permission_strategy.rb,
lib/otto/security/authentication/route_auth_wrapper/response_builder.rb,
lib/otto/security/authentication/route_auth_wrapper/strategy_resolver.rb,
lib/otto/security/authentication/route_auth_wrapper/role_authorization.rb

Overview

lib/otto/security/middleware/ip_privacy_middleware.rb

frozen_string_literal: true

Defined Under Namespace

Modules: BaseHelpers, ClassMethods, Core, DesignSystem, EnvKeys, Locale, LoggingHelpers, MCP, Privacy, ResponseHandlers, RouteHandlers, Security, Static, Utils Classes: BadRequestError, ForbiddenError, HTTPError, NotFoundError, PayloadTooLargeError, Request, Response, Route, RouteDefinition, UnauthorizedError

Constant Summary collapse

LIB_HOME =
__dir__
VERSION =
'2.0.1'
StrategyResult =

Top-level backward compatibility aliases

Security::Authentication::StrategyResult
AuthFailure =
Security::Authentication::AuthFailure

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

default, env?, load, path, unfreeze_for_testing

Methods included from MCP::Core

#enable_mcp!, #mcp_enabled?

Methods included from Privacy::Core

#configure_ip_privacy, #disable_ip_privacy!, #enable_full_ip_privacy!

Methods included from Security::Core

#add_auth_strategy, #add_rate_limit_rule, #add_trusted_proxy, #enable_csp!, #enable_csp_with_nonce!, #enable_csrf_protection!, #enable_frame_protection!, #enable_hsts!, #enable_rate_limiting!, #enable_request_validation!, #set_security_headers

Methods included from Core::LifecycleHooks

#on_request_complete, #request_complete_callbacks

Methods included from Core::MiddlewareManagement

#build_app!, #middleware_enabled?, #middleware_stack, #middleware_stack=, #use

Methods included from Core::HelperRegistry

#register_request_helpers, #register_response_helpers, #registered_request_helpers, #registered_response_helpers

Methods included from Core::UriGenerator

#uri

Methods included from Core::ErrorHandler

#handle_error, #register_error_handler

Methods included from Core::Configuration

#configure, #configure_auth_strategies, #configure_authentication, #configure_locale, #configure_mcp, #configure_rate_limiting, #configure_security, #ensure_not_frozen!, #freeze_configuration!, #frozen_configuration?, #middleware_enabled?

Methods included from Core::Freezable

#deep_freeze!

Methods included from Core::FileSafety

#add_static_path, #safe_dir?, #safe_file?

Methods included from Core::Router

#determine_locale, #handle_request, #load

Constructor Details

#initialize(path = nil, opts = {}) ⇒ Otto

Returns a new instance of Otto.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/otto.rb', line 82

def initialize(path = nil, opts = {})
  initialize_core_state
  initialize_options(path, opts)
  initialize_configurations(opts)

  Otto.logger.debug "new Otto: #{opts}" if Otto.debug
  load(path) unless path.nil?
  super()

  # Auto-register all Otto framework error classes
  # This allows Logic classes and framework code to raise appropriate errors
  # without requiring manual registration in implementing projects
  register_framework_errors

  # Build the middleware app once after all initialization is complete
  build_app!

  # Configuration freezing is deferred until first request to support
  # multi-step initialization (e.g., multi-app architectures).
  # This allows adding auth strategies, middleware, etc. after Otto.new
  # but before processing requests.
  @freeze_mutex = Mutex.new
  @configuration_frozen = false
end

Class Attribute Details

.debugObject

rubocop:disable ThreadSafety/ClassAndModuleAttributes



215
216
217
# File 'lib/otto.rb', line 215

def debug
  @debug
end

.loggerObject

rubocop:disable ThreadSafety/ClassAndModuleAttributes



215
216
217
# File 'lib/otto.rb', line 215

def logger
  @logger
end

Instance Attribute Details

#auth_configObject (readonly)

Returns the value of attribute auth_config.



76
77
78
# File 'lib/otto.rb', line 76

def auth_config
  @auth_config
end

#error_handlersObject (readonly)

Returns the value of attribute error_handlers.



76
77
78
# File 'lib/otto.rb', line 76

def error_handlers
  @error_handlers
end

#locale_configObject (readonly)

Returns the value of attribute locale_config.



76
77
78
# File 'lib/otto.rb', line 76

def locale_config
  @locale_config
end

#mcp_serverObject (readonly)

Returns the value of attribute mcp_server.



76
77
78
# File 'lib/otto.rb', line 76

def mcp_server
  @mcp_server
end

#middlewareObject (readonly)

Returns the value of attribute middleware.



76
77
78
# File 'lib/otto.rb', line 76

def middleware
  @middleware
end

#not_foundObject

Returns the value of attribute not_found.



80
81
82
# File 'lib/otto.rb', line 80

def not_found
  @not_found
end

#optionObject (readonly) Also known as: options

Returns the value of attribute option.



76
77
78
# File 'lib/otto.rb', line 76

def option
  @option
end

#request_classObject (readonly)

Returns the value of attribute request_class.



76
77
78
# File 'lib/otto.rb', line 76

def request_class
  @request_class
end

#response_classObject (readonly)

Returns the value of attribute response_class.



76
77
78
# File 'lib/otto.rb', line 76

def response_class
  @response_class
end

#route_definitionsObject (readonly)

Returns the value of attribute route_definitions.



76
77
78
# File 'lib/otto.rb', line 76

def route_definitions
  @route_definitions
end

#route_handler_factoryObject (readonly)

Returns the value of attribute route_handler_factory.



76
77
78
# File 'lib/otto.rb', line 76

def route_handler_factory
  @route_handler_factory
end

#routesObject (readonly)

Returns the value of attribute routes.



76
77
78
# File 'lib/otto.rb', line 76

def routes
  @routes
end

#routes_literalObject (readonly)

Returns the value of attribute routes_literal.



76
77
78
# File 'lib/otto.rb', line 76

def routes_literal
  @routes_literal
end

#routes_staticObject (readonly)

Returns the value of attribute routes_static.



76
77
78
# File 'lib/otto.rb', line 76

def routes_static
  @routes_static
end

#securityObject (readonly)

Returns the value of attribute security.



76
77
78
# File 'lib/otto.rb', line 76

def security
  @security
end

#security_configObject (readonly)

Returns the value of attribute security_config.



76
77
78
# File 'lib/otto.rb', line 76

def security_config
  @security_config
end

#server_errorObject

Returns the value of attribute server_error.



80
81
82
# File 'lib/otto.rb', line 80

def server_error
  @server_error
end

#static_routeObject (readonly)

Returns the value of attribute static_route.



76
77
78
# File 'lib/otto.rb', line 76

def static_route
  @static_route
end

Class Method Details

.structured_log(level, message, data = {}) ⇒ Object

Helper method for structured logging that works with both standard Logger and structured loggers



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/otto.rb', line 218

def structured_log(level, message, data = {})
  return unless logger

  # Skip debug logging when Otto.debug is false
  return if level == :debug && !debug

  # Sanitize backtrace if present
  if data.is_a?(Hash) && data[:backtrace].is_a?(Array)
    data = data.dup
    data[:backtrace] = Otto::LoggingHelpers.sanitize_backtrace(data[:backtrace])
  end

  # Try structured logging first (SemanticLogger, etc.)
  if logger.respond_to?(level) && logger.method(level).arity > 1
    logger.send(level, message, data)
  else
    # Fallback to standard logger with formatted string
    formatted_data = data.empty? ? '' : " -- #{data.inspect}"
    logger.send(level, "[Otto] #{message}#{formatted_data}")
  end
end

Instance Method Details

#call(env) ⇒ Object

Main Rack application interface



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
# File 'lib/otto.rb', line 109

def call(env)
  # Freeze configuration on first request (thread-safe)
  # Skip in test environment to allow test flexibility
  unless defined?(RSpec) || @configuration_frozen
    Otto.logger.debug '[Otto] Lazy freezing check: configuration not yet frozen' if Otto.debug

    @freeze_mutex.synchronize do
      unless @configuration_frozen
        Otto.logger.info '[Otto] Freezing configuration on first request (lazy freeze)'
        freeze_configuration!
        @configuration_frozen = true
        Otto.logger.debug '[Otto] Configuration frozen successfully' if Otto.debug
      end
    end
  end

  # Track request timing for lifecycle hooks
  start_time = Otto::Utils.now_in_μs
  request = @request_class.new(env)
  response_raw = nil

  begin
    # Use pre-built middleware app (built once at initialization)
    response_raw = @app.call(env)
  rescue StandardError => e
    response_raw = handle_error(e, env)
  ensure
    # Execute request completion hooks if any are registered
    unless @request_complete_callbacks.empty?
      begin
        duration = Otto::Utils.now_in_μs - start_time
        # Wrap response tuple in Otto::Response for developer-friendly API
        # Otto's hook API should provide nice abstractions like Otto::Request/Response
        response = @response_class.new(response_raw[2], response_raw[0], response_raw[1])
        @request_complete_callbacks.each do |callback|
          callback.call(request, response, duration)
        end
      rescue StandardError => e
        Otto.logger.error "[Otto] Request completion hook error: #{e.message}"
        Otto.logger.debug "[Otto] Hook error backtrace: #{e.backtrace.join("\n")}" if Otto.debug
      end
    end
  end

  response_raw
end