Skip to content

Commit bc16469

Browse files
tac0turtlegoogle-labs-jules[bot]tac0turtle
authored
refactor: custom HTTP endpoint registration (#2380)
## Overview This commit introduces a new file `pkg/rpc/server/http.go` to provide a centralized location for registering custom, non-gRPC, plain HTTP endpoints. A new function `RegisterCustomHTTPEndpoints(mux *http.ServeMux)` is defined in this file. The existing `/health/live` endpoint has been moved into this function. The `NewServiceHandler` in `pkg/rpc/server/server.go` now calls `RegisterCustomHTTPEndpoints` to include these custom routes. This change helps to keep the `server.go` file cleaner and makes it easier to manage and add new custom HTTP endpoints in the future. A test file `pkg/rpc/server/http_test.go` has been added with `TestRegisterCustomHTTPEndpoints` to ensure that endpoints registered via this new structure are correctly served. <!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. NOTE: PR titles should follow semantic commits: https://www.conventionalcommits.org/en/v1.0.0/ --> closes #2375 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new liveness health check endpoint at /health/live, returning a 200 OK status and "OK" response. - **Tests** - Added tests to verify the correct registration and response of the /health/live endpoint. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: tac0turtle <you@example.com>
1 parent 2cc7e0b commit bc16469

5 files changed

Lines changed: 100 additions & 0 deletions

File tree

pkg/rpc/server/http.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package server
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
// RegisterCustomHTTPEndpoints is the designated place to add new, non-gRPC, plain HTTP handlers.
9+
// Additional custom HTTP endpoints can be registered on the mux here.
10+
func RegisterCustomHTTPEndpoints(mux *http.ServeMux) {
11+
mux.HandleFunc("/health/live", func(w http.ResponseWriter, r *http.Request) {
12+
w.Header().Set("Content-Type", "text/plain")
13+
w.WriteHeader(http.StatusOK)
14+
fmt.Fprintln(w, "OK")
15+
})
16+
17+
// Example for adding more custom endpoints:
18+
// mux.HandleFunc("/custom/myendpoint", func(w http.ResponseWriter, r *http.Request) {
19+
// // Your handler logic here
20+
// w.WriteHeader(http.StatusOK)
21+
// fmt.Fprintln(w, "My custom endpoint!")
22+
// })
23+
}

pkg/rpc/server/http_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package server
2+
3+
import (
4+
"io"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestRegisterCustomHTTPEndpoints(t *testing.T) {
13+
// Create a new ServeMux
14+
mux := http.NewServeMux()
15+
16+
// Register custom HTTP endpoints
17+
RegisterCustomHTTPEndpoints(mux)
18+
19+
// Create a new HTTP test server with the mux
20+
testServer := httptest.NewServer(mux)
21+
defer testServer.Close()
22+
23+
// Make an HTTP GET request to the /health/live endpoint
24+
resp, err := http.Get(testServer.URL + "/health/live")
25+
assert.NoError(t, err)
26+
defer resp.Body.Close()
27+
28+
// Check the status code
29+
assert.Equal(t, http.StatusOK, resp.StatusCode)
30+
31+
// Read the response body
32+
body, err := io.ReadAll(resp.Body)
33+
assert.NoError(t, err)
34+
35+
// Check the response body content
36+
assert.Equal(t, "OK\n", string(body)) // fmt.Fprintln adds a newline
37+
}

pkg/rpc/server/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ func NewServiceHandler(store store.Store, peerManager p2p.P2PRPC) (http.Handler,
237237
healthPath, healthHandler := rpc.NewHealthServiceHandler(healthServer)
238238
mux.Handle(healthPath, healthHandler)
239239

240+
// Register custom HTTP endpoints
241+
RegisterCustomHTTPEndpoints(mux)
242+
240243
// Use h2c to support HTTP/2 without TLS
241244
return h2c.NewHandler(mux, &http2.Server{
242245
IdleTimeout: 120 * time.Second,

pkg/rpc/server/server_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package server
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"net/http"
8+
"net/http/httptest"
69
"testing"
710
"time"
811

@@ -220,3 +223,33 @@ func TestHealthServer_Livez(t *testing.T) {
220223
require.NoError(t, err)
221224
require.Equal(t, pb.HealthStatus_PASS, resp.Msg.Status)
222225
}
226+
227+
func TestHealthLiveEndpoint(t *testing.T) {
228+
assert := require.New(t)
229+
230+
// Create mock dependencies
231+
mockStore := mocks.NewStore(t)
232+
mockP2PManager := &mocks.P2PRPC{} // Assuming this mock is sufficient or can be adapted
233+
234+
// Create the service handler
235+
handler, err := NewServiceHandler(mockStore, mockP2PManager)
236+
assert.NoError(err)
237+
assert.NotNil(handler)
238+
239+
// Create a new HTTP test server
240+
server := httptest.NewServer(handler)
241+
defer server.Close()
242+
243+
// Make a GET request to the /health/live endpoint
244+
resp, err := http.Get(server.URL + "/health/live")
245+
assert.NoError(err)
246+
defer resp.Body.Close()
247+
248+
// Check the status code
249+
assert.Equal(http.StatusOK, resp.StatusCode)
250+
251+
// Check the response body
252+
body, err := io.ReadAll(resp.Body)
253+
assert.NoError(err)
254+
assert.Equal("OK\n", string(body)) // fmt.Fprintln adds a newline
255+
}

test/docker-e2e/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package docker_e2e provides end-to-end tests for the Docker integration.
3+
*/
4+
package docker_e2e

0 commit comments

Comments
 (0)