When coding this project some features may be difficult to test due to the dependent nature of this project.
One of the more notable issues in setting up local testing is authentication. For now authentication is handled via the OAuth2 standard. In short this means that we support logging in using another company's authentication. For example: Github.
Locally, we use docker-compose for testing. To install this follow the instructions for your operating system on the docker website. The quickest way is to use the Docker Desktop.
Environmental variables are a way that we pass information from the host machine into the container running in docker. For the scope of this project, think of environmental variables as key-value pairs that we get to set for each container and retrieve from the container at runtime, similarly to a configuration file.
In this project we store these environmental variables in a file named ".env". This file will hold constants that we define across events , hackathon , sponsors, and users.
The following excerpt shows what the .env file will look like without the values set:
OAUTH_GITHUB_CLIENT_ID=
OAUTH_GITHUB_CLIENT_SECRET=
OAUTH_GITHUB_REDIRECT_URL=
JWT_SIGNING_KEY=
AES_CIPHER=
You should create this .env file in the root directory in the project, meaning that the .env file should be in the same folder as the docker-compose.yaml file is.
As previous mentioned we use the OAuth2 standard for authenticating with 3rd party platforms. Creating this OAuth2 app is what allows us to retrieve this information from the users, when the user selects "Login with GitHub" on the website it will redirect them to GitHub with a popup asking if the user permits giving us their email address and other public information like their GitHub name. After granting us the permission the user will be redirected to the link set in the OAUTH_GITHUB_REDIRECT_URL environmental variable.
To fill in the environmental variables beginning with OAUTH_GITHUB follow this tutorial on how to create a GitHub OAuth app. The application name and homepage url do not matter. The callback url should be set to http://localhost:8080/auth_redirect for testing using the knighthacks_cli. Make sure to tick the box that says "Enable Device Flow".
After creating the OAuth App, retrieve the client id and client secret and set those values in the corresponding .env file entries. Set OAUTH_GITHUB_REDIRECT_URL to http://localhost:8080/auth_redirect.
You can smash your hands on your keyboard and create an under 100 character string.
Similarly to the JWT Signing Key, you can smash your hands on your keyboard and create an exactly 32 character string.
For the router microservice you first need to compose your schema from all of your microservice "subschemas" follow the instructions on installing Rover. Rover is the program that handles supergraph schema composition. For information on super and subgraphs checkout this.
In the router microservice there is a compose.sh file that you can run like so:
bash compose.sh dev
This command will generate a file called schema.graphql that will be NOT be source controlled and whenever you make a change to any of the other microservice's schemas you must re-run this command.
Using docker-compose, and it's configuration stored in the docker-compose.yaml you can use the docker-compose up command to run the project.
Before going any further make sure to do the following command in the main directory of the project the before proceeding:
docker-compose up --build postgres
After about 30 seconds of the program running it should say postgres can now accept
If a change was made to the code since the last build of the docker image append the --build
flag at the end of
command.
For example:
docker-compose up --build
If you would like to just spin up a certain service you can do the previous command with the name of the service provided in the docker-compose.yaml file appended. For example, to start up the postgres service you would do the following:
docker-compose up --build postgres
If you just want to start up a single service, for example: users, you can do:
docker-compose up --build users
This will turn on the users service, and its dependent services such as postgres and router.
Whenever you do docker-compose up
by default it runs live in the current terminal widow
Connecting to the Apollo Router microservice now running is as simple as opening your browser and going to http://localhost:4000. Once you open that you will be able to open Apollo Studio and explore the GraphQL API.
Using the KnightHacks CLI in the knighthacks_cli repository you can register, sign in, or do whatever you'd like to do with the API. Ensure you follow the instructions on the knighthacks_cli repository.
Most importantly, you need a JWT to be able to send authenticated requests to the backend. To get that JWT you must use the knighthacks_cli.
First, register an account using the CLI. For example:
./knighthacks_cli auth register --first-name Joe --last-name Mama --email [email protected] --phone 1234567890
Then, login to the account:
./knighthacks_cli auth login
Copy the JWT it gives when you execute the login command. Ensure that the copied JWT does not have any spaces in it then
add it as a HTTP header in the apollo studio website where the header name/key is authorization
and the value
is bearer JWT
where JWT
is the JWT you copied.
The next step is to set the role of the user you just registered as an admin. To do that you will use
the set_user_role.sh
script. Ensure the database is online when using this command.
First, using the previous knighthacks_cli login command retrieve the User ID
from the output of the command.
Enter the scripts folder and execute the following command:
bash set_user_role.sh {USER_ID} ADMIN
Where {USER_ID}
is the ID from the login command.