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

Remove AppConstants #5044

Merged
merged 1 commit into from
Sep 16, 2024
Merged

Remove AppConstants #5044

merged 1 commit into from
Sep 16, 2024

Conversation

Vinnl
Copy link
Collaborator

@Vinnl Vinnl commented Sep 11, 2024

To make the migration as safe as possible, in places where it was used to access environment variables that were absolutely required, I replaced it with a function that forces the caller to explicitly enumerate the environment variables they specifically expect, allows them to access them in a type-safe way, and throws if the named environment variables aren't set. (This is slightly different than the previous behaviour, where AppConstants would only log the missing variable without throwing, but I checked that all these variables should be set in production.)

Merges into #5043.

@Vinnl Vinnl added the Review: XS Code review time: up to 30min label Sep 11, 2024
@Vinnl Vinnl requested a review from mansaj September 11, 2024 14:43
@Vinnl Vinnl self-assigned this Sep 11, 2024
Copy link

@Vinnl Vinnl requested a review from rhelmer September 11, 2024 19:36
Copy link
Collaborator

@rhelmer rhelmer left a comment

Choose a reason for hiding this comment

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

lgtm, I'm wondering why you don't use the new getEnvVarsOrThrow in a few places but I'll leave it up to you whether it's appropriate vs. plain process.env.

@@ -26,6 +25,15 @@ import { SerializedSubscriber } from "../../../next-auth";
import { record } from "../../functions/server/glean";
import { renderEmail } from "../../../emails/renderEmail";
import { SignupReportEmail } from "../../../emails/templates/signupReport/SignupReportEmail";
import { getEnvVarsOrThrow } from "../../../envVars";

const envVars = getEnvVarsOrThrow([
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh that's pretty cool, nice idea. I didn't like how non-standard AppConstants was but I really like the behavior of throwing upfront.

@@ -108,6 +107,6 @@ export async function POST(req: NextRequest) {
}
} else {
// Not Signed in, redirect to home
return NextResponse.redirect(AppConstants.SERVER_URL, 301);
return NextResponse.redirect(process.env.SERVER_URL ?? "/", 301);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm curious why you don't use the getEnvVarsOrThrow function here? Seems like you could avoid having to do the ?? "/", is this fallback ever desirable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good question, and I was (still am) on the fence. My reasoning was as follows: AppConstants only logged an error without throwing, so if this variable wouldn't be set, this would just result in a log and a call to NextResponse.redirect(undefined, 301). So if I were to use that function instead, maybe I would be causing API routes that used to succeed but return a malformed response, to start failing upfront now. Not in practice, because the variable should be set, I think - but I might be thinking wrong.

It looks like .redirect supports relative URLs, so judging by the comment above this line, this fallback is the desired behaviour. Though given that this is a POST request handler, I don't see why that makes sense - so presumably, we're never hitting this.

@@ -50,7 +49,7 @@ export async function POST(req: NextRequest) {
existingEmail.email,
);
return NextResponse.redirect(
AppConstants.SERVER_URL + "/user/settings",
process.env.SERVER_URL + "/user/settings",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question re: getEnvVarsOrThrow vs. process.env

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same reasoning as #5044 (comment) - in this case, if it's unset, it would fall back to redirecting to /user/settings anyway, I think.

!FX_REMOTE_SETTINGS_WRITER_USER ||
!FX_REMOTE_SETTINGS_WRITER_PASS ||
!FX_REMOTE_SETTINGS_WRITER_SERVER
) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like you could use getEnvVarsOrThrow and catch it + process.exit (I think throwing from here would the process to exit anyway but being explicit is probably better just in case it's refactored later)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I was actually considering that too, but then I figured I'd have to wrap the entire file in a try {} block, which... Isn't that big of a deal, but felt a little bit icky, so I stuck with mostly matching the existing code. I hadn't realised yet that throwing would cause an exit anyway, so that could actually be a good compromise too. What do you think?

Base automatically changed from MNTOR-3077-everything-ts to main September 12, 2024 10:15
@Vinnl
Copy link
Collaborator Author

Vinnl commented Sep 12, 2024

@rhelmer Heh, my answers to all your questions are basically: good point, here's my reasoning, but I could go either way, what do you think? So, do you have opinions? :P

@Vinnl Vinnl requested a review from rhelmer September 12, 2024 10:28
To make the migration as safe as possible, in places where it was
used to access environment variables that were absolutely required,
I replaced it with a function that forces the caller to explicitly
enumerate the environment variables they specifically expect,
allows them to access them in a type-safe way, and throws if the
named environment variables aren't set. (This is slightly different
than the previous behaviour, where AppConstants would only log the
missing variable without throwing, but I checked that all these
variables should be set in production.)
@Vinnl Vinnl merged commit cda0f5a into main Sep 16, 2024
15 checks passed
@Vinnl Vinnl deleted the remove-appconstants branch September 16, 2024 10:40
Copy link

Cleanup completed - database 'blurts-server-pr-5044' destroyed, cloud run service 'blurts-server-pr-5044' destroyed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Review: XS Code review time: up to 30min
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants