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

Fixes #1576, Added FRONTEND env variable, updated routes/index.js #1606

Closed
wants to merge 7 commits into from

Conversation

Metropass
Copy link
Contributor

@Metropass Metropass commented Jan 25, 2021

Issue This PR Addresses

Closes #1576
Created a FRONTEND variable for all of the .env files, and added a conditional if within backend/routes/index.js
Currently, the only two values that can be accepted for FRONTEND are: gatsby and next
gatsby is the default value as of this Pull Request

Type of Change

  • Bugfix: Change which fixes an issue
  • New Feature: Change which adds functionality
  • Documentation Update: Change which improves documentation
  • UI: Change which improves UI

Description

Checklist

  • Quality: This PR builds and passes our npm test and works locally
  • Tests: This PR includes thorough tests or an explanation of why it does not
  • Screenshots: This PR includes screenshots or GIFs of the changes made or an explanation of why it does not (if applicable)
  • Documentation: This PR includes updated/added documentation to user exposed functionality or configuration variables are added/changed or an explanation of why it does not(if applicable)

env.example Outdated
# 'gatsby' - The Gatsby Front-End
# 'next' - The NextJs Front-End
# Our default value for FRONTEND is 'gatsby'
FRONTEND='gatsby'
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the surrounding quotes, all values are strings:

FRONTEND=gatsby

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do, thanks!

netlify.toml Outdated
@@ -8,3 +8,4 @@
API_URL="https://dev.telescope.cdot.systems"
# We need to expose the API_URL to the front-end too
NEXT_PUBLIC_API_URL="https://dev.telescope.cdot.systems"
FRONTEND='gatsby'
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be next on Netlify.

@@ -17,7 +17,7 @@ const user = require('./user');
const query = require('./query');

const router = express.Router();

const { FRONTEND } = process.env.FRONTEND;
Copy link
Contributor

Choose a reason for hiding this comment

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

In the rest of this code, we seem to be doing process.env.FRONTEND inline, instead of pulling things out. Can you match that below?

Copy link
Member

Choose a reason for hiding this comment

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

Careful here: using destructuring, you have to remove the name of the variable in the rhs.

const { FRONTEND } = process.env;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do! Everything should be fixed along with the rest of your suggestions

} else if (FRONTEND === 'next') {
router.use(express.static(path.join(__dirname, '../../../frontend/next/out')));
} else {
throw new Error(
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's test that this does what we want (e.g., we should probably crash right now).

// Or serve the static files in the Gatsby build directory
} else if (FRONTEND === 'gatsby') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's make sure that case doesn't affect these (e.g., .toLowerCase() on the frontend values we compare against).

Copy link
Contributor

@humphd humphd 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. @Metropass, please update the team tomorrow about this change so people can update their local .env, since it will throw right now if it's missing.

Question related to that: should we default to Gatsby if it's missing instead of throwing? That is:

  • gatsby: use gatsby
  • next: use next
  • {not set}: use gatsby
  • anything else: throw

@@ -17,7 +17,6 @@ const user = require('./user');
const query = require('./query');

const router = express.Router();

Copy link
Contributor

Choose a reason for hiding this comment

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

Watch out for spurious whitespace changes like this, as it churns git history unnecessarily.

@HyperTHD
Copy link
Contributor

HyperTHD commented Jan 26, 2021

Looks good. @Metropass, please update the team tomorrow about this change so people can update their local .env, since it will throw right now if it's missing.

Question related to that: should we default to Gatsby if it's missing instead of throwing? That is:

  • gatsby: use gatsby
  • next: use next
  • {not set}: use gatsby
  • anything else: throw

I say for now, we should have it default to gatsby since the next transition is not complete yet. Whenever that's complete, unless we have any sort of reason to keep gatsby, we can just have it serve next, and throw if anything else other than that is used

@@ -45,13 +45,13 @@ if (process.env.NODE_ENV === 'development') {
// Allow proxying the Gatsby dev server through our backend if PROXY_FRONTEND=1 is set in env
router.use('/', createProxyMiddleware({ target: 'http://localhost:8000', changeOrigin: true }));
// Or serve the static files in the Gatsby build directory
} else if (process.env.FRONTEND.toLowerCase() === 'gatsby') {
} else if (process.env.FRONTEND.toLowerCase() === 'gatsby' || process.env.FRONTEND === '') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: will process.env.FRONTEND.toLowerCase() break if FRONTEND isn't defined? That is, should this be:

else if(!process.env.FRONTEND || process.env.FRONTEND === '' || process.env.FRONTEND.toLowerCase() === 'gatsby') {...}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's a good point, I'll make that change real quick

@manekenpix
Copy link
Member

is this ready for review?

@Metropass
Copy link
Contributor Author

@manekenpix Should be ready now!

@yuanLeeMidori yuanLeeMidori requested a review from humphd February 9, 2021 15:10
@chrispinkney
Copy link
Contributor

@Metropass This might be a stupid question but how can I test this? Also does this mean I have to copy a new example .env file after this merges?

@@ -4,6 +4,16 @@ NODE_ENV=development
# PORT is the port used by the web server
PORT=3000
Copy link
Contributor

@chrispinkney chrispinkney Feb 10, 2021

Choose a reason for hiding this comment

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

Does this change affect this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does, thanks for bring it up.
It seems like I need to make a conditional statement within the package.json
@humphd cc'ing just for clarity, Are we allowed to make conditionals in our packages for Telescope?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think they're related. PORT=3000 is for telescope (telescope serving the back end), The change @chrispinkney is talking about (this) is for next's production server (a separate server to serve the production build, but telescope has nothing to do with it).

@Metropass
Copy link
Contributor Author

@Metropass This might be a stupid question but how can I test this? Also does this mean I have to copy a new example .env file after this merges?

It's not a stupid question at all!
Just

@Metropass This might be a stupid question but how can I test this? Also does this mean I have to copy a new example .env file after this merges?

It's not a stupid question at all!
Just have the FRONTEND variable set to which ever Front-End service you'd like to run eg, FRONTEND=gatsby, and index.js should take care of the rest.

Copy link
Contributor

@chrispinkney chrispinkney left a comment

Choose a reason for hiding this comment

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

Otherwise LGTM!

Start redis and elasticsearch in docker:
npm run build
npm run build:next
set FRONTEND=gatsby
npm start
set FRONTEND=next
npm start

Hope this is right 😄. Everything started up and ran

@@ -4,6 +4,16 @@ NODE_ENV=development
# PORT is the port used by the web server
PORT=3000

Copy link
Contributor

Choose a reason for hiding this comment

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

Haha sorry, I know it's pedantic, but can you get rid of the extra newlines on line 6 and 15?

Copy link
Contributor

@HyperTHD HyperTHD left a comment

Choose a reason for hiding this comment

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

This PR looks okay. We're switching to next and not gatsby so we can make this exclusively next instead. You'll need to rebase and fix conflicts here too.

Read #1733 for context

@chrispinkney
Copy link
Contributor

This PR looks okay. We're switching to next and not gatsby so we can make this exclusively next instead. You'll need to rebase and fix conflicts here too.

Read #1733 for context

Hm good point. In fact AFAIK we'll be removing Gatsby entirely when Next finally lands. Not sure! Something to think about I guess @Metropass

@humphd
Copy link
Contributor

humphd commented Feb 14, 2021

This was started nearly a month ago, and we needed it then, but at this point, I don't think it's useful, since we're moving to a single front-end. Closing.

@humphd humphd closed this Feb 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use an env var to determine which front-end gets served
5 participants