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

Fix #664: Use API_URL from .env for telescope API #690

Merged
merged 1 commit into from
Feb 10, 2020

Conversation

humphd
Copy link
Contributor

@humphd humphd commented Feb 9, 2020

Issue This PR Addresses

Fixes #664

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

This adds an API_URL environment variable, and the logic for using it in various build steps. The API_URL should be one of the URLs we use for our deployments: localhost, staging, or (eventually) production.

You choose the backend you want your frontend to work against by setting this value in your .env, or otherwise setting this environment variable. When you run one of the GatsbyJS cli scrips (build, develop, etc) it will read this value via the code in src/frontend/gatsby-config.js. Here we'll try to load the root .env file, and import any environment variables, then try to pick the correct API_URL to use, falling back to http://localhost:PORT if none is found. This means we don't need the http-proxy-middleware anymore, since it will use whatever URL you specify without the need for proxying.

In our Zeit Now build, I specify that I want it to always use the staging URL. This way our preview deployments will have live data.

In terms of how you consume this variable in the frontend, I did some research and the way Gatsby wants you to do it is via GraphQL to query the sitemetadata. There are two ways to do this:

I did both because we use both methods in our tree, and now there are examples and reusable code (the custom hook) we can reuse.

I've never used either of these before, so it's possible I'm doing some of it wrong. Check my work.

@humphd
Copy link
Contributor Author

humphd commented Feb 9, 2020

It looks like the Zeit Now setup worked using this: https://telescope-9ykv7wjv6.now.sh/

manekenpix
manekenpix previously approved these changes Feb 9, 2020
Copy link
Member

@manekenpix manekenpix left a comment

Choose a reason for hiding this comment

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

Tested locally with your branch.
I tried API_URL=http://localhost:3000 and API_URL=https://dev.telescope.cdot.systems.
Both 👍

lozinska
lozinska previously approved these changes Feb 10, 2020
@humphd
Copy link
Contributor Author

humphd commented Feb 10, 2020

I've rebased this to fix a conflict and will merge now.

@humphd humphd merged commit a9fa96f into Seneca-CDOT:master Feb 10, 2020
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.

Use env variable for Telescope API URL in Gatsby app
3 participants