-
Notifications
You must be signed in to change notification settings - Fork 18
Running the Dev Containers
This guide assumes you have completed the following prerequisite tasks.
- Setup 1Password and have access to the Notify folders
- Have access to your own AWS accounts for both the Staging and Production environments
- Docker Desktop is installed on your machine
- Visual Studio Code is installed on your machine
- Cloned the API , Admin, and notification-document-download-api repos
- If it is not already installed, add the Dev Containers extension to VSCode
- Open the Command Palette with
⌘ + shift + p
- Type
Dev Containers
and select theOpen folder in container
option - Navigate to and open the Admin project
- The Dev Container should begin building. You should notice output in the terminal.
If you're familiar with Docker and Dev Containers already, feel free to skip this section.
Since you're waiting for the dev container and project to build, now is a good time to learn about the basics of the dev container setup.
In each project you'll notice a .devcontainer
folder structured similarly to the one pictured below.
Let's briefly go over how these files relate to one another.
A set of instructions used by Docker to build an image that serves as the basis for the our Dev Container environment. OS and root level operations are defined here.
Install OS level dependencies and packages (
git, npm, nodejs, vim
etc.), perform root-level operations (chown, chmod, set env vars
etc)
These shell scripts execute within the the container after it is built. They are geared toward user space actions and general project setup.
Set up terminal aliases and autocompletions, install python/JS dependencies, build the project
This file brings everything together. It instructs VSCode how to handle creation of, and access to a dev container as well as automatically configuring VSCode extensions.
Which Dockerfile is used to build the container, will we use bash or zsh, what VSCode extensions are should be installed, which scripts should run after the container is built
Each project relies on a .env
Flask configuration file that should be placed in the root of the project. You can find one for each respective project in 1Password: Canadian Digital Service > CDS Platform - Notify Local (NEW)
.
- Create a file named
.env
at the root of each project - Copy the contents from 1Password into each respective
.env
file.
The last piece needed is a launch configuration to instruct VSCode how to launch the apps. The configs can be found in 1Password.
- Add the following directory structure in each project: `<project_root>/.vscode
- In the
.vscode
directory, create a file calledlaunch.json
- Copy the contents from 1Password into each respective
launch.json
file
With the configurations in place and the dev containers built, running the application is as simple as:
- Navigate to
Run and Debug
- Select
Python: Flask
from the drop down - Press the green arrow
- Run the Admin project.
- Once the app has started, you should see configuration output in your terminal.
- Navigate to http://localhost:6012, you should see the following page:
You're seeing this message because we also need to run the API app to provide Admin with a means to interact with the data.
Start API the same way you started Admin, running both the Python: Flask
and Python: Celery
apps, then refresh the page in your browser. If you see the following, congrats! You've successfully setup GCNotify locally!
- Create an account
- log in
- Create a service and template
- Perform a send
If all goes well, you're ready to start developing. If you ran into issues check out the troubleshooting section or hit up one of your fellow devs and we'll help you out.
In general, Admin and API suffice for most day to day work. However if you need to send notifications with attached files, the Document Download API app needs to be running.
Once you built and ran the DD-API container and app you'll need to change a few service settings, add a new template, and create an API key to send an email notification with an attachment.
- Log in and navigate to your service settings
- Under the
Emails
settings enable theSend files by email
setting - Navigate to your Dashboard and create a new email template
Make sure to include a variable in the template body,
((var))
will do.
- Navigate to
API Integration > API keys
and create aLive API key
Take note of the API key as it is needed in the next steps
- Use a tool like Postman to send the following request to:
http://localhost:6011/v2/notifications/email
Make sure you set the following headers:
Authorization
: The API key you created in step 4Content-Type
: application/json
{
"email_address": "<YOUR EMAIL ADDRESS>",
"template_id": "<TEMPLATE ID>",
"personalisation": {
"var": "var",
"application_file": {
"file": "aGkgdGhlcmU=",
"filename": "test_file.txt",
"sending_method": "attach"
}
}
}
- Check your email and make sure you can open the attached or linked file.
Alternatively you can use a curl command:
curl -i -X POST \
-H "Authorization:ApiKey-v1 <YOUR API KEY>" \
-H "Content-Type:application/json" \
-d \
'{
"email_address": "<YOUR EMAIL ADDRESS>",
"template_id": "<TEMPLATE ID>",
"personalisation": {
"var": "var",
"application_file": {
"file": "aGkgdGhlcmU=",
"filename": "test_file.txt",
"sending_method": "attach"
}
}
}' \
'http://localhost:6011/v2/notifications/email'
I see the error: "Sorry, we’re experiencing technical difficulties" when I navigate to the home page
- Have you built and ran the the API app?
- In the admin project, check that the
API_HOST_NAME
in your.env
file is pointing to the correct address:http://host.docker.internal:6011
.
In simple terms, host.docker.internal is like the Docker version of localhost, except with a little extra DNS magic. It is scoped to a virtual network in Docker In this case the
bridge
network.
If the above is in order, it's time to inspect the bridge
network in Docker, to ensure that both the Admin and API containers are both present. Open a terminal and:
- Identify the ADMIN and API containers:
docker container ls
- Check the bridge network:
docker network inspect bridge
- Add any missing containers to the bridge network:
docker network connect bridge <container-id>
We use a tool called Poetry to create and automagically manage the Python virtual environments that the apps run in. Occasionally VSCode doesn't pick up this virtual environment and must be pointed to manually.
- Open a Python source file in VSCode
- Check the bottom status bar and ensure the selected Python interpreter indicates Poetry:
If this isn't present, right click the status bar and check the
Selected Python Interpreter
option.
- To change the selected Interpreter click current interpreter on the status bar and select the Poetry environment in the Command Palette window that opens.
If the Poetry environment isn't listed
- Select
Enter interpreter path...
- Navigate to
/home/vscode/.cache/pypoetry/virtualenvs/<project-name-some_id-py3.10>/bin/python3.10