Class: Tina4::RackApp
- Inherits:
-
Object
- Object
- Tina4::RackApp
- Defined in:
- lib/tina4/rack_app.rb
Constant Summary collapse
- STATIC_DIRS =
%w[public src/public src/assets assets].freeze
- FRAMEWORK_PUBLIC_DIR =
Framework’s own public directory (bundled static assets like the logo)
File.("public", __dir__).freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#handle(request) ⇒ Object
Dispatch a pre-built Request through the Rack app and return the Rack response triple.
-
#initialize(root_dir: Dir.pwd) ⇒ RackApp
constructor
A new instance of RackApp.
Constructor Details
#initialize(root_dir: Dir.pwd) ⇒ RackApp
Returns a new instance of RackApp.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tina4/rack_app.rb', line 28 def initialize(root_dir: Dir.pwd) @root_dir = root_dir # Pre-compute static roots at boot (not per-request) # Project dirs are checked first; framework's bundled public dir is the fallback project_roots = STATIC_DIRS.map { |d| File.join(root_dir, d) } .select { |d| Dir.exist?(d) } fallback = Dir.exist?(FRAMEWORK_PUBLIC_DIR) ? [FRAMEWORK_PUBLIC_DIR] : [] @static_roots = (project_roots + fallback).freeze # Shared WebSocket engine for route-based WS handling @websocket_engine = Tina4::WebSocket.new end |
Instance Method Details
#call(env) ⇒ Object
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 |
# File 'lib/tina4/rack_app.rb', line 41 def call(env) method = env["REQUEST_METHOD"] path = env["PATH_INFO"] || "/" request_start = Process.clock_gettime(Process::CLOCK_MONOTONIC) # Fast-path: CORS preflight. Real CORS preflight requests carry an # Origin header AND an Access-Control-Request-Method header — the # browser is asking "may I send this method?" before the actual # request. If neither is present, the OPTIONS is a plain protocol- # introspection request (link checker, monitoring probe, RFC 9110 # §9.3.7 OPTIONS) and must fall through to the router's generic # Allow-header response. Otherwise we'd shadow the framework's own # OPTIONS support and force every operator to hand-register CORS # exceptions for every introspection client. if method == "OPTIONS" && (env["HTTP_ORIGIN"] || env["HTTP_ACCESS_CONTROL_REQUEST_METHOD"]) return Tina4::CorsMiddleware.preflight_response(env) end # WebSocket upgrade — match against registered ws_routes if websocket_upgrade?(env) ws_result = Tina4::Router.find_ws_route(path) if ws_result ws_route, ws_params = ws_result return handle_websocket_upgrade(env, ws_route, ws_params) end end # Dev dashboard routes (handled before anything else) if path.start_with?("/__dev") # Block live-reload endpoint on the AI port — AI tools must get stable responses if path == "/__dev_reload" && env["tina4.ai_port"] return [404, { "content-type" => "text/plain" }, ["Not available on AI port"]] end dev_response = Tina4::DevAdmin.handle_request(env) return dev_response if dev_response end # Fast-path: API routes skip static file + swagger checks entirely unless path.start_with?("/api/") # Swagger if path == "/swagger" || path == "/swagger/" return serve_swagger_ui end if path == "/swagger/openapi.json" return serve_openapi_json end # Static files (only for non-API paths) static_response = try_static(path) return static_response if static_response end # Route matching result = Tina4::Router.match(method, path) if result route, path_params = result rack_response = handle_route(env, route, path_params) matched_pattern = route.path else # RFC 9110 conformance — before falling through to 404, check whether # the PATH is known to the router under any OTHER method. # - OPTIONS request → 204 with Allow header (§9.3.7) # - Any other method (PUT on GET-only, TRACE, CONNECT, etc.) # → 405 with Allow header (§15.5.6 + §10.2.1) allowed = Tina4::Router.methods_allowed_for_path(path) if !allowed.empty? allow_header = allowed.join(", ") if method.to_s.upcase == "OPTIONS" rack_response = [204, { "allow" => allow_header, "content-length" => "0" }, [""]] else body = %({"error":"Method Not Allowed","path":"#{path}","method":"#{method}","allow":[#{allowed.map { |m| %("#{m}") }.join(",")}],"status":405}) rack_response = [405, { "allow" => allow_header, "content-type" => "application/json", "content-length" => body.bytesize.to_s }, [body]] end matched_pattern = nil else rack_response = handle_404(path) matched_pattern = nil end end # RFC 9110 §9.3.2: a HEAD response MUST NOT include content. Strip # the body unconditionally and record what Content-Length the GET # would have sent. Cache validators / link checkers / monitoring # probes use that header to estimate sizes. if method.to_s.upcase == "HEAD" status, headers, body_parts = rack_response joined = body_parts.respond_to?(:join) ? body_parts.join : body_parts.to_s unless joined.empty? new_headers = headers.dup new_headers["content-length"] = joined.bytesize.to_s rack_response = [status, new_headers, [""]] end end # Capture request for dev inspector if dev_mode? && !path.start_with?("/__dev") duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - request_start) * 1000).round(3) Tina4::DevAdmin.request_inspector.capture( method: method, path: path, status: rack_response[0], duration: duration_ms ) end # Inject dev overlay button for HTML responses in dev mode if dev_mode? && !path.start_with?("/__dev") status, headers, body_parts = rack_response content_type = headers["content-type"] || "" if content_type.include?("text/html") request_info = { method: method, path: path, matched_pattern: matched_pattern || "(no match)", } joined = body_parts.join = (joined, request_info, ai_port: env["tina4.ai_port"]) rack_response = [status, headers, []] end end # Save session and set cookie if session was used if result && defined?(rack_response) status, headers, body_parts = rack_response request_obj = env["tina4.request"] if request_obj&.instance_variable_get(:@session) sess = request_obj.session sess.save # Probabilistic garbage collection (~1% of requests) if rand(1..100) == 1 begin sess.gc rescue StandardError # GC failure is non-critical — silently ignore end end sid = sess.id = (env["HTTP_COOKIE"] || "")[/tina4_session=([^;]+)/, 1] if sid && sid != ttl = Integer(ENV.fetch("TINA4_SESSION_TTL", 3600)) headers["set-cookie"] = "tina4_session=#{sid}; Path=/; HttpOnly; SameSite=Lax; Max-Age=#{ttl}" end rack_response = [status, headers, body_parts] end end rack_response rescue => e handle_500(e, env) end |
#handle(request) ⇒ Object
Dispatch a pre-built Request through the Rack app and return the Rack response triple. Useful for testing and embedding without starting an HTTP server.
200 201 202 203 204 |
# File 'lib/tina4/rack_app.rb', line 200 def handle(request) env = request.env env["rack.input"].rewind if env["rack.input"].respond_to?(:rewind) call(env) end |