Skip to content

LAB4_Set environment variables in your application

David Jones-Gilardi edited this page Feb 24, 2021 · 24 revisions

⚒️ Set environment variables in your application

Exercise time: ~5 minutes

Objectives

In this step, we will:

  • Configure a .env file with database environment variables

We will cover:

  1. Creating the .env file
  2. Explore the API with HTTPie

1. Creating the .env file

In the "hello world" section, we pushed a simple helloWorld "test" function to get a feel for how things work. Now, we are going to start working with the "real" code to get our game working. To do this, we need to set a group of environment variables referring to the database we just created with DataStax Astra, not only in our code, but across GitHub and Netlify as well.

Why are we doing this, you might ask? Because as part of our CI/CD pipeline our tests will attempt to connect to our data layer to ensure everything is hooked up and working. Not only that, but once you deploy your application to Netlify it will use these variables to hook up your production app and power your serverless functions.

We set these all ONE time and that's it, you are ready to go. With that, let's do it.

First things first, we need to create the .env file in our application to store our database information.

The following instructions are the same whether using GitPod or a local IDE.

✅ Step 1a: Go back to the Astra UI and click the CONNECT button above the display of the database you just created.

This will bring you to the Connect page.

Database connect

✅ Step 1b: Click the clipboard button to the far right to copy all of your environment variables to the copy buffer.

The Document API option should already be chosen by default. Notice the copy widget on the right of the UI.

Document API

✅ Step 1c: Ensure that you are in the "battlestax" directory wherever you are editing code (local or gitpod) and execute the following command to add your database environment variables to your code project.

📘 Command to execute

bash env.sh

✅ Step 1d: Follow the prompts to paste your copied variables and enter your password.

When completed, your .env file should look something like this:

Gitpod paste to env end result

2. Explore the API with HTTPie (OPTIONAL)

HTTPie—aitch-tee-tee-pie—is a user-friendly command-line HTTP client for the API era. It comes with JSON support, syntax highlighting, persistent sessions, wget-like downloads, plugins, and more.

Just to note, this section is completely optional, but we wanted to offer you a quick and easy way to test against the database credentials we just set before we get to the code section. Feel free to move on to the next section if you would like to.

Test the database with HTTPie

✅ Step 2a: Install HTTPie (LOCAL)

If you're using GitPod, we've already installed httpie for you, however, if you are running on a local IDE you'll need to install it.

📘 Command to execute

pip3 install httpie-astra

✅ Step 2b: Create a document

First, pick a 4 letter code for your game. This can be anything - the name of your cat, letters from your state, whatever you like. This will be the unique ID for this game in the system.

✔ For my example, I'll use ABCD.

📘 Command to execute

http --auth-type astra -a default: PUT :/rest/v2/namespaces/battlestax/collections/games/ABCD user=me --json

✔ If you want to see more information about this interaction, you can use -vvv to show more verbose output. If you're running it a second time you will need to change the code.

📘 Command to execute

http --auth-type astra -a default: PUT :/rest/v2/namespaces/battlestax/collections/games/EFGH user=me --json -vvv

✅ Step 2c: Retrieve the document you just created

✔ You can retrieve your specific document from the collection with:

📘 Command to execute

http --auth-type astra -a default: :/rest/v2/namespaces/battlestax/collections/games/ABCD

✔ If you want to simply pull the most recently created game, leave off your code:

📘 Command to execute

http --auth-type astra -a default: :/rest/v2/namespaces/battlestax/collections/games

✔ To see more documents in the database, set a page-size to tell it how many you want:

📘 Command to execute

http --auth-type astra -a default: :/rest/v2/namespaces/battlestax/collections/games page-size==10

Now you are set to run your application locally against your Astra database. We'll test all of that in an upcoming section. For now, let's move to the next section.

Clone this wiki locally