Skip to content

Commit db5ca77

Browse files
committed
move core operation endpoints to their own files
1 parent 18d03db commit db5ca77

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

lib/hooks/app/api.rb

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,31 @@
99
require_relative "../core/logger_factory"
1010
require_relative "../core/log"
1111

12+
# import all core endpoint classes dynamically
13+
Dir[File.join(__dir__, "endpoints/**/*.rb")].sort.each do |file|
14+
require file
15+
end
16+
1217
module Hooks
1318
module App
1419
# Factory for creating configured Grape API classes
1520
class API
1621
include Hooks::App::Helpers
1722

23+
# Expose start_time for endpoint modules
24+
def self.start_time
25+
@start_time
26+
end
27+
1828
# Create a new configured API class
1929
def self.create(config:, endpoints:, log:)
2030
# Store startup time for uptime calculation
21-
start_time = Time.now
31+
@start_time = Time.now
2232

2333
# Capture values in local variables for closure
2434
captured_config = config
2535
captured_endpoints = endpoints
2636
captured_logger = log
27-
captured_start_time = start_time
2837

2938
# Set global logger instance for plugins/validators
3039
Hooks::Log.instance = log
@@ -45,34 +54,9 @@ def self.create(config:, endpoints:, log:)
4554
# Define helper methods first, before routes
4655
helpers Helpers
4756

48-
# Define operational endpoints
49-
get captured_config[:health_path] do
50-
content_type "application/json"
51-
{
52-
status: "healthy",
53-
timestamp: Time.now.iso8601,
54-
version: Hooks::VERSION,
55-
uptime_seconds: (Time.now - captured_start_time).to_i
56-
}.to_json
57-
end
58-
59-
get captured_config[:version_path] do
60-
content_type "application/json"
61-
{
62-
version: Hooks::VERSION,
63-
timestamp: Time.now.iso8601
64-
}.to_json
65-
end
66-
67-
# Hello world demo endpoint
68-
get "#{captured_config[:root_path]}/hello" do
69-
content_type "application/json"
70-
{
71-
message: "hooks is working!",
72-
version: Hooks::VERSION,
73-
timestamp: Time.now.iso8601
74-
}.to_json
75-
end
57+
# Mount split-out endpoints
58+
mount Hooks::App::HealthEndpoint => config[:health_path]
59+
mount Hooks::App::VersionEndpoint => config[:version_path]
7660

7761
# Define webhook endpoints dynamically
7862
captured_endpoints.each do |endpoint_config|
@@ -82,7 +66,6 @@ def self.create(config:, endpoints:, log:)
8266
# Use send to dynamically create POST route
8367
send(:post, full_path) do
8468
request_id = SecureRandom.uuid
85-
start_time = Time.now
8669

8770
# Use captured values
8871
config = captured_config
@@ -161,7 +144,6 @@ def self.create(config:, endpoints:, log:)
161144
if captured_config[:use_catchall_route]
162145
post "#{captured_config[:root_path]}/*path" do
163146
request_id = SecureRandom.uuid
164-
start_time = Time.now
165147

166148
# Use captured values
167149
config = captured_config

lib/hooks/app/endpoints/health.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require "grape"
4+
require_relative "../../version"
5+
6+
module Hooks
7+
module App
8+
class HealthEndpoint < Grape::API
9+
get do
10+
content_type "application/json"
11+
{
12+
status: "healthy",
13+
timestamp: Time.now.iso8601,
14+
version: Hooks::VERSION,
15+
uptime_seconds: (Time.now - Hooks::App::API.start_time).to_i
16+
}.to_json
17+
end
18+
end
19+
end
20+
end

lib/hooks/app/endpoints/version.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require "grape"
4+
require_relative "../../version"
5+
6+
module Hooks
7+
module App
8+
class VersionEndpoint < Grape::API
9+
get do
10+
content_type "application/json"
11+
{
12+
version: Hooks::VERSION,
13+
timestamp: Time.now.iso8601
14+
}.to_json
15+
end
16+
end
17+
end
18+
end

spec/integration/hooks_integration_spec.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,6 @@ def call(payload:, headers:, config:)
8080
end
8181
end
8282

83-
describe "hello world endpoint" do
84-
it "responds to hello endpoint" do
85-
get "/webhooks/hello"
86-
expect(last_response.status).to eq(200)
87-
88-
body = JSON.parse(last_response.body)
89-
expect(body["message"]).to eq("hooks is working!")
90-
expect(body["version"]).to eq(Hooks::VERSION)
91-
expect(body).to have_key("timestamp")
92-
end
93-
end
94-
9583
describe "webhook endpoints" do
9684
it "processes JSON webhook with custom handler" do
9785
payload = { event: "test_event", data: "test_data" }

0 commit comments

Comments
 (0)