-
Notifications
You must be signed in to change notification settings - Fork 502
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
exp/lighthorizon: enforce the limit from request on the response size #4431
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c8042ff
#4430: wip code for further reveiw on intended change for index usage
sreuland f05d62a
#4430: enforce limit parameter from request on response size for txs
sreuland 2103514
#4430: fixed static shadow errors
sreuland 3108347
#4330: silenced some static check warnings
sreuland 4204af6
Merge remote-tracking branch 'upstream/lighthorizon' into lighthorizon
sreuland 99cf679
#4430: added a starter for api docs in oapi format
sreuland 5783f26
#4430: added page limit processing to operations endpoint
sreuland 70bda9c
#4430: removed some left over debug code
sreuland d5e26e5
Merge remote-tracking branch 'upstream/lighthorizon' into lighthorizon
sreuland a5a8b83
#4430: increment tx order num in all cases for each ledger read opera…
sreuland 947070c
#4430: refactored paging params validation
sreuland eb918f5
#4430: used correct index constants
sreuland 9e401e5
#4430: more verbose error messages for invalid tx id request param
sreuland a6d26c4
#4430: fixed transaction toid increment on read loop
sreuland c18d113
#4430: fixed go formatting warning
sreuland b11efd9
#4430: added unit test coverage, use adapter pattern for archive
sreuland 7fc04ae
#4430: added some docs on ingest archive interface
sreuland 7ed242f
#4430: fixed go formatting warning
sreuland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package actions | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func ApiDocs() func(http.ResponseWriter, *http.Request) { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
r.URL.Scheme = "http" | ||
r.URL.Host = "localhost:8080" | ||
|
||
if r.Method != "GET" { | ||
sendErrorResponse(w, http.StatusMethodNotAllowed, "") | ||
return | ||
} | ||
|
||
p, err := staticFiles.ReadFile("static/api_docs.yml") | ||
if err != nil { | ||
w.WriteHeader(http.StatusNotFound) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/openapi+yaml") | ||
w.Write(p) | ||
} | ||
} |
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,94 @@ | ||
package actions | ||
|
||
import ( | ||
"embed" | ||
"encoding/json" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
|
||
"github.com/stellar/go/support/log" | ||
"github.com/stellar/go/support/render/hal" | ||
) | ||
|
||
var ( | ||
//go:embed static | ||
staticFiles embed.FS | ||
) | ||
|
||
type Order string | ||
type ErrorMessage string | ||
|
||
const ( | ||
OrderAsc Order = "asc" | ||
OrderDesc Order = "desc" | ||
) | ||
|
||
const ( | ||
ServerError ErrorMessage = "Error: A problem occurred on the server while processing request" | ||
InvalidPagingParameters ErrorMessage = "Error: Invalid paging parameters" | ||
) | ||
|
||
type Pagination struct { | ||
Limit int64 | ||
Cursor int64 | ||
Order | ||
} | ||
|
||
func sendPageResponse(w http.ResponseWriter, page hal.Page) { | ||
encoder := json.NewEncoder(w) | ||
encoder.SetIndent("", " ") | ||
err := encoder.Encode(page) | ||
if err != nil { | ||
log.Error(err) | ||
sendErrorResponse(w, http.StatusInternalServerError, "") | ||
} | ||
} | ||
|
||
func sendErrorResponse(w http.ResponseWriter, errorCode int, errorMsg string) { | ||
if errorMsg != "" { | ||
http.Error(w, errorMsg, errorCode) | ||
} else { | ||
http.Error(w, string(ServerError), errorCode) | ||
} | ||
} | ||
|
||
func RequestUnaryParam(r *http.Request, paramName string) (string, error) { | ||
query, err := url.ParseQuery(r.URL.RawQuery) | ||
if err != nil { | ||
return "", err | ||
} | ||
return query.Get(paramName), nil | ||
} | ||
|
||
func Paging(r *http.Request) (Pagination, error) { | ||
paginate := Pagination{ | ||
Order: OrderAsc, | ||
} | ||
|
||
if cursorRequested, err := RequestUnaryParam(r, "cursor"); err != nil { | ||
return Pagination{}, err | ||
} else if cursorRequested != "" { | ||
paginate.Cursor, err = strconv.ParseInt(cursorRequested, 10, 64) | ||
if err != nil { | ||
return Pagination{}, err | ||
} | ||
} | ||
|
||
if limitRequested, err := RequestUnaryParam(r, "limit"); err != nil { | ||
return Pagination{}, err | ||
} else if limitRequested != "" { | ||
paginate.Limit, err = strconv.ParseInt(limitRequested, 10, 64) | ||
if err != nil { | ||
return Pagination{}, err | ||
} | ||
} | ||
|
||
if orderRequested, err := RequestUnaryParam(r, "order"); err != nil { | ||
return Pagination{}, err | ||
} else if orderRequested != "" && orderRequested == string(OrderDesc) { | ||
paginate.Order = OrderDesc | ||
} | ||
|
||
return paginate, nil | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have an "invalid order" error if orderRequested != "asc" or "desc"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how do you feel about the minimal approach first, having the api do some opinionated error proofing rather than more chatter? will put in the error response, but wanted to get a read on that style aspect first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no strong opinion, agreed that we should keep things minimal to keep eyes on the prize - aka working mvp