-
Notifications
You must be signed in to change notification settings - Fork 972
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/go_modules/golang.org/x/sync-0.6.0
- Loading branch information
Showing
31 changed files
with
476 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package gateway | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func (h *Handler) RegisterEndpoints(rpc *Server) { | ||
// state endpoints | ||
rpc.RegisterHandlerFunc( | ||
fmt.Sprintf("%s/{%s}", balanceEndpoint, addrKey), | ||
h.handleBalanceRequest, | ||
http.MethodGet, | ||
) | ||
|
||
rpc.RegisterHandlerFunc( | ||
submitTxEndpoint, | ||
h.handleSubmitTx, | ||
http.MethodPost, | ||
) | ||
|
||
rpc.RegisterHandlerFunc( | ||
healthEndpoint, | ||
h.handleHealthRequest, | ||
http.MethodGet, | ||
) | ||
|
||
// share endpoints | ||
rpc.RegisterHandlerFunc( | ||
fmt.Sprintf( | ||
"%s/{%s}/height/{%s}", | ||
namespacedSharesEndpoint, | ||
namespaceKey, | ||
heightKey, | ||
), | ||
h.handleSharesByNamespaceRequest, | ||
http.MethodGet, | ||
) | ||
|
||
rpc.RegisterHandlerFunc( | ||
fmt.Sprintf("%s/{%s}", namespacedSharesEndpoint, namespaceKey), | ||
h.handleSharesByNamespaceRequest, | ||
http.MethodGet, | ||
) | ||
|
||
rpc.RegisterHandlerFunc( | ||
fmt.Sprintf("%s/{%s}/height/{%s}", namespacedDataEndpoint, namespaceKey, heightKey), | ||
h.handleDataByNamespaceRequest, | ||
http.MethodGet, | ||
) | ||
|
||
rpc.RegisterHandlerFunc( | ||
fmt.Sprintf("%s/{%s}", namespacedDataEndpoint, namespaceKey), | ||
h.handleDataByNamespaceRequest, | ||
http.MethodGet, | ||
) | ||
|
||
// DAS endpoints | ||
rpc.RegisterHandlerFunc( | ||
fmt.Sprintf("%s/{%s}", heightAvailabilityEndpoint, heightKey), | ||
h.handleHeightAvailabilityRequest, | ||
http.MethodGet, | ||
) | ||
|
||
// header endpoints | ||
rpc.RegisterHandlerFunc( | ||
fmt.Sprintf("%s/{%s}", headerByHeightEndpoint, heightKey), | ||
h.handleHeaderRequest, | ||
http.MethodGet, | ||
) | ||
|
||
rpc.RegisterHandlerFunc(headEndpoint, h.handleHeadRequest, http.MethodGet) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package gateway | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRegisterEndpoints(t *testing.T) { | ||
handler := &Handler{} | ||
rpc := NewServer("localhost", "6969") | ||
|
||
handler.RegisterEndpoints(rpc) | ||
|
||
testCases := []struct { | ||
name string | ||
path string | ||
method string | ||
expected bool | ||
}{ | ||
{ | ||
name: "Get balance endpoint", | ||
path: fmt.Sprintf("%s/{%s}", balanceEndpoint, addrKey), | ||
method: http.MethodGet, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Submit transaction endpoint", | ||
path: submitTxEndpoint, | ||
method: http.MethodPost, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Get namespaced shares by height endpoint", | ||
path: fmt.Sprintf("%s/{%s}/height/{%s}", namespacedSharesEndpoint, namespaceKey, heightKey), | ||
method: http.MethodGet, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Get namespaced shares endpoint", | ||
path: fmt.Sprintf("%s/{%s}", namespacedSharesEndpoint, namespaceKey), | ||
method: http.MethodGet, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Get namespaced data by height endpoint", | ||
path: fmt.Sprintf("%s/{%s}/height/{%s}", namespacedDataEndpoint, namespaceKey, heightKey), | ||
method: http.MethodGet, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Get namespaced data endpoint", | ||
path: fmt.Sprintf("%s/{%s}", namespacedDataEndpoint, namespaceKey), | ||
method: http.MethodGet, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Get health endpoint", | ||
path: "/status/health", | ||
method: http.MethodGet, | ||
expected: true, | ||
}, | ||
|
||
// Going forward, we can add previously deprecated and since | ||
// removed endpoints here to ensure we don't accidentally re-enable | ||
// them in the future and accidentally expand surface area | ||
{ | ||
name: "example totally bogus endpoint", | ||
path: fmt.Sprintf("/wutang/{%s}/%s", "chambers", "36"), | ||
method: http.MethodGet, | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
require.Equal( | ||
t, | ||
tc.expected, | ||
hasEndpointRegistered(rpc.Router(), tc.path, tc.method), | ||
"Endpoint registration mismatch for: %s %s %s", tc.name, tc.method, tc.path) | ||
}) | ||
} | ||
} | ||
|
||
func hasEndpointRegistered(router *mux.Router, path string, method string) bool { | ||
var registered bool | ||
err := router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error { | ||
template, err := route.GetPathTemplate() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if template == path { | ||
methods, err := route.GetMethods() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, m := range methods { | ||
if m == method { | ||
registered = true | ||
return nil | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
fmt.Println("Error walking through routes:", err) | ||
return false | ||
} | ||
|
||
return registered | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package gateway | ||
|
||
import "net/http" | ||
|
||
const ( | ||
healthEndpoint = "/status/health" | ||
) | ||
|
||
func (h *Handler) handleHealthRequest(w http.ResponseWriter, _ *http.Request) { | ||
_, err := w.Write([]byte("ok")) | ||
if err != nil { | ||
log.Errorw("serving request", "endpoint", healthEndpoint, "err", err) | ||
writeError(w, http.StatusBadGateway, healthEndpoint, err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
package gateway | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
func writeError(w http.ResponseWriter, statusCode int, endpoint string, err error) { | ||
log.Debugw("serving request", "endpoint", endpoint, "err", err) | ||
|
||
w.WriteHeader(statusCode) | ||
errBody, jerr := json.Marshal(err.Error()) | ||
if jerr != nil { | ||
log.Errorw("serializing error", "endpoint", endpoint, "err", jerr) | ||
return | ||
} | ||
_, werr := w.Write(errBody) | ||
if werr != nil { | ||
log.Errorw("writing error response", "endpoint", endpoint, "err", werr) | ||
|
||
errorMessage := err.Error() // Get the error message as a string | ||
errorBytes := []byte(errorMessage) | ||
|
||
_, err = w.Write(errorBytes) | ||
if err != nil { | ||
log.Errorw("writing error response", "endpoint", endpoint, "err", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package gateway | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWriteError(t *testing.T) { | ||
t.Run("writeError", func(t *testing.T) { | ||
// Create a mock HTTP response writer | ||
w := httptest.NewRecorder() | ||
|
||
testErr := errors.New("test error") | ||
|
||
writeError(w, http.StatusInternalServerError, "/api/endpoint", testErr) | ||
assert.Equal(t, http.StatusInternalServerError, w.Code) | ||
responseBody := w.Body.Bytes() | ||
assert.Equal(t, testErr.Error(), string(responseBody)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.