Class: Tina4::RackApp

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/rack_app.rb

Constant Summary collapse

STATIC_DIRS =
%w[public src/public src/assets assets].freeze
CORS_HEADERS =

Pre-built frozen responses for zero-allocation fast paths

{
  "access-control-allow-origin" => "*",
  "access-control-allow-methods" => "GET, POST, PUT, PATCH, DELETE, OPTIONS",
  "access-control-allow-headers" => "Content-Type, Authorization, Accept",
  "access-control-max-age" => "86400"
}.freeze
OPTIONS_RESPONSE =
[204, CORS_HEADERS, [""]].freeze

Instance Method Summary collapse

Constructor Details

#initialize(root_dir: Dir.pwd) ⇒ RackApp

Returns a new instance of RackApp.



18
19
20
21
22
23
24
# File 'lib/tina4/rack_app.rb', line 18

def initialize(root_dir: Dir.pwd)
  @root_dir = root_dir
  # Pre-compute static roots at boot (not per-request)
  @static_roots = STATIC_DIRS.map { |d| File.join(root_dir, d) }
                              .select { |d| Dir.exist?(d) }
                              .freeze
end

Instance Method Details

#call(env) ⇒ Object



26
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
# File 'lib/tina4/rack_app.rb', line 26

def call(env)
  method = env["REQUEST_METHOD"]
  path = env["PATH_INFO"] || "/"

  # Fast-path: OPTIONS preflight (zero allocation)
  return OPTIONS_RESPONSE if method == "OPTIONS"

  # 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.find_route(path, method)
  if result
    route, path_params = result
    handle_route(env, route, path_params)
  else
    handle_404(path)
  end
rescue => e
  handle_500(e)
end