Skip to content

Commit

Permalink
refactor: Improve the HTTP API implementation (#382)
Browse files Browse the repository at this point in the history
Resolves #372
Resolves #367

This PR restructures the api/http package with idiomatic patterns and a few useful utility functions.
  • Loading branch information
fredcarle authored May 12, 2022
1 parent dbbe282 commit 593214a
Show file tree
Hide file tree
Showing 22 changed files with 1,271 additions and 309 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ defradb client ping
```
which should respond with `Success!`

Once you've confirmed your node is running correctly, if you're using the GraphiQL client to interact with the database, then make sure you set the `GraphQL Endpoint` to `http://localhost:9181/graphql` and the `Method` to `GET`.
Once you've confirmed your node is running correctly, if you're using the GraphiQL client to interact with the database, then make sure you set the `GraphQL Endpoint` to `http://localhost:9181/api/v1/graphql`.

### Add a Schema type

Expand Down Expand Up @@ -391,6 +391,7 @@ When contributing to a DefraDB feature, you can find the relevant license in the
- Andrew Sisley ([@AndrewSisley](https://github.com/AndrewSisley))
- Shahzad Lone ([@shahzadlone](https://github.com/shahzadlone))
- Orpheus Lummis ([@orpheuslummis](https://github.com/orpheuslummis))
- Fred Carle ([@fredcarle](https://github.com/fredcarle))

<br>

Expand Down
289 changes: 0 additions & 289 deletions api/http/api.go

This file was deleted.

51 changes: 51 additions & 0 deletions api/http/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package http

import (
"context"
"fmt"
"net/http"
"os"
"strings"
)

var env = os.Getenv("DEFRA_ENV")

type errorResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Stack string `json:"stack,omitempty"`
}

func handleErr(ctx context.Context, rw http.ResponseWriter, err error, status int) {
if status == http.StatusInternalServerError {
log.ErrorE(ctx, http.StatusText(status), err)
}

sendJSON(
ctx,
rw,
errorResponse{
Status: status,
Message: http.StatusText(status),
Stack: formatError(err),
},
status,
)
}

func formatError(err error) string {
if strings.ToLower(env) == "dev" || strings.ToLower(env) == "development" {
return fmt.Sprintf("[DEV] %+v\n", err)
}
return ""
}
Loading

0 comments on commit 593214a

Please sign in to comment.