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

refactor: Improve the HTTP API implementation #382

Merged
merged 19 commits into from
May 12, 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
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄


<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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I think this is very tucked away for a public concept, would suggest making the variable public (Env) and moving to http.go (or maybe even env.go - but that might be overkill here)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env.go would be overkill for one environment variable.

Makes sense to make it public if the config file or cli flags can set the variable. Although if that is the case maybe it should be moved to the WIP config package. I'll leave it as is for now and create a pull request to make the change once the config package is merged.


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