Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to display peer id #719

Merged
merged 21 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions api/http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package http

import (
"context"
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -20,13 +21,20 @@ import (

var env = os.Getenv("DEFRA_ENV")

type errorResponse struct {
Errors []errorItem `json:"errors"`
var (
errNoListener = errors.New("cannot serve with no listener")
errSchema = errors.New("base must start with the http or https scheme")
)
shahzadlone marked this conversation as resolved.
Show resolved Hide resolved

// ErrorResponse is the GQL top level object holding error items for the response payload.
type ErrorResponse struct {
Errors []ErrorItem `json:"errors"`
}

type errorItem struct {
Message string `json:"message"`
Extensions *extensions `json:"extensions,omitempty"`
// ErrorItem hold an error message and extensions that might be pertinent to the request
type ErrorItem struct {
Message string `json:"message"`
Extensions extensions `json:"extensions,omitempty"`
shahzadlone marked this conversation as resolved.
Show resolved Hide resolved
}

type extensions struct {
Expand All @@ -43,11 +51,11 @@ func handleErr(ctx context.Context, rw http.ResponseWriter, err error, status in
sendJSON(
ctx,
rw,
errorResponse{
Errors: []errorItem{
ErrorResponse{
Errors: []ErrorItem{
{
Message: err.Error(),
Extensions: &extensions{
Extensions: extensions{
Status: status,
HTTPError: http.StatusText(status),
Stack: formatError(err),
Expand Down
8 changes: 4 additions & 4 deletions api/http/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestHandleErrOnBadRequest(t *testing.T) {

resp := rec.Result()

errResponse := errorResponse{}
errResponse := ErrorResponse{}
err = json.NewDecoder(resp.Body).Decode(&errResponse)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestHandleErrOnInternalServerError(t *testing.T) {

resp := rec.Result()

errResponse := errorResponse{}
errResponse := ErrorResponse{}
err = json.NewDecoder(resp.Body).Decode(&errResponse)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestHandleErrOnNotFound(t *testing.T) {

resp := rec.Result()

errResponse := errorResponse{}
errResponse := ErrorResponse{}
err = json.NewDecoder(resp.Body).Decode(&errResponse)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestHandleErrOnDefault(t *testing.T) {

resp := rec.Result()

errResponse := errorResponse{}
errResponse := ErrorResponse{}
err = json.NewDecoder(resp.Body).Decode(&errResponse)
if err != nil {
t.Fatal(err)
Expand Down
14 changes: 10 additions & 4 deletions api/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ type handler struct {

type ctxDB struct{}

type dataResponse struct {
fredcarle marked this conversation as resolved.
Show resolved Hide resolved
type ctxPeerID struct{}

// DataResponse is the GQL top level object holding data for the response payload.
type DataResponse struct {
Data interface{} `json:"data"`
}

// simpleDataResponse is a helper function that returns a dataResponse struct.
// simpleDataResponse is a helper function that returns a DataResponse struct.
// Odd arguments are the keys and must be strings otherwise they are ignored.
// Even arguments are the values associated with the previous key.
// Odd arguments are also ignored if there are no following arguments.
func simpleDataResponse(args ...interface{}) dataResponse {
func simpleDataResponse(args ...interface{}) DataResponse {
data := make(map[string]interface{})

for i := 0; i < len(args); i += 2 {
Expand All @@ -55,7 +58,7 @@ func simpleDataResponse(args ...interface{}) dataResponse {
}
}

return dataResponse{
return DataResponse{
Data: data,
}
}
Expand All @@ -71,6 +74,9 @@ func newHandler(db client.DB, opts serverOptions) *handler {
func (h *handler) handle(f http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
ctx := context.WithValue(req.Context(), ctxDB{}, h.db)
if h.options.peerID != "" {
ctx = context.WithValue(ctx, ctxPeerID{}, h.options.peerID)
}
f(rw, req.WithContext(ctx))
}
}
Expand Down
17 changes: 17 additions & 0 deletions api/http/handlerfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,20 @@ func getBlockHandler(rw http.ResponseWriter, req *http.Request) {
http.StatusOK,
)
}

func peerIDHandler(rw http.ResponseWriter, req *http.Request) {
peerID, ok := req.Context().Value(ctxPeerID{}).(string)
if !ok || peerID == "" {
handleErr(req.Context(), rw, errors.New("no peer ID available. P2P might be disabled"), http.StatusNotFound)
return
}

sendJSON(
req.Context(),
rw,
simpleDataResponse(
"peerID", peerID,
),
http.StatusOK,
)
}
Loading