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

feat(cli): Enable host option for yarn rw serve #8385

Merged
merged 27 commits into from
May 24, 2023
Merged

feat(cli): Enable host option for yarn rw serve #8385

merged 27 commits into from
May 24, 2023

Conversation

Josh-Walker-GM
Copy link
Collaborator

@Josh-Walker-GM Josh-Walker-GM commented May 22, 2023

Problem
We need to have some consistent way to set the host used when running yarn rw serve [side] and we also want to default to localhost.

See #8019 (comment)

Solution
We add a --host option to the yarn rw serve command. It defaults to the value inside the appropriate redwood.toml option. These toml options themselves default to localhost when not explicitly defined.

We should also be aware that when we run both sides using yarn rw serve we preferentially choose the web config over the api config. We have this because we offer both options but only start one fastify server when running both sides.

@Josh-Walker-GM Josh-Walker-GM changed the title Add 'host' option which defaults to toml value - which defaults to 'l… feat(cli): Enable host option for yarn rw serve May 22, 2023
@Josh-Walker-GM Josh-Walker-GM added the release:feature This PR introduces a new feature label May 22, 2023
@replay-io
Copy link

replay-io bot commented May 22, 2023

19 replays were recorded for d1e1fba.

image 0 Failed
image 19 Passed

View test run on Replay ↗︎

@jtoar
Copy link
Contributor

jtoar commented May 22, 2023

Just to confirm that this fix will work for you, @dennemark are you running the api-server independently of the CLI? We should probably make this change anyway just to be consistent, but if you are, the work here won't solve the bug for you, which I think you could've only really experienced by running the api-server binary as-is and not through the CLI


Update: the api-server CLI handlers are also for yarn rw dev

Copy link
Contributor

@jtoar jtoar left a comment

Choose a reason for hiding this comment

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

LGTM @Josh-Walker-GM, thanks!

@jtoar jtoar added this to the next-release-patch milestone May 22, 2023
Copy link
Contributor

@jtoar jtoar left a comment

Choose a reason for hiding this comment

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

Probably okay'ed a bit too early; I should test a few more things because there's a lot of entry points here

  • yarn rw serve
  • yarn rw dev
  • running api-server's bins (rw-api-server-watch, rw-server) independently of the CLI
    • it's unlikely that rw-api-server-watch is used independently of the CLI
  • passing flags via CLI
  • setting config in redwood.toml

yarn rw dev

Flag n/a
Toml api settings are respected

Now if you set host in the api table in redwood.toml:

 [api]
   port = 8911
+  host = "0.0.0.0" # normally 'localhost'

yarn rw dev doesn't work out of the box. Before it did, but only because host was ignored, which could also be considered a bug.

To get it working again, you have to set RWJS_DEV_API_URL (an env var that has no TOML-equivalent nor is it used anywhere else) to "http://0.0.0.0" so that the webpack dev server proxy works again:

target: `${process.env.RWJS_DEV_API_URL ?? 'http://localhost'}:${port}`,

Again, I'm not saying this is a bug or is undesirable. It's just different than it was before.

jtoar

This comment was marked as duplicate.

@jtoar
Copy link
Contributor

jtoar commented May 23, 2023

Here's an old one this is related to: #6362

@dennemark
Copy link
Contributor

dennemark commented May 23, 2023

Just to confirm that this fix will work for you, @dennemark are you running the api-server independently of the CLI? We should probably make this change anyway just to be consistent, but if you are, the work here won't solve the bug for you, which I think you could've only really experienced by running the api-server binary as-is and not through the CLI

Update: the api-server CLI handlers are also for yarn rw dev

So much progress while I was asleep... thanks so much!

Within my development environment I do not need @redwoodjs/api-server. But when I try to run my docker container, I need to install @redwoodjs/api-server and @redwoodjs/internals seperately. (I am not fully sure why, anymore, since those packages should be installed anyways). But when starting the docker container, I am calling

