Skip to content

Refactor webserver handlers#2261

Merged
leszko merged 1 commit intolivepeer:masterfrom
leszko:refactor-handlers
Apr 11, 2022
Merged

Refactor webserver handlers#2261
leszko merged 1 commit intolivepeer:masterfrom
leszko:refactor-handlers

Conversation

@leszko
Copy link
Copy Markdown
Contributor

@leszko leszko commented Feb 14, 2022

What does this pull request do? Explain your changes. (required)
Move all endpoint handlers from webserver.go to handlers.go and create unit tests for them.

The endpoints should work exactly the same as before with the following exceptions:

  • Sometimes different HTTP codes are returned (before we often returned 200 even if there was an error)
  • Fail fast if ETH client or DB is missing
  • If we want to return an empty object as JSON, now the proper null is returned (before it was either empty string or {})
  • Error message in the response body (made it more consistent)

Non-goals for this PR:

  • Improve our REST API
  • Improve messages on the Livepeer CLI side

Specific updates (required)

  • Moved all handlers to handlers.go
  • Reuse common code mustHaveClient(), mustHaveDb(), etc.
  • Add unit tests for almost handlers

How did you test each of these updates (required)

Played round with CLI to see if the commands work fine.

Does this pull request close any open issues?

Checklist:

@leszko leszko force-pushed the refactor-handlers branch 2 times, most recently from 5a79f41 to 673f428 Compare February 18, 2022 14:06
@leszko leszko marked this pull request as ready for review February 18, 2022 14:44
@leszko leszko changed the title WIP: Refactor webserver handlers Refactor webserver handlers Feb 18, 2022
Copy link
Copy Markdown
Member

@yondonfu yondonfu left a comment

Choose a reason for hiding this comment

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

Looks good overall (note: I mainly skimmed since most of the code changes for the handlers were copy + pasting existing logic into dedicated handler functions). Just a few :small questions.

If we want to return an empty object as JSON, now the proper null is returned

Could this cause problems for any of the livepeer_cli functions that work with these objects if the return value is "null" instead of an empty string or empty object?

assert.Equal(http.StatusOK, res.StatusCode)
body, err := ioutil.ReadAll(res.Body)
require.Nil(err)
assert.Equal("0", string(body))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we keep this test case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you mean the whole cliserver_test.go, right? It's kind-of integration/e2e test for the same what we test in handlers_test.go. I see some value in it, because if we, e.g., break something with the endpoints themselves, or in the REST server configuration of Livepeer, we would catch it here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I specifically mean the code here under // test offchain which appears to be a test case for when the /EthChainID endpoint should return 0. I see that we kept the test case starting on L228 for when the /EthChainID endpoint returns 1. So, I am wondering if we should have both?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ahh, I see you point. No, I think we don't need it anymore. This test was actually in some sense wrong before. Because it tested the following scenario:

If DB is nil, then return 0 from /EthChainID.

But it's not a valid scenario, we never have a situation when DB is nil, we always create DB, even for the off-chain mode. So, in result // test offchain is exactly the same as // test onchain in this test case. That's why I removed it and consolidated it into one.

And the root cause why I actually removed it was that now I check in the handler if we have the DB object, so this test started to fail.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Got it thanks for the clarification!

@leszko leszko removed the request for review from darkdarkdragon March 22, 2022 12:51
@leszko
Copy link
Copy Markdown
Contributor Author

leszko commented Mar 23, 2022

If we want to return an empty object as JSON, now the proper null is returned

Could this cause problems for any of the livepeer_cli functions that work with these objects if the return value is "null" instead of an empty string or empty object?

No, it's not a problem, because we should (and we actually do) json.Umarshal() in livepeer_cli. Then, null is a correct value to unmarshal a json.

@leszko leszko requested a review from yondonfu March 23, 2022 16:00
Copy link
Copy Markdown
Contributor Author

@leszko leszko left a comment

Choose a reason for hiding this comment

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

Thanks for the review @yondonfu Addressed both of your comments. PTAL

Copy link
Copy Markdown
Member

@yondonfu yondonfu left a comment

Choose a reason for hiding this comment

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

LGTM after resolving conflicts

@leszko leszko force-pushed the refactor-handlers branch from 52c2902 to e747af9 Compare April 11, 2022 09:17
@leszko leszko merged commit 786769b into livepeer:master Apr 11, 2022
@leszko leszko deleted the refactor-handlers branch April 11, 2022 09:42
ad-astra-video pushed a commit to ad-astra-video/go-livepeer that referenced this pull request May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants