This repository contains the API of the SafetyWare application. The frontend is in another repository.
The following informal diagram shows data flow and the relationship between the API and other components.
Follow these steps if you need to run the API without making code changes.
- Install Docker Desktop.
- Run the API.
docker compose up --build
- Navigate to http://localhost:3001/playground.
The API exposes all operations through GraphQL. A web interface is included for testing queries.
- Start the API as described in the 'Quickstart' section.
- Load sample data as described later in this document (optional).
- Navigate to http://localhost:3001/playground.
- Execute the following example query.
{ companies { id name people { id name locationReadings { timestamp coordinates } } } }
This section describes how to make code changes.
- Install Docker Desktop.
- Install Rust Programming Language.
CLion is the recommended IDE. CLI commands are also supported.
- Open this repository in CLion.
- Install required plugins when prompted.
- Confirm the "Start containers" configuration is able to run.
- Configure CLion to use Rustfmt.
- Press Ctrl + Shift + A to search actions.
- Search "Rustfmt" and open the first result.
- Enable "Use Rustfmt instead of built-in formatter" and "Run rustfmt on Save".
- Execute the "Run" configuration. Docker containers are started automatically.
- Navigate to http://localhost:3001.
- Stop the Docker containers in the Services tab before leaving.
- Start the Docker containers once per sitting.
docker compose up -d --build mongo
- Set environmental variables.
$env:SW_DB_URI="mongodb://localhost:42781" $env:SW_PRIVATE_KEY="secret" $env:RUST_LOG="info" $env:RUST_BACKTRACE="1"
- Build and run.
cargo run -p api
- Navigate to http://localhost:3001.
- Stop the Docker containers before leaving.
docker compose down
- Run the tests.
.\script\Test.ps1
This section describes how to load and save sample data. PowerShell Core is required.
- Load sample data. Your existing database contents will be dropped.
.\script\LoadSampleData.ps1 sample-data.gz
- Save database contents as sample data.
.\script\SaveSampleData.ps1 sample-data.gz
This section describes how to deploy the API to the cloud.
- Install Azure CLI.
- Install Azure Functions Core Tools.
- Install Docker Desktop.
- Install MongoDB CLI for Cloud.
- Install PowerShell Core (not preinstalled PowerShell).
- Install Rust Programming Language.
- Create a Microsoft Azure account.
- Create a MongoDB Atlas account.
- Connect the Azure CLI to your Azure account.
az login
- Connect the MongoDB Cloud CLI to your MongoDB Atlas account.
mongocli config
- Publish the API to the specified environment. It will be publicly accessible.
.\script\Publish.ps1 -Org cap -App sw -Env dev
- You can pick something other than
dev
as the environment name, such as your name, to avoid name conflicts.
- You can pick something other than
- Unpublish the API and delete all cloud resources.
.\script\Unpublish.ps1 -Org cap -App sw -Env dev
SpectaQL is used to automatically generate GraphQL documentation. The generated documentation is saved in the repository. It should not be edited manually.
- Install NodeJS.
- Install SpectaQL.
npm install -g spectaql@0.12
- Start the API locally.
- Navigate to the doc directory.
cd doc
- Generate documentation.
npx spectaql config.yml
- Navigate to
/doc/
to see documentation.
When the application starts for the first time, there may be no users. An admin user is required to create other users through the API. The first admin user can be created manually. By default, accounts have no password.
- Insert a temporary account into the database using MongoDB Shell.
db.user_account.insertOne({ _id: 'tempAdmin', name: '', access: 'admin', title: '', email: '', phone: '', company_id: '', })
- Login with the temporary account using the GraphQL interface at
/playground
. Take note of the returned bearer token.mutation { login(userAccountId: "tempAdmin", password: "") }
- In the HTTP headers tab at the bottom of the page, set the HTTP authorization header to the bearer token.
{"Authorization": "Bearer {token}"}
- Create a genuine admin account. Take note of the returned user account ID.
mutation { createUserAccount(input: { name: "User A", access: ADMIN, title: "Chief Observer", email: "user.a@example.com", phone: "(111) 111-1111", companyId: "" }) { id name } }
- Set a password for the new admin account.
mutation { setUserAccountPassword( userAccountId: "{user account ID}", password: "{password}" ) }
- Delete the temporary admin account.
mutation { deleteUserAccount(id: "tempAdmin") }