Skip to content

Commit 19522b5

Browse files
committed
move catchall logic out
1 parent b4854ff commit 19522b5

2 files changed

Lines changed: 84 additions & 64 deletions

File tree

lib/hooks/app/api.rb

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -142,70 +142,9 @@ def self.create(config:, endpoints:, log:)
142142
# Catch-all route for unknown endpoints - use default handler
143143
# Only create if explicitly enabled in config
144144
if captured_config[:use_catchall_route]
145-
post "#{captured_config[:root_path]}/*path" do
146-
request_id = SecureRandom.uuid
147-
148-
# Use captured values
149-
config = captured_config
150-
log = captured_logger
151-
152-
# Set request context for logging
153-
request_context = {
154-
request_id:,
155-
path: "/#{params[:path]}",
156-
handler: "DefaultHandler"
157-
}
158-
159-
Core::LogContext.with(request_context) do
160-
begin
161-
# Enforce request limits
162-
enforce_request_limits(config)
163-
164-
# Get raw body for payload parsing
165-
request.body.rewind
166-
raw_body = request.body.read
167-
168-
# Parse payload
169-
payload = parse_payload(raw_body, headers)
170-
171-
# Use default handler
172-
handler = DefaultHandler.new
173-
174-
# Call handler
175-
response = handler.call(
176-
payload: payload,
177-
headers: headers,
178-
config: {}
179-
)
180-
181-
log.info "request processed successfully with default handler (id: #{request_id})"
182-
183-
# Return response as JSON string when using txt format
184-
status 200
185-
content_type "application/json"
186-
(response || { status: "ok" }).to_json
187-
188-
rescue StandardError => e
189-
log.error "request failed: #{e.message} (id: #{request_id})"
190-
191-
# Return error response
192-
error_response = {
193-
error: e.message,
194-
code: determine_error_code(e),
195-
request_id:
196-
}
197-
198-
# Add backtrace in all environments except production
199-
unless config[:production] == true
200-
error_response[:backtrace] = e.backtrace
201-
end
202-
203-
status error_response[:code]
204-
content_type "application/json"
205-
error_response.to_json
206-
end
207-
end
208-
end
145+
route_path = Hooks::App::CatchallEndpoint.mount_path(captured_config)
146+
route_block = Hooks::App::CatchallEndpoint.route_block(captured_config, captured_logger)
147+
post(route_path, &route_block)
209148
end
210149
end
211150

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# frozen_string_literal: true
2+
3+
require "grape"
4+
require_relative "../../handlers/default"
5+
6+
module Hooks
7+
module App
8+
class CatchallEndpoint < Grape::API
9+
def self.mount_path(config)
10+
"#{config[:root_path]}/*path"
11+
end
12+
13+
def self.route_block(captured_config, captured_logger)
14+
proc do
15+
request_id = SecureRandom.uuid
16+
17+
# Use captured values
18+
config = captured_config
19+
log = captured_logger
20+
21+
# Set request context for logging
22+
request_context = {
23+
request_id: request_id,
24+
path: "/#{params[:path]}",
25+
handler: "DefaultHandler"
26+
}
27+
28+
Hooks::Core::LogContext.with(request_context) do
29+
begin
30+
# Enforce request limits
31+
enforce_request_limits(config)
32+
33+
# Get raw body for payload parsing
34+
request.body.rewind
35+
raw_body = request.body.read
36+
37+
# Parse payload
38+
payload = parse_payload(raw_body, headers)
39+
40+
# Use default handler
41+
handler = DefaultHandler.new
42+
43+
# Call handler
44+
response = handler.call(
45+
payload: payload,
46+
headers: headers,
47+
config: {}
48+
)
49+
50+
log.info "request processed successfully with default handler (id: #{request_id})"
51+
52+
# Return response as JSON string when using txt format
53+
status 200
54+
content_type "application/json"
55+
(response || { status: "ok" }).to_json
56+
57+
rescue StandardError => e
58+
log.error "request failed: #{e.message} (id: #{request_id})"
59+
60+
# Return error response
61+
error_response = {
62+
error: e.message,
63+
code: determine_error_code(e),
64+
request_id: request_id
65+
}
66+
67+
# Add backtrace in all environments except production
68+
unless config[:production] == true
69+
error_response[:backtrace] = e.backtrace
70+
end
71+
72+
status error_response[:code]
73+
content_type "application/json"
74+
error_response.to_json
75+
end
76+
end
77+
end
78+
end
79+
end
80+
end
81+
end

0 commit comments

Comments
 (0)