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 API server to use a redirect page instead of proxy, and fix a bug in cockpit #1513

Merged
merged 2 commits into from
Apr 16, 2019

Conversation

michaelsbradleyjr
Copy link
Contributor

refactor(@embark/api): in dev use cockpit redirect instead of proxy

Embark API server's development proxy from port 55555 to 3000 was attempting to inappropriately forward an /embark-api/ endpoint for the blockchain process logs to Create React App's development server. Why it was only happening for the one endpoint is not known but probably has to do with timing around registration of the API server's express routes.

The problem can be fixed with a one-line filter: function in the options for express-http-proxy. However, it was realized that to fix an unrelated problem, whereby the proxy doesn't forward websockets to CRA such that hot reload doesn't work when accessing embark-ui in development on port 55555, a switch to http-proxy-middleware would be required. That was quickly attempted (easy switch) but there are outstanding difficulties with webpack-dev-server and node-http-proxy that cause CRA to crash.

Switch strategies and refactor the API module to serve a page on port 55555 (in development only) that alerts the developer embark-ui should be accessed on port 3000. The page redirects (client-side) after 10 seconds, with URL query params and/or hash preserved. A future version could instead do client-side polling of port 3000 with fetch and then redirect only once it's available. The reason for not redirecting immediately is that the intermediate page makes it more obvious what needs to be done, e.g. CRA dev server may need to be started with yarn start.

fix(@cockpit/services): send only process names to embark-api-client

webSocketProcess function in embark-api-client expects only a process name, but cockpit was calling it with name-strings prepended with /process-logs/ resulting in bad URLs. Revise cockpit's services/api module to call webSocketProcess with process names only.

@michaelsbradleyjr michaelsbradleyjr requested a review from a team April 16, 2019 00:41
@michaelsbradleyjr
Copy link
Contributor Author

michaelsbradleyjr commented Apr 16, 2019

A couple of things to be aware of:

Sometimes updates to embark's and blockchain's process logs, as seen in cockpit, don't start happening immediately after loading cockpit (reason unknown) but most of the time they do start right away.

There are some problems with how we're escaping characters like & and > before sending embark's process logs to cockpit. They're connected to what seems like a doubling-up (or more) of logs being sent to cockpit, which results in > becoming >and then > becoming >, and so on.

Neither issue pertains directly to the code changed in this PR, so they'll be addressed in later PRs.

Copy link
Collaborator

@emizzle emizzle left a comment

Choose a reason for hiding this comment

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

I did not take it for a spin, but the changes look good! Great job.

@@ -179,7 +179,7 @@ export function listenToChannel(credentials, channel) {
}

export function webSocketProcess(credentials, processName) {
return embarkAPI.webSocketProcess(credentials, `/process-logs/${processName}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was there a double-up of process-logs in the failing URLs before? For example, embark-api/process-logs/process-logs/blockchain. I guess I'm curious as to why there was no need for an update on the other end (ie in embark-api-client). At a glance, what you've done looks correct.

Copy link
Contributor

Choose a reason for hiding this comment

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

There was. I've seen them too, but they didn't seem to cause errors in the network, which is why I wasn't sure if it was really an issue or just devtools just outputting certain protocol request in a different way...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@emizzle yes, in the network tab of browser devtools they looked like process-logs/[name]//process-logs/[name].

@PascalPrecht they didn't cause errors per se, but because of the bad URLs the process logs weren't getting updates. You could toggle between the process log tabs and a fetch would result in the latest logs being loaded; but with the URL fix the updates come streaming in, which is the desired behavior.

Copy link
Contributor Author

@michaelsbradleyjr michaelsbradleyjr Apr 16, 2019

Choose a reason for hiding this comment

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

So really there were two bugs. One was related to the proxy on 55555 forwarding /embark-api/ requests; the other was the bad URLs, unrelated to the first. The effect of the second wasn't apparent until the first one was fixed.

Copy link
Contributor

@0x-r4bbit 0x-r4bbit left a comment

Choose a reason for hiding this comment

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

Tested it locally, this works fine now.

Great work!

<p>to instead use a static build from the monorepo, restart embark with:
<code>EMBARK_UI_STATIC=t embark run</code></p>
` : ""}
<p>this page will automatically redirect to <a id="redirect" href="">
<span></span></a> in <span id="timer">${redirectSeconds}</span>
Copy link
Collaborator

@jrainville jrainville Apr 16, 2019

Choose a reason for hiding this comment

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

The <span> inside the <a> is useless as it's overwritten

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, and I also made a change so that loading from 127.0.0.1:55555 works just as well as loading from localhost:55555.

Embark API server's development proxy from port 55555 to 3000 was attempting to
inappropriately forward an `/embark-api/` endpoint for the blockchain process
logs to Create React App's development server. Why it was only happening for
the one endpoint is not known but probably has to do with timing around
registration of the API server's express routes.

The problem can be fixed with a one-line `filter:` function in the options for
`express-http-proxy`. However, it was realized that to fix an unrelated
problem, whereby the proxy doesn't forward websockets to CRA such that hot
reload doesn't work when accessing `embark-ui` in development on port 55555, a
switch to `http-proxy-middleware` would be required. That was quickly
attempted (easy switch) but there are outstanding [difficulties][bug] with
`webpack-dev-server` and `node-http-proxy` that cause CRA to crash.

Switch strategies and refactor the API module to serve a page on port 55555 (in
development only) that alerts the developer `embark-ui` should be accessed on
port 3000. The page redirects (client-side) after 10 seconds, with URL query
params and/or hash preserved. A future version could instead do client-side
polling of port 3000 with `fetch` and then redirect only once it's
available. The reason for not redirecting immediately is that the intermediate
page makes it more obvious what needs to be done, e.g. CRA dev server may need
to be started with `yarn start`.

[bug]: webpack/webpack-dev-server#1642
`webSocketProcess` function in `embark-api-client` expects only a process name,
but cockpit was calling it with name-strings prepended with `/process-logs/`
resulting in bad URLs. Revise cockpit's `services/api` module to call
`webSocketProcess` with process names only.
@iurimatias iurimatias merged commit eb9de68 into master Apr 16, 2019
@delete-merged-branch delete-merged-branch bot deleted the fix/ui-proxy branch April 16, 2019 18:25
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.

5 participants