yarn rw-server api
(follows roughly this docker version: https://github.com/redwoodjs/docker/blob/a983aca3b95bdccfb339cf1f6dfcb6e5bbde22b5/docker/jeliasson-nginx/api/Dockerfile#L90)

Btw. right now I have set host on api and web side in redwood.toml. Both stating 0.0.0.0. But I only added the host to api side yesterday, hoping it would resolve my docker issue (i downgraded api-server for now). I think I would prefer clear seperation of api and web host. So on api side api || localhost and on web side web || localhost, since I would not assume web host to affect api side.

@jtoar
Copy link
Contributor

jtoar commented May 24, 2023

@dennemark I think I've got my head wrapped around all the changes here now. Here's the state of things:

With this PR, if you run the web and api servers separately, the settings for each will be respected. But if you run them together, since we only start one fastify server at the moment, the web settings will be used.

You said you were running yarn rw-server api, so the settings in your [api] TOML table should be respected now.

I'll merge this and release an RC; if you could upgrade to it (I'll follow up with the version once it's published) and confirm that it works the way you expect that'd be greatly appreciated!

Copy link
Contributor

@jtoar jtoar left a comment

Choose a reason for hiding this comment

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

Thanks @Josh-Walker-GM, and thanks for letting me hijack this one.

I want to follow up on a few more discussions with others, but want to get an RC out for testing.

@jtoar jtoar merged commit 26ad3e2 into main May 24, 2023
@jtoar jtoar deleted the jgmw-serve-host branch May 24, 2023 02:37
jtoar added a commit that referenced this pull request May 24, 2023
* Add 'host' option which defaults to toml value - which defaults to 'localhost'

* Update api-server cli-handlers too

* style: save project config calls in var

won't make much of a difference for getPaths but getConfig isn't
memoized so that one is a little excessive to keep calling

* style: save project-config calls where possible

* style: prefer ?? to || for undefined values

* style changes

* enable api-server to take flag

* remove stray nullish accessor

* style: remove needless instantiation

* use existing config in scope

* style

* set host in dev handler

* remove needless null checks

* style: make api command more readable

* add host to watch

* get dev test passing

* get serve test passing

* c

* get watch working

* remove console log

* comments

* style

* fix dev handler when web host is set

* revert to expected webpack behavior

* fix test

---------

Co-authored-by: Dominic Saadi <dominiceliassaadi@gmail.com>
@jtoar jtoar modified the milestones: next-release-patch, v5.2.3 May 24, 2023
jtoar added a commit that referenced this pull request May 24, 2023
* Add 'host' option which defaults to toml value - which defaults to 'localhost'

* Update api-server cli-handlers too

* style: save project config calls in var

won't make much of a difference for getPaths but getConfig isn't
memoized so that one is a little excessive to keep calling

* style: save project-config calls where possible

* style: prefer ?? to || for undefined values

* style changes

* enable api-server to take flag

* remove stray nullish accessor

* style: remove needless instantiation

* use existing config in scope

* style

* set host in dev handler

* remove needless null checks

* style: make api command more readable

* add host to watch

* get dev test passing

* get serve test passing

* c

* get watch working

* remove console log

* comments

* style

* fix dev handler when web host is set

* revert to expected webpack behavior

* fix test

---------

Co-authored-by: Dominic Saadi <dominiceliassaadi@gmail.com>
@jtoar
Copy link
Contributor

jtoar commented May 24, 2023

@dennemark I just released this PR in an RC: 5.2.3-rc.3+ddbd1f431. You can upgrade to it via yarn rw upgrade -t rc. The hash after the + may not be there, but it shouldn't matter. Would love to know if this fixes it for you

jtoar added a commit that referenced this pull request May 24, 2023
@jtoar
Copy link
Contributor

jtoar commented May 24, 2023

@dennemark another update: I've changed my by plan here. I'm going to revert #8019 and release a patch till we've got this sorted out in main and know all the edge cases. So this PR won't be available in the patch when I actually release it. Sorry for the churn! I'll follow up one more time when v5.2.3 is released

@dennemark
Copy link
Contributor

@dennemark another update: I've changed my by plan here. I'm going to revert #8019 and release a patch till we've got this sorted out in main and know all the edge cases. So this PR won't be available in the patch when I actually release it. Sorry for the churn! I'll follow up one more time when v5.2.3 is released

Thanks so much for the effort!

So i am going to test it in one of the follow up releases?

Going to check the commits tomorrow. Curious, why this issue is quite complex :)

@jtoar jtoar modified the milestones: v5.2.3, next-release May 24, 2023
@jtoar
Copy link
Contributor

jtoar commented May 24, 2023

@dennemark yeah you can ignore the RC I mentioned earlier and use this patch instead https://github.com/redwoodjs/redwood/releases/tag/v5.2.3.

This patch just reverts #8019, so you don't have to set api.host in redwood.toml. We still want to make it so that you can configure api.host in redwood.toml, but it was easier and faster to revert the breaking change we made.

The reason for complexity here is 1) there's a lot of different deploy environments and 2) there's a lot of different combinations to these settings among the CLI commands, and checking them all just takes time. (And I have other things I'm supposed to be working on instead, so I kind of need this one wrapped for the time being.)

@dennemark
Copy link
Contributor

Thanks for the heads up and your effort :)

@jtoar jtoar modified the milestones: next-release, v6.0.0 May 25, 2023
jtoar added a commit that referenced this pull request Jun 28, 2023
jtoar added a commit that referenced this pull request Jun 28, 2023
@jtoar jtoar modified the milestones: v6.0.0, v7.0.0 Jun 28, 2023
jtoar added a commit that referenced this pull request Jun 28, 2023
* Revert "Reserve h for help, and format docs (#8407)"

This reverts commit 70b7c4d.

* Revert "feat(cli): Enable `host` option for `yarn rw serve` (#8385)"

This reverts commit 26ad3e2.

* Revert "Fastify server: Default to localhost (#8019)"

This reverts commit d609db6.
@jtoar jtoar modified the milestones: SSR, v7.0.0 Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature This PR introduces a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants