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(dev): Allow setting host and port #1816

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

bastilian
Copy link
Contributor

@bastilian bastilian commented Sep 25, 2023

This adds --host and --port flags to allow running fresh with a custom IP and port to listen on for requests.

@@ -426,7 +426,8 @@ async function spawnServer(
// @ts-ignore yes it does
for await (const line of lines.values({ preventCancel: true })) {
output.push(line);
const match = line.match(/https?:\/\/localhost:\d+/g);
const match = line.match("Fresh ready");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the hostname/IP can now be variable this needed to be changed to assert the fresh is running. "Fresh ready" seemed to be better as it'll always be there and not dynamically change.

if (match) {
const running = line.match("Fresh ready");

if (running) {
address = match[0];
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line is likely causing the tests to fail. Previously the regex was used to grab the actual server address out of the child process stdout and that's missing now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I reverted the change to "Fresh ready" and just made the regex less strict, which should do the trick.

@bastilian
Copy link
Contributor Author

The test added for the HOST env var currently covers just the env var, is there a place (already) where the the flags and/or the dev() options should be tested? I couldn't find an existing test for the port, or have I missed it?

@bastilian
Copy link
Contributor Author

Just checking up on here. If this should have more tests, it might be a better path to first add general tests for the dev command, which might be better in a separate PR (that I'd be happy to submit).

@bastilian
Copy link
Contributor Author

@marvinhagemeister After playing with this more, I felt it would be better if flags or environment variables would override what is passed in the options.

@bastilian
Copy link
Contributor Author

ping. Let me know if this PR needs more work. :)

Copy link
Contributor

@deer deer left a comment

Choose a reason for hiding this comment

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

Hi @bastilian, cool feature. A few comments:

  • looks like a conflict has crept in (I think due to one of my PRs that got merged) -- please merge main and clean that up
  • the rest are inline
@@ -39,13 +39,20 @@ If you want to change the port or host, modify the config bag of the `start()`
call in `main.ts` to include an explicit port number:

```js main.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this isn't from you, but we no longer modify main.ts to do this. The correct spot is now in fresh.config.ts. (Otherwise these changes wouldn't take place when running in dev mode.) Additionally, setting port and hostname directly are deprecated. Please see FreshConfig for details.

I guess the correct thing would be something like:

export default defineConfig({
  server: {
    port: 3000,
    hostname: "0.0.0.0",
  },
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure on how to properly update the docs properly. This helped! Thank you for the hint!

console.log(` ${localLabel} ${address}\n`);
};
if (portEnv !== undefined || portFlag !== undefined) {
opts.port = parseInt((portFlag || portEnv) as string, 10);
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't 10 the default? Do you really need to declare it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's unfortunately a little more complex. Quoting from MDN:

  • If the input string, with leading whitespace and possible +/- signs removed, begins with 0x or 0X (a zero, followed by lowercase or uppercase X), radix is assumed to be 16 and the rest of the string is parsed as a hexadecimal number.
  • If the input string begins with any other value, the radix is 10 (decimal).

So better to always pass the radix argument when using parseInt

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the link and the clarification!

@@ -841,6 +841,27 @@ Deno.test("PORT environment variable", async () => {
for await (const _ of lines) { /* noop */ }
});

Deno.test("HOST environment variable", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I see your comment about testing port, but since Fresh relies on Deno.serve, I guess there aren't any tests for testing hostname/port from the options bag. We can assume it works from upstream.

Perhaps you could modify this to also set the port via an environment variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add the PORT to this test. Thank you!

Copy link
Contributor

@deer deer left a comment

Choose a reason for hiding this comment

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

New test looks good. Just a minor comment about canary that I should have made yesterday!

Copy link
Contributor

Choose a reason for hiding this comment

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

I unfortunately think my comment from here applies as well:

Now that I think about it more, this should probably be branched to a canary version.

If someone reads this prior to the next release, they could end up confused. https://github.com/denoland/fresh/pull/1984/files#diff-40ec5d53f77de7b64b64ce31562e1717e678c4c55640531b6a55d2cbc9f53936L57 provides an example of how to modify the toc.ts file to have a different canary page.

In the future I'll make sure to make this suggestion straight away when people are adding new features.

@bastilian
Copy link
Contributor Author

@deer Thank you for taking a look again! The updated docs should now be under canary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